Equity Options

Requesting Data

Introduction

Request Equity 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 Equity Options dataset listing. To trade Equity Options live, you can use one of the brokerage data providers. We currently only support American-style Options for US Equity Options.

Create Subscriptions

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

Configure the Underlying Equity

If you want to subscribe to the underlying Equity in the Initialize method, set the Equity data normalization to DataNormalizationMode.Raw.

_symbol = AddEquity("SPY", dataNormalizationMode: DataNormalizationMode.Raw).Symbol;
self.symbol = self.AddEquity("SPY", dataNormalizationMode=DataNormalizationMode.Raw).Symbol

If your algorithm has a dynamic universe of Equities, before you add the Equity universe in the Initialize method, set the universe data normalization mode to DataNormalizationMode.Raw.

UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw;
self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw

If you subscribe to an Equity Option contract but don't have a subscription to the underlying Equity, LEAN automatically subscribes to the underlying Equity with the following settings:

SettingValue
Fill forwardSame as the Option contract
Leverage0
Extended Market HoursSame as the Option contract
Data NormalizationDataNormalizationMode.Raw

In this case, you still need the Equity Symbol to subscribe to Equity Option contracts. If you don't have access to it, create it.

_symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
self.symbol = Symbol.Create("SPY", SecurityType.Equity, Market.USA)

To override the initial guess of implied volatility, set and warm up the underlying volatility model.

Get Contract Symbols

To subscribe to an Option contract, you need the contract Symbol. You can get the contract Symbol from the CreateOption method or from the OptionChainProvider. If you use the CreateOption method, you need to provide the details of an existing contract.

_contractSymbol = QuantConnect.Symbol.CreateOption(_symbol, Market.USA,
    OptionStyle.American, OptionRight.Call, 365, new DateTime(2022, 6, 17));
self.contract_symbol = Symbol.CreateOption(self.symbol, Market.USA,
    OptionStyle.American, OptionRight.Call, 365, datetime(2022, 6, 17))

Another way to get an 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 Equity on a given date. To filter and select contracts, you can use the following properties of each Symbol object:

PropertyDescription
ID.DateThe expiration date of the contract.
ID.StrikePriceThe strike price of the contract.
ID.OptionRight The contract type. The OptionRight enumeration has the following members:
ID.OptionStyle The contract style. The OptionStyle enumeration has the following members:
We currently only support American-style Options for US Equity Options.
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]

Subscribe to Contracts

To create an Equity Option contract subscription, pass the contract Symbol to the AddOptionContract method. Save a reference to the contract Symbol so you can easily access the Option contract in the OptionChain that LEAN passes to the OnData method. This method returns an Option object. To override the default pricing model of the Option, set a pricing model.

var option = AddOptionContract(_contractSymbol);
option.PriceModel = OptionPriceModels.BinomialCoxRossRubinstein();
option = self.AddOptionContract(self.contract_symbol)
option.PriceModel = OptionPriceModels.BinomialCoxRossRubinstein()

The AddOptionContract method creates a subscription for a single Option contract and adds it to your user-defined universe. To create a dynamic universe of Option contracts, add an Equity Options universe or an Options Universe Selection model.

Warm Up Contract Prices

If you subscribe to an Option contract with AddOptionContract, 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 BrokerageModelSecurityInitializer(BrokerageModel, seeder));
seeder = FuncSecuritySeeder(self.GetLastKnownPrices)
self.SetSecurityInitializer(BrokerageModelSecurityInitializer(self.BrokerageModel, seeder))

Supported Assets

To view the supported assets in the US Equities dataset, see the Data Explorer.

Resolutions

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

ResolutionTradeBarQuoteBarTrade TickQuote Tick
Tick

Second

Minutegreen checkgreen check
Hourgreen checkgreen check
Dailygreen checkgreen check

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

AddOptionContract(_contractSymbol, Resolution.Minute);
self.AddOptionContract(self.contract_symbol, Resolution.Minute)

To create custom resolution periods, see Consolidating Data.

Supported Markets

LEAN groups all of the US Equity exchanges under Market.USA. You don't need to pass a Market argument to the AddOptionContract method because the contract Symbol already contains the market.

Fill Forward

Fill forward means if there is no data point for the current slice, LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get stale fills or you may see trade volume as zero.

To disable fill forward for a security, set the fillForward argument to false when you create the security subscription.

AddOptionContract(_contractSymbol, fillForward: false);
self.AddOptionContract(self.contract_symbol, fillForward=False)

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.

Extended Market Hours

By default, your security subscriptions only cover regular trading hours. To subscribe to pre and post-market trading hours for a specific asset, enable the extendedMarketHours argument when you create the security subscription.

AddOptionContract(_contractSymbol, extendedMarketHours: true);
self.AddOptionContract(self.contract_symbol, extendedMarketHours=True)

You only receive extended market hours data if you create the subscription with minute, second, or tick resolution. If you create the subscription with daily or hourly resolution, the bars only reflect the regular trading hours.

To view the schedule of regular and extended market hours, see Market Hours.

Data Normalization

The data normalization mode doesn't affect the data that LEAN passes to OnData or the data from history request. By default, LEAN doesn't adjust Equity Options data for splits and dividends of their underlying. If you change the data normalization mode, it won't change the outcome.

If you hold an Option contract when a corporate action occurs for the underlying Equity, LEAN automatically closes your position.

Remove Subscriptions

To remove a contract subscription that you created with AddOptionContract, 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.

Properties

The AddOptionContract method returns an Option object, which have the following properties:

Helper Methods

The Option object provides methods you can use for basic calculations. These methods require the underlying price. To get the Option object and the Security object for its underlying in any function, use the Option Symbol to access the value in the Securities object.

var option = Securities[_contractSymbol];
var underlying = Securities[_contractSymbol.Underlying];
var underlyingPrice = underlying.Price;
option = self.Securities[self.contract_symbol]
underlying = self.Securities[self.contract_symbol.Underlying]
underlying_price = underlying.Price

To get the Option payoff, call the GetPayOff method.

var payOff = option.GetPayOff(underlyingPrice);
pay_off = option.GetPayOff(underlying_price)

To get the Option intrinsic value, call the GetIntrinsicValue method.

var intrinsicValue = option.GetIntrinsicValue(underlyingPrice);
intrinsic_value = option.GetIntrinsicValue(underlying_price)

To get the Option out-of-the-money amount, call the OutOfTheMoneyAmount method.

var otmAmount = option.OutOfTheMoneyAmount(underlyingPrice);
otm_amount = option.OutOfTheMoneyAmount(underlying_price)

To check whether the Option can be automatic exercised, call the IsAutoExercised method.

var isAutoExercised = option.IsAutoExercised(underlyingPrice);
is_auto_exercised = option.IsAutoExercised(underlying_price)

Example

The following example shows how to update the Option chain every five minutes. The OptionChainManager class implements the selection logic and manages the contract subscriptions.

namespace QuantConnect.Algorithm.CSharp
{
    public class OptionChainProviderFullExample : QCAlgorithm
    {
        private Dictionary<Symbol, OptionChainManager> _chainManager = new();
        public override void Initialize()
        {
            SetStartDate(2023, 1, 2);
            SetEndDate(2023, 1, 30);
            SetCash(100000);
            UniverseSettings.Asynchronous = true;
            UniverseSettings.MinimumTimeInUniverse = TimeSpan.Zero;
            SetSecurityInitializer(new BrokerageModelSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));
            var spy = AddEquity("SPY", dataNormalizationMode: DataNormalizationMode.Raw).Symbol;
            _chainManager[QuantConnect.Symbol.CreateCanonicalOption(spy)] = new(-10, 10, 0, 7);
            PopulateOptionChain();
            Schedule.On(DateRules.EveryDay(spy), TimeRules.AfterMarketOpen(spy, 1), PopulateOptionChain);
            Schedule.On(DateRules.EveryDay(spy), TimeRules.Every(TimeSpan.FromMinutes(5)), Filter);
        }
        
        private void PopulateOptionChain()
        {
            // The contract list is updated daily, so we can get it and apply
            // the expiration filter as soon as the market open
            foreach (var (symbol, manager) in _chainManager)
            {
                manager.SetChain(OptionChainProvider.GetOptionContractList(symbol, Time), Time);
            }
    
            Filter();
        }
        
        private void Filter()
        {
            foreach (var (symbol, manager) in _chainManager)
            {
                manager.Select(this, symbol);
            }
        }
        
        public override void OnData(Slice slice)
        {
            foreach (var (symbol, manager) in _chainManager)
            {
                if (!slice.OptionChains.TryGetValue(symbol, out var chain))
                    continue;
                var expiry = chain.Min(x => x.Expiry);
                var atmCall = chain
                    .Where(x => x.Expiry == expiry && x.Right == OptionRight.Call && Securities[x.Symbol].IsTradable)
                    .OrderBy(x => Math.Abs(chain.Underlying.Price - x.Strike))
                    .FirstOrDefault();

                if (atmCall != null && !Portfolio[atmCall.Symbol].Invested)
                    MarketOrder(atmCall.Symbol, 1);
            }
        }
    }

    internal class OptionChainManager
    {
        private readonly int _minStrike;
        private readonly int _maxStrike;
        private readonly int _minExpiry;
        private readonly int _maxExpiry;
        private List<Symbol> _chain = new();
        private readonly List<Symbol> _symbols = new();

        public OptionChainManager(int minStrike, int maxStrike, int minExpiry, int maxExpiry)
        {
            _minStrike = minStrike;
            _maxStrike = maxStrike;
            _minExpiry = minExpiry;
            _maxExpiry = maxExpiry;
        }

        public void SetChain(IEnumerable<Symbol> symbols, DateTime time)
        {
            _chain = symbols.Where(x =>
            {
                var totalDays = (x.ID.Date - time).TotalDays;
                return _minExpiry <= totalDays && totalDays <= _maxExpiry;
            }).ToList();
        }
        
        public void Select(QCAlgorithm algorithm, Symbol underlyingSymbol)
        {
            if (_chain.IsNullOrEmpty())
                return;
            if (underlyingSymbol.IsCanonical())
                underlyingSymbol = underlyingSymbol.Underlying;

            var strikes = _chain.Select(x => x.ID.StrikePrice).OrderBy(x => x).Distinct().ToList();
            var spot = algorithm.Securities[underlyingSymbol].Price;
            var atm = strikes.OrderBy(x => Math.Abs(spot - x)).FirstOrDefault();
            var index = strikes.IndexOf(atm);
            var minStrike = strikes[Math.Max(0, index + _minStrike)];
            var maxStrike = strikes[Math.Min(strikes.Count - 1, index + _maxStrike)];
            var symbols = _chain.Where(x => minStrike <= x.ID.StrikePrice && x.ID.StrikePrice <= maxStrike).ToList();

            var toRemove = _symbols.Except(symbols).ToList();
            foreach (var symbol in toRemove)
            {
                if (algorithm.RemoveOptionContract(symbol))
                    _symbols.Remove(symbol);
            }
            var toAdd = symbols.Except(_symbols).ToList();
            foreach (var symbol in toAdd)
            {
                _symbols.Add(symbol);
                algorithm.AddOptionContract(symbol);
            }
        }
    }
}
class OptionChainProviderFullExample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2023, 1, 2)
        self.SetEndDate(2023, 1, 30)
        self.SetCash(100000)
        self.UniverseSettings.Asynchronous = True
        self.UniverseSettings.MinimumTimeInUniverse = timedelta(minutes=0)
        self.SetSecurityInitializer(BrokerageModelSecurityInitializer(self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices)))
        spy = self.AddEquity("SPY", dataNormalizationMode=DataNormalizationMode.Raw).Symbol
        self.chain_manager = {
            Symbol.CreateCanonicalOption(spy): OptionChainManager(-10, 10, 0, 7)
        }
        self.PopulateOptionChain()
        self.Schedule.On(self.DateRules.EveryDay(spy), self.TimeRules.AfterMarketOpen(spy, 1), self.PopulateOptionChain)
        self.Schedule.On(self.DateRules.EveryDay(spy), self.TimeRules.Every(timedelta(minutes=5)), self.Filter)

    def PopulateOptionChain(self):
        # The contract list is updated daily, so we can get it and apply
        # the expiration filter as soon as the market open
        for symbol, manager in self.chain_manager.items():
            manager.SetChain(self.OptionChainProvider.GetOptionContractList(symbol, self.Time), self.Time)
        self.Filter()

    def Filter(self):
        for symbol, manager in self.chain_manager.items():
            manager.Select(self, symbol)

    def OnData(self, slice: Slice) -> None:
        for symbol, _ in self.chain_manager.items():
            chain = slice.OptionChains.get(symbol)
            if not chain: continue
            if self.Portfolio[symbol.Underlying].Invested:
                self.Liquidate(symbol.Underlying)

            expiry = min([x.Expiry for x in chain])
            contracts = [x for x in chain if x.Expiry == expiry and x.Right == OptionRight.Call and self.Securities[x.Symbol].IsTradable]
            if not contracts: continue
            atm_call = sorted(contracts, key=lambda x: abs(chain.Underlying.Price-x.Strike))[0]

            if not self.Portfolio[atm_call.Symbol].Invested:
                self.MarketOrder(atm_call.Symbol, 1)

class OptionChainManager:
    chain = []
    symbols = []
    def __init__(self, min_strike, max_strike, min_expiry, max_expiry):
        self.min_strike = min_strike
        self.max_strike = max_strike
        self.min_expiry = min_expiry
        self.max_expiry = max_expiry
    def SetChain(self, symbols: List[Symbol], time: datetime) -> None:
        self.chain = [x for x in symbols if self.min_expiry <= (x.ID.Date - time).days <= self.max_expiry]
    def Select(self, algorithm: QCAlgorithm, symbol: Symbol) -> None:
        if not self.chain:
            return
        if symbol.IsCanonical():
            symbol = symbol.Underlying
        strikes = sorted(set(x.ID.StrikePrice for x in self.chain))
        spot = algorithm.Securities[symbol].Price
        atm = sorted(strikes, key=lambda x: abs(spot-x))[0]
        index = strikes.index(atm)
        min_strike = strikes[max(0, index + self.min_strike)]
        max_strike = strikes[min(len(strikes) - 1, index + self.max_strike)]
        symbols = set(x for x in self.chain if min_strike <= x.ID.StrikePrice <= max_strike)
        to_remove = set(self.symbols).difference(symbols)
        for symbol in to_remove:
            if algorithm.RemoveOptionContract(symbol):
                self.symbols.remove(symbol)
        to_add = symbols.difference(self.symbols)
        for symbol in to_add:
            self.symbols.append(symbol)
            algorithm.AddOptionContract(symbol)

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: