Skip to content
All library documents

Building and Closing Long and Short Call Calendar Spreads

Article Strategy library · Author: QuantConnect

Summary

This example demonstrates constructing a call calendar spread from an options chain and submitting it as a grouped strategy order. It selects call contracts, sorts them by proximity to the underlying price, then looks for a strike with contracts at multiple expirations. At the selected strike, the nearer expiration is short and the farther expiration is long, with two units of the spread submitted.

The example checks that the resulting position group contains two legs with the expected quantities and expirations. It also illustrates closing the position by submitting the inverse, short call calendar spread. The code is an implementation example rather than a trading analysis: it provides no market rationale, pricing assumptions, backtest period, or performance evidence. Contract availability and the chain-selection logic constrain which spread can be formed, and the description does not explain how expiration risk or execution costs are handled.

Key ideas

  • A call calendar spread combines calls at the same strike with different expirations.
  • The example chooses a strike near the underlying and selects the first two available expirations for that strike.
  • Buying two spread units results in a short near-expiration call and a long farther-expiration call, each with quantity two.
  • The position group is checked for its expected legs and quantities.
  • The example closes the spread using the inverse strategy, without reporting performance or execution assumptions.

Tags

Full text
# LongAndShortCallCalendarSpreadStrategiesAlgorithm


# LongAndShortCallCalendarSpreadStrategiesAlgorithm









This algorithm demonstrate how to use OptionStrategies helper class to batch send orders for common strategies. In this case, the algorithm tests the Call Calendar Spread and Short Call Calendar Spread strategies.

## Source (Apache-2.0)

```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

import itertools

from OptionStrategyFactoryMethodsBaseAlgorithm import *

### <summary>
### This algorithm demonstrate how to use OptionStrategies helper class to batch send orders for common strategies.
### In this case, the algorithm tests the Call Calendar Spread and Short Call Calendar Spread strategies.
### </summary>
class LongAndShortCallCalendarSpreadStrategiesAlgorithm(OptionStrategyFactoryMethodsBaseAlgorithm):

    def expected_orders_count(self) -> int:
        return 4

    def trade_strategy(self, chain: OptionChain, option_symbol: Symbol) -> None:
        call_contracts = sorted((contract for contract in chain if contract.right == OptionRight.CALL),
                           key=lambda x: abs(x.strike - chain.underlying.value))
        for strike, group in itertools.groupby(call_contracts, lambda x: x.strike):
            contracts = sorted(group, key=lambda x: x.expiry)
            if len(contracts) < 2:
                continue

            self._near_expiration = contracts[0].expiry
            self._far_expiration = contracts[1].expiry

            self._call_calendar_spread = OptionStrategies.call_calendar_spread(option_symbol, strike, self._near_expiration, self._far_expiration)
            self._short_call_calendar_spread = OptionStrategies.short_call_calendar_spread(option_symbol, strike, self._near_expiration, self._far_expiration)
            self.buy(self._call_calendar_spread, 2)
            return

    def assert_strategy_position_group(self, position_group: IPositionGroup, option_symbol: Symbol) -> None:
        positions = list(position_group.positions)
        if len(positions) != 2:
            raise AssertionError(f"Expected position group to have 2 positions. Actual: {len(positions)}")

        near_expiration_position = next((position for position in positions
                                       if position.symbol.id.option_right == OptionRight.CALL and position.symbol.id.date == self._near_expiration),
                                      None)
        if not near_expiration_position or near_expiration_position.quantity != -2:
            raise AssertionError(f"Expected near expiration position to be -2. Actual: {near_expiration_position.quantity}")

        far_expiration_position = next((position for position in positions
                                      if position.symbol.id.option_right == OptionRight.CALL and position.symbol.id.date == self._far_expiration),
                                     None)
        if not far_expiration_position or far_expiration_position.quantity != 2:
            raise AssertionError(f"Expected far expiration position to be 2. Actual: {far_expiration_position.quantity}")

    def liquidate_strategy(self) -> None:
        # We should be able to close the position using the inverse strategy (a short call calendar spread)
        self.buy(self._short_call_calendar_spread, 2)

```

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.