Index Options

Requesting Data

Introduction

Request Index Options data in your algorithm to receive a feed of contract prices in the OnData method. For more information about the specific dataset we use for backtests, see the US Index Options dataset listing.

Create Subscriptions

Before you can subscribe to an Index Option contract, you must configure the underlying Index and get the contract Symbol.

Configure the Underlying Index

If you want to subscribe to the underlying Index in the Initialize method, set the underlying volatility model of the Index and warm it up.

var index = AddIndex("SPX");
index.VolatilityModel = new StandardDeviationOfReturnsVolatilityModel(30);
_symbol = index.Symbol;
SetWarmup(30, Resolution.Daily);
index = self.AddIndex("SPX")
index.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)
self.symbol = index.Symbol
self.SetWarmup(30, Resolution.Daily)

If you trade Options for many Indices in your algorithm, add this logic to a security initializer.

// In Initialize
var seeder = SecuritySeeder.Null;
SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, seeder, this));


class MySecurityInitializer : BrokerageModelSecurityInitializer
{
    private QCAlgorithm _algorithm;

    public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder, QCAlgorithm algorithm)
        : base(brokerageModel, securitySeeder) 
    {
        _algorithm = algorithm;
    }
    
    public override void Initialize(Security security)
    {
        // First, call the superclass definition
        // This method sets the reality models of each security using the default reality models of the brokerage model
        base.Initialize(security);

        // Next, set and warm up the volatility model
        if (security.Type == SecurityType.Index)
        {
            security.VolatilityModel = new StandardDeviationOfReturnsVolatilityModel(30);
            foreach (var tradeBar in _algorithm.History(security.Symbol, 30, Resolution.Daily))
            {
                security.VolatilityModel.Update(security, tradeBar);
            }
        }
    }
}
# In Initialize
seeder = SecuritySeeder.Null
self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, seeder, self))


class MySecurityInitializer(BrokerageModelSecurityInitializer):

    def __init__(self, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder, algorithm: QCAlgorithm) -> None:
        super().__init__(brokerage_model, security_seeder)
        self.algorithm = algorithm

    def Initialize(self, security: Security) -> None:
        # First, call the superclass definition
        # This method sets the reality models of each security using the default reality models of the brokerage model
        super().Initialize(security)

        # Next, set and warm up the volatility model
        if security.Type == SecurityType.Index:
            security.VolatilityModel = StandardDeviationOfReturnsVolatilityModel(30)
            trade_bars = self.algorithm.History[TradeBar](security.Symbol, 30, Resolution.Daily)
            for trade_bar in trade_bars:
                security.VolatilityModel.Update(security, trade_bar)

Get Contract Symbols

To get Index Option contract Symbol objects, call the CreateOption method or use the OptionChainProvider. If you use the CreateOption method, you need to know the specific contract details.

_symbol = QuantConnect.Symbol.Create("SPX", SecurityType.Index, Market.USA);
_contractSymbol = QuantConnect.Symbol.CreateOption(_symbol, Market.USA,
    OptionStyle.European, OptionRight.Call, 3650, new DateTime(2022, 6, 17));
self.symbol = Symbol.Create("SPX", SecurityType.Index, Market.USA)
self.contract_symbol = Symbol.CreateOption(self.symbol, Market.USA,
    OptionStyle.European, OptionRight.Call, 3650, datetime(2022, 6, 17))

Another way to get an Index Option contract Symbol is to use the OptionChainProvider. The GetOptionContractList method of OptionChainProvider returns a list of Symbol objects that reference the available Option contracts for a given underlying Index on a given date.

var contractSymbols = OptionChainProvider.GetOptionContractList(_symbol, Time);
var expiry = contractSymbols.Select(symbol => symbol.ID.Date).Min();
var filteredSymbols = contractSymbols.Where(symbol => symbol.ID.Date == expiry && symbol.ID.OptionRight == OptionRight.Call);
_contractSymbol = filteredSymbols.OrderByDescending(symbol => symbol.ID.StrikePrice).Last();
contract_symbols = self.OptionChainProvider.GetOptionContractList(self.symbol, self.Time)
expiry = min([symbol.ID.Date for symbol in contract_symbols])
filtered_symbols = [symbol for symbol in contract_symbols if symbol.ID.Date == expiry and symbol.ID.OptionRight == OptionRight.Call]
self.contract_symbol = sorted(filtered_symbols, key=lambda symbol: symbol.ID.StrikePrice)[0]

The OptionRight enumeration has the following members:

Subscribe to Contracts

To create an Index Option contract subscription, pass the contract Symbol to the AddIndexOptionContract method. Save a reference to the contract Symbol so you can easily access the contract in the OptionChain that LEAN passes to the OnData method. To set the price model of the Option, set its PriceModel property.

var option = AddIndexOptionContract(_contractSymbol);
option.PriceModel = OptionPriceModels.BlackScholes();
option = self.AddIndexOptionContract(self.contract_symbol)
option.PriceModel = OptionPriceModels.BlackScholes()

The AddIndexOptionContract method creates a subscription for a single Index Option contract and adds it to your user-defined universe. To create a dynamic universe of Index Option contracts, add an Index Option universe.

Warm Up Contract Prices

If you subscribe to an Index Option contract with AddIndexOptionContract, you'll need to wait until the next Slice to receive data and trade the contract. To trade the contract in the same time step you subscribe to the contract, set the current price of the contract in a security initializer.

var seeder = new FuncSecuritySeeder(GetLastKnownPrices);
SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, seeder, this));
seeder = FuncSecuritySeeder(self.GetLastKnownPrices)
self.SetSecurityInitializer(MySecurityInitializer(self.BrokerageModel, seeder, self))

Supported Assets

To view the supported assets in the US Index Options dataset, see the Supported Indices.

Resolutions

The following table shows the available resolutions and data formats for Index Option contract subscriptions:

ResolutionTradeBarQuoteBarTrade TickQuote Tick
Tick

Second

Minutegreen checkgreen check
Hourgreen checkgreen check
Dailygreen checkgreen check

The default resolution for Index Option subscriptions is Resolution.Minute. To change the resolution, pass a resolution argument to the AddIndexOptionContract method.

AddIndexOptionContract(_contractSymbol, Resolution.Hour);
self.AddIndexOptionContract(self.contract_symbol, Resolution.Hour)

To create custom resolution periods, see Consolidating Data.

Supported Markets

The following Market enumeration members are available for Index Options:

You don't need to pass a Market argument to the AddIndexOptionContract method because the contract Symbol already contains the market.

Margin and Leverage

LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. Options are already leveraged products, so you can't change their leverage.

Remove Subscriptions

To remove a contract subscription that you created with AddIndexOptionContract, call the RemoveOptionContract method. This method is an alias for RemoveSecurity.

RemoveOptionContract(_contractSymbol);
self.RemoveOptionContract(self.contract_symbol)

The RemoveOptionContract method cancels your open orders for the contract and liquidates your holdings.

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: