QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.46k stars 3.21k forks source link

Empty Option Chains for Future Options #8277

Open AlexCatarino opened 2 weeks ago

AlexCatarino commented 2 weeks ago

Expected Behavior

No need to define SetFilter for the underlying future. The option chain should have the mapped contract.

Actual Behavior

If we need to set the filter.

Potential Solution

N/A

Reproducing the Problem

# region imports
from AlgorithmImports import *
# endregion

class BasicFutureOptionAlgorithm(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2023,1,1)
        self._future = self.add_future(Futures.Indices.SP_500_E_MINI,
            extended_market_hours=True,
            data_mapping_mode=DataMappingMode.OPEN_INTEREST,
            data_normalization_mode=DataNormalizationMode.BACKWARDS_RATIO,
            contract_depth_offset=0)
        #self._future.set_filter(0, 182)   # < --uncomment to get options

        self.add_future_option(self._future.symbol, lambda u: u.strikes(-1, 1))

    def on_data(self, data):
        # Iterate all Option chains to find the desired future and future options contracts 
        for canonical_symbol, chain in data.option_chains.items():
            future_contract = canonical_symbol.underlying
            for symbol, contract in chain.contracts.items():
                contract.strike

        # Get the Option chain for the mapped contract 
        chain = data.option_chains.get(Symbol.create_canonical_option(self._future.mapped))
        if chain:
            for symbol, contract in chain.contracts.items():
                contract.strike

Checklist

AlexCatarino commented 2 weeks ago

By the way, it would be nice to be able to get the chain with the underlying symbol:

chain = data.option_chains.get(self._future.mapped)