Future Options

Handling Data

Introduction

LEAN passes the data you request to the OnData method so you can make trading decisions. The default OnData method accepts a Slice object, but you can define additional OnData methods that accept different data types. For example, if you define an OnData method that accepts a TradeBar argument, it only receives TradeBar objects. The Slice object that the OnData method receives groups all the data together at a single moment in time. To access the Slice outside of the OnData method, use the CurrentSlice property of your algorithm.

All the data formats use DataDictionary objects to group data by Symbol and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the TradeBars DataDictionary is made up of TradeBar objects. To access individual data points in the dictionary, you can index the dictionary with the Futures Option contract ticker or Symbol, but we recommend you use the Symbol.

Futures Chains

FuturesChain objects represent an entire chain of contracts for a single underlying Future. They have the following properties:

To get the FuturesChain, index the FuturesChains property of the Slice with the continuous contract Symbol.

public override void OnData(Slice slice)
{
    if (slice.FuturesChains.TryGetValue(_futureContractSymbol.Canonical, out var chain))
    {
        //
    }
}
def OnData(self, slice: Slice) -> None:
    chain = slice.FuturesChains.get(self.future_contract_symbol.Canonical)
    if chain:
        pass

You can also loop through the FuturesChains property to get each FuturesChain.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.FuturesChains)
    {
        var continuousContractSymbol = kvp.Key;
        var chain = kvp.Value;
    }
}

public void OnData(FuturesChains futuresChains)
{
    foreach (var kvp in futuresChains)
    {
        var continuousContractSymbol = kvp.Key;
        var chain = kvp.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    for continuous_contract_symbol, chain in slice.FuturesChains.items():
        pass

Futures Contracts

FuturesContract objects represent the data of a single Futures contract in the market. They have the following properties:

To get the Futures contracts in the Slice, use the Contracts property of the FuturesChain.

public override void OnData(Slice slice)
{
    if (slice.FuturesChains.TryGetValue(_futureContractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_futureContractSymbol, out var contract))
        {
            //
        }
    }
}

public void OnData(FuturesChains futuresChains)
{
    if (futuresChains.TryGetValue(_futureContractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_futureContractSymbol, out var contract))
        {
            //
        }
    }
}
def OnData(self, slice: Slice) -> None:
    chain = slice.FuturesChains.get(self.future_contract_symbol.Canonical)
    if chain:
        contract = chain.Contracts.get(self.future_contract_symbol)
        if contract:
            pass

Option Chains

OptionChain objects represent and entire chain of Option contracts for a single underlying security. They have the following properties:

To get the OptionChain, index the OptionChains property of the Slice with the canonical Symbol.

public override void OnData(Slice slice)
{
    if (slice.OptionChains.TryGetValue(_optionContractSymbol.Canonical, out var chain))
    {
        //
    }
}

public void OnData(OptionChains optionChains)
{
    if (optionChains.TryGetValue(_optionContractSymbol.Canonical, out var chain))
    {
        //
    }
}
def OnData(self, slice: Slice) -> None:
    chain = slice.OptionChains.get(self.option_contract_symbol.Canonical)
    if chain:
        pass

You can also loop through the OptionChains property to get each OptionChain.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.OptionChains)
    {
        var canonicalFOPSymbol = kvp.Key;
        var chain = kvp.Value;
    }
}

public void OnData(OptionChains optionChains)
{
    foreach (var kvp in optionChains)
    {
        var canonicalFOPSymbol = kvp.Key;
        var chain = kvp.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    for canonical_fop_symbol, chain in slice.OptionChains.items():
        pass

Option Contracts

OptionContract objects represent the data of a single Option contract in the market. They have the following properties:

For more information about Greeks, see The Greek Letters.

To get the Option contracts in the Slice, use the Contracts property of the OptionChain.

public override void OnData(Slice slice)
{
    if (slice.OptionChains.TryGetValue(_optionContractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_optionContractSymbol, out var contract))
        {
            //
        }
    }
}

public void OnData(OptionChains optionChains)
{
    if (optionChains.TryGetValue(_optionContractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_optionContractSymbol, out var contract))
        {
            //
        }
    }
}
def OnData(self, slice: Slice) -> None:
    chain = slice.OptionChains.get(self.option_contract_symbol.Canonical)
    if chain:
        contract = chain.Contracts.get(self.option_contract_symbol)
        if contract:
            pass

You can also iterate through the FuturesChains first.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.FuturesChains)
    {
        var continuousContractSymbol = kvp.Key;
        var futuresChain = kvp.Value;
        
        // Select a Future Contract and create its canonical FOP Symbol
        var futuresContract = futuresChain.First();
        var canonicalFOPSymbol = QuantConnect.Symbol.CreateCanonicalOption(futuresContract.Symbol);
        if (slice.OptionChains.TryGetValue(canonicalFOPSymbol, out var optionChain))
        {
            if (optionChain.Contracts.TryGetValue(_optionContractSymbol, out var optionContract))
            {
                //
            }
        }
    }
}

public void OnData(FuturesChains futuresChains)
{
    foreach (var kvp in futuresChains)
    {
        var continuousContractSymbol = kvp.Key;
        var futuresChain = kvp.Value;
        
        // Select a Future Contract and create its canonical FOP Symbol
        var futuresContract = futuresChain.First();
        var canonicalFOPSymbol = QuantConnect.Symbol.CreateCanonicalOption(futuresContract.Symbol);
        if (slice.OptionChains.TryGetValue(canonicalFOPSymbol, out var optionChain))
        {
            if (optionChain.Contracts.TryGetValue(_optionContractSymbol, out var optionContract))
            {
                //
            }
        }
    }
}
def OnData(self, slice: Slice) -> None:
    for continuous_future_symbol, futures_chain in slice.FuturesChains.items():
        # Select a Future Contract and create its canonical FOP Symbol
        futures_contract = [contract for contract in futures_chain][0]
        canonical_fop_symbol = Symbol.CreateCanonicalOption(futures_contract.Symbol)
        option_chain = slice.OptionChains.get(canonical_fop_symbol)
        if option_chain:
            option_contract = option_chain.Contracts.get(self.option_contract_symbol)
            if option_contract:
                pass

Trades

TradeBar objects are price bars that consolidate individual trades from the exchanges. They contain the open, high, low, close, and volume of trading activity over a period of time.

TradeBar objects have the following properties:

To get the TradeBar objects in the Slice, index the Slice or index the Bars property of the Slice with the Option contract Symbol. If the Option contract doesn't actively trade or you are in the same time step as when you added the Option contract subscription, the Slice may not contain data for your Symbol. To avoid issues, check if the Slice contains data for your Option contract before you index the Slice with the Option contract Symbol.

public override void OnData(Slice slice)
{
    if (slice.Bars.ContainsKey(_optionContractSymbol))
    {
        var tradeBar = slice.Bars[_optionContractSymbol];
    }
}

public void OnData(TradeBars tradeBars)
{
    if (tradeBars.ContainsKey(_optionContractSymbol))
    {
        var tradeBar = tradeBars[_optionContractSymbol];
    }
}
def OnData(self, slice: Slice) -> None:
    if self.option_contract_symbol in slice.Bars:
        trade_bar = slice.Bars[self.option_contract_symbol]

You can also iterate through the TradeBars dictionary. The keys of the dictionary are the Symbol objects and the values are the TradeBar objects.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.Bars)
    {
        var symbol = kvp.Key;
        var tradeBar = kvp.Value;
    }
}

public void OnData(TradeBars tradeBars)
{
    foreach (var kvp in tradeBars)
    {
        var symbol = kvp.Key;
        var tradeBar = kvp.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    for symbol, trade_bar in slice.Bars.items():
        pass

Quotes

QuoteBar objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The Open, High, Low, and Close properties of the QuoteBar object are the mean of the respective bid and ask prices. If the bid or ask portion of the QuoteBar has no data, the Open, High, Low, and Close properties of the QuoteBar copy the values of either the Bid or Ask instead of taking their mean.

QuoteBar objects have the following properties:

To get the QuoteBar objects in the Slice, index the QuoteBars property of the Slice with the Option contract Symbol. If the Option contract doesn't actively get quotes or you are in the same time step as when you added the Option contract subscription, the Slice may not contain data for your Symbol. To avoid issues, check if the Slice contains data for your Option contract before you index the Slice with the Option contract Symbol.

public override void OnData(Slice slice)
{
    if (slice.QuoteBars.ContainsKey(_optionContractSymbol))
    {
        var quoteBar = slice.QuoteBars[_optionContractSymbol];
    }
}

public void OnData(QuoteBars quoteBars)
{
    if (quoteBars.ContainsKey(_optionContractSymbol))
    {
        var quoteBar = quoteBars[_optionContractSymbol];
    }
}
def OnData(self, slice: Slice) -> None:
    if self.option_contract_symbol in slice.QuoteBars:
        quote_bar = slice.QuoteBars[self.option_contract_symbol]

You can also iterate through the QuoteBars dictionary. The keys of the dictionary are the Symbol objects and the values are the QuoteBar objects.

public override void OnData(Slice slice)
{
    foreach (var kvp in slice.QuoteBars)
    {
        var symbol = kvp.Key;
        var quoteBar = kvp.Value;
    }
}

public void OnData(QuoteBars quoteBars)
{
    foreach (var kvp in quoteBars)
    {
        var symbol = kvp.Key;
        var quoteBar = kvp.Value;
    }
}
def OnData(self, slice: Slice) -> None:
    for symbol, quote_bar in slice.QuoteBars.items():
        pass

QuoteBar objects let LEAN incorporate spread costs into your simulated trade fills to make backtest results more realistic.

Open Interest

Open interest is the number of outstanding contracts that haven't been settled. It provides a measure of investor interest and the market liquidity, so it's a popular metric to use for contract selection. Open interest is calculated once per day. To get the latest open interest value, use the OpenInterest property of the Option or OptionContract.

public override void OnData(Slice slice)
{
    if (slice.OptionChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_contractSymbol, out var contract))
        {
            var openInterest = contract.OpenInterest;
        }
    }
}

public void OnData(OptionChains optionChains)
{
    if (optionChains.TryGetValue(_contractSymbol.Canonical, out var chain))
    {
        if (chain.Contracts.TryGetValue(_contractSymbol, out var contract))
        {
            var openInterest = contract.OpenInterest;
        }
    }
}
def OnData(self, slice: Slice) -> None:
    chain = slice.OptionChains.get(self.contract_symbol.Canonical)
    if chain:
        contract = chain.Contracts.get(self.contract_symbol)
        if contract:
            open_interest = contract.OpenInterest

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: