Greeks and Implied Volatility

Indicators

Introduction

Option indicators enable you to calculate daily and intraday values for the Greeks and implied volatility of Option contracts. This page explains the most popular Option indicators and how to use them with LEAN.

Parameters

The following table describes the arguments that the automatic Option indicators methods accept:

ArgumentData TypeDescriptionDefault Value
symbolSymbolThe contract to use when calculating the indicator values.
mirrorOptionmirror_optionSymbolThe mirror contract to use in parity type calculations.nullNone
riskFreeRaterisk_free_ratedecimalfloat The risk-free interest rate. If you don't provide a value, the default value is the US primary credit rate from the Interest Rate Provider Model. nullNone
dividendYielddividend_yielddecimalfloat The dividend yield rate. If you don't provide a value, the default value comes from the Dividend Yield Provider Model. nullNone
optionModeloption_modelOptionPricingModelType The Option pricing model that's used to calculate the Greeks. If you don't provide a value, the default value is OptionPricingModelType.BlackScholes for Index Options. nullNone
resolutionResolution The resolution of the indicator data. If you don't provide a value, the default value is the resolution of the subscription you have for the Option contract(s). nullNone

To perform implied volatility (IV) smoothing with a put-call pair, pass one of the contracts as the symbol argument and pass the other contract in the pair as the mirrorOptionmirror_option argument. The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatility class/property.

Several different Option pricing models are supported to calculate the IV and Greeks. The following table describes the OptionPricingModelType enumeration members:

Implied Volatility

Implied volatility, , is the market's expectation for the future volatility of an asset and is implied by the price of the assets's Options contracts. You can't observe it in the market but you can derive it from the price of an Option. For more information about implied volatility, see Implied Volatility.

Automatic Indicators

To create an automatic indicator for implied volatility, call the QCAlgorithm.IVQCAlgorithm.iv method with the Option contract Symbolsymbol object(s).

public class AutomaticImpliedVolatilityIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<ImpliedVolatility> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic ImpliedVolatility indicators.
                _indicators.Add(IV(contracts[0], contracts[1]));
                _indicators.Add(IV(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the ImpliedVolatility indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticImpliedVolatilityIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic ImpliedVolatility indicators.
            self._indicators.extend([self.iv(call, put), self.iv(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the ImpliedVolatility indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

For more information about the IViv method, see Parameters and Using IV Indicator.

Manual Indicators

To create a manual indicator for implied volatility, call the ImpliedVolatility constructor.

public class ManualImpliedVolatilityIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<ImpliedVolatility> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual ImpliedVolatility indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new ImpliedVolatility(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the ImpliedVolatility indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualImpliedVolatilityIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic ImpliedVolatility indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    ImpliedVolatility(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the ImpliedVolatility indicator.
                value = indicator.current.value

For more information about the ImpliedVolatility constructor, see Using IV Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the IViv method or ImpliedVolatility constructor and then call the SetSmoothingFunctionset_smoothing_function method of the resulting ImpliedVolatility object. The follow table describes the arguments of the custom function:

ArgumentData TypeDescription
ivdecimalfloatThe IV of the Option contract.
mirrorIvmirror_ivdecimalfloatThe IV of the mirror Option contract.

The method must return a decimalfloat as the smoothened IV.

private ImpliedVolatility _iv;

public override void Initialize()
{
    var option = QuantConnect.Symbol.CreateOption("AAPL", Market.USA, OptionStyle.American, OptionRight.Put, 505m, new DateTime(2014, 6, 27));
    AddOptionContract(option);

    var mirrorOption = QuantConnect.Symbol.CreateOption("AAPL", Market.USA, OptionStyle.American, OptionRight.Call, 505m, new DateTime(2014, 6, 27));
    AddOptionContract(mirrorOption);

    _iv = IV(option, mirrorOption);
    // example: take average of the call-put pair
    _iv.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
}
def initialize(self):
    option = Symbol.create_option("AAPL", Market.USA, OptionStyle.AMERICAN, OptionRight.PUT, 505, datetime(2014, 6, 27))
    self.add_option_contract(option)

    mirror_option = Symbol.create_option("AAPL", Market.USA, OptionStyle.AMERICAN, OptionRight.CALL, 505, datetime(2014, 6, 27))
    self.add_option_contract(mirror_option)

    self._iv = self.iv(option, mirror_option)
    # Example: The average of the call-put pair.
    self._iv.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

Delta

Delta, , is the rate of change of the Option price with respect to the price of the underlying asset. It measures the first-order sensitivity of the price to a movement in underlying price. For example, an Option delta of 0.4 means that if the underlying asset moves by 1%, then the value of the Option moves by 0.4 × 1% = 0.4%. For more information about delta, see Delta.

Automatic Indicators

To create an automatic indicator for delta, call the QCAlgorithm.DQCAlgorithm.d method with the Option contract Symbolsymbol object(s).

public class AutomaticDeltaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<Delta> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic Delta indicators.
                _indicators.Add(D(contracts[0], contracts[1]));
                _indicators.Add(D(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the Delta indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticDeltaIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Delta indicators.
            self._indicators.extend([self.d(call, put), self.d(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the Delta indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

The follow table describes the arguments that the Dd method accepts in addition to the standard parameters:

ArgumentData TypeDescriptionDefault Value
ivModeliv_modelOptionPricingModelType The Option pricing model to use to estimate the IV when calculating Delta. If you don't provide a value, the default value is to match the optionModeloption_model parameter. nullNone

For more information about the Dd method, see Using D Indicator.

Manual Indicators

To create a manual indicator for delta, call the Delta constructor.

public class ManualDeltaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<Delta> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual Delta indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new Delta(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the Delta indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualDeltaIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Delta indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    Delta(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the Delta indicator.
                value = indicator.current.value

For more information about the Delta constructor, see Using D Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the indicator method or constructor and then call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatilityimplied_volatility property of the indicator.

// Example: Average IV of the call-put pair.
deltaIndicator.ImpliedVolatility.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
# Example: Average IV of the call-put pair.
delta_indicator.implied_volatility.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

Gamma

Gamma, , is the rate of change of the portfolio's delta with respect to the underlying asset's price. It represents the second-order sensitivity of the Option to a movement in the underlying asset's price. For more information about Gamma, see Gamma.

Automatic Indicators

To create an automatic indicator for gamma, call the QCAlgorithm.GQCAlgorithm.g method with the Option contract Symbolsymbol object(s).

public class AutomaticGammaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<Gamma> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic Gamma indicators.
                _indicators.Add(G(contracts[0], contracts[1]));
                _indicators.Add(G(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the Gamma indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticGammaIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Gamma indicators.
            self._indicators.extend([self.g(call, put), self.g(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the Gamma indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

The follow table describes the arguments that the Gg method accepts in addition to the standard parameters:

ArgumentData TypeDescriptionDefault Value
ivModeliv_modelOptionPricingModelType The Option pricing model to use to estimate the IV when calculating Gamma. If you don't provide a value, the default value is to match the optionModeloption_model parameter. nullNone

For more information about the Gg method, see Using G Indicator.

Manual Indicators

To create a manual indicator for gamma, call the Gamma constructor.

public class ManualGammaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<Gamma> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual Gamma indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new Gamma(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the Gamma indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualGammaIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Gamma indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    Gamma(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the Gamma indicator.
                value = indicator.current.value

For more information about the Gamma constructor, see Using G Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the indicator method or constructor and then call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatilityimplied_volatility property of the indicator.

// Example: Average IV of the call-put pair.
gammaIndicator.ImpliedVolatility.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
# Example: Average IV of the call-put pair.
gamma_indicator.implied_volatility.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

Vega

Vega, , is the rate of change in the value of the Option with respect to the volatility of the underlying asset. For more information about vega, see Vega.

Automatic Indicators

To create an automatic indicator for vega, call the QCAlgorithm.VQCAlgorithm.v method with the Option contract Symbolsymbol object(s).

public class AutomaticVegaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<Vega> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic Vega indicators.
                _indicators.Add(V(contracts[0], contracts[1]));
                _indicators.Add(V(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the Vega indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticVegaIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Vega indicators.
            self._indicators.extend([self.v(call, put), self.v(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the Vega indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

The follow table describes the arguments that the Vv method accepts in addition to the standard parameters:

ArgumentData TypeDescriptionDefault Value
ivModeliv_modelOptionPricingModelType The Option pricing model to use to estimate the IV when calculating Vega. If you don't provide a value, the default value is to match the optionModeloption_model parameter. nullNone

For more information about the Vv method, see Using V Indicator.

Manual Indicators

To create a manual indicator for vega, call the Vega constructor.

public class ManualVegaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<Vega> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual Vega indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new Vega(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the Vega indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualVegaIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Vega indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    Vega(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the Vega indicator.
                value = indicator.current.value

For more information about the Vega constructor, see Using V Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the indicator method or constructor and then call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatilityimplied_volatility property of the indicator.

// Example: Average IV of the call-put pair.
vegaIndicator.ImpliedVolatility.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
# Example: Average IV of the call-put pair.
vega_indicator.implied_volatility.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

Theta

Theta, , is the rate of change of the value of the Option with respect to the passage of time. It is also known to as the time decay of an Option. For more information about theta, see Theta.

Automatic Indicators

To create an automatic indicator for theta, call the QCAlgorithm.TQCAlgorithm.t method with the Option contract Symbolsymbol object(s).

public class AutomaticThetaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<Theta> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic Theta indicators.
                _indicators.Add(T(contracts[0], contracts[1]));
                _indicators.Add(T(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the Theta indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticThetaIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Theta indicators.
            self._indicators.extend([self.t(call, put), self.t(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the Theta indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

The follow table describes the arguments that the Tt method accepts in addition to the standard parameters:

ArgumentData TypeDescriptionDefault Value
ivModeliv_modelOptionPricingModelType The Option pricing model to use to estimate the IV when calculating theta. If you don't provide a value, the default value is to match the optionModeloption_model parameter. nullNone

For more information about the Tt method, see Using T Indicator.

Manual Indicators

To create a manual indicator for theta, call the Theta constructor.

public class ManualThetaIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<Theta> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual Theta indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new Theta(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the Theta indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualThetaIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Theta indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    Theta(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the Theta indicator.
                value = indicator.current.value

For more information about the Theta constructor, see Using T Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the indicator method or constructor and then call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatilityimplied_volatility property of the indicator.

// Example: Average IV of the call-put pair.
thetaIndicator.ImpliedVolatility.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
# Example: Average IV of the call-put pair.
theta_indicator.implied_volatility.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

Rho

Rho, , is the rate of change of the value of a derivative with respect to the interest rate.  It is usually small and not a big issue in practice unless the Option is deep in-the-money and has a long horizon. In this case, the interest rate matters because you need to discount a larger cash flow over a longer horizon. For more information about rho, see Rho.

Automatic Indicators

To create an automatic indicator for rho, call the QCAlgorithm.RQCAlgorithm.r method with the Option contract Symbolsymbol object(s).

public class AutomaticRhoIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private List<Rho> _indicators = new();

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;
    
        // Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the automatic Rho indicators.
                _indicators.Add(R(contracts[0], contracts[1]));
                _indicators.Add(R(contracts[1], contracts[0]));
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Get the Rho indicator of each contract.
        foreach (var indicator in _indicators)
        {
            var symbol = indicator.OptionSymbol;
            var value = indicator.Current.Value;
        }
    }
}
class AutomaticRhoIndicatorAlgorithm(QCAlgorithm):
    
    _indicators = []
    
    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol

        # Set up a Scheduled Event to select contracts and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )

    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return

        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Rho indicators.
            self._indicators.extend([self.r(call, put), self.r(put, call)])
        
    def on_data(self, slice: Slice) -> None:
        # Get the Rho indicator of each contract.
        for indicator in self._indicators:
            symbol = indicator.option_symbol
            value = indicator.current.value

The follow table describes the arguments that the Rr method accepts in addition to the standard parameters:

ArgumentData TypeDescriptionDefault Value
ivModeliv_modelOptionPricingModelType The Option pricing model to use to estimate the IV when calculating rho If you don't provide a value, the default value is to match the optionModeloption_model parameter. nullNone

For more information about the Rr method, see Using R Indicator.

Manual Indicators

To create a manual indicator for rho, call the Rho constructor.

public class ManualRhoIndicatorAlgorithm : QCAlgorithm
{
    private Symbol _underlying;
    private DividendYieldProvider _dividendYieldProvider;
    // Create a list to store the indicators.
    private List<Rho> _indicators = new();
    // Define the Option pricing model.
    private readonly OptionPricingModelType _optionPricingModel = OptionPricingModelType.ForwardTree;

    public override void Initialize()
    {
        SetStartDate(2024, 1, 1);
        // Subscribe to the underlying asset.
        _underlying = AddIndex("SPX").Symbol;

        // Set up dividend yield provider for the underlying
        _dividendYieldProvider = new(_underlying);
        
        // Set up a Scheduled Event to select contract and create the indicators every day before market open.
        Schedule.On(
            DateRules.EveryDay(_underlying),
            TimeRules.At(9, 0),
            UpdateContractsAndGreeks
        );
    }

    private void UpdateContractsAndGreeks()
    {
        // Remove indicators on expired contracts.
        _indicators = _indicators.Where(indicator => indicator.OptionSymbol.ID.Date > Time).ToList();
        
        // Get all the tradable Option contracts.
        var chain = OptionChain(_underlying);
        
        // Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        var filteredChain = chain.Where(contract => contract.Expiry > Time.AddDays(30));
        if (filteredChain.Count() == 0)
        {
            return;
        }
        var expiry = filteredChain.Min(contract => contract.Expiry);
        filteredChain = filteredChain
            .Where(contract => contract.Expiry == expiry)
            .OrderBy(contract => Math.Abs(contract.Strike - contract.UnderlyingLastPrice))
            .Take(4);

        // Group the contracts into call/put pairs.
        foreach (var group in filteredChain.GroupBy(contract => contract.Strike))
        {
            var contracts = group.ToList();
            if (contracts.Count > 1)
            {
                // Subscribe to both contracts.
                AddIndexOptionContract(contracts[0]);
                AddIndexOptionContract(contracts[1]);

                // Create and save the manual Rho indicators.
                foreach (var (contractA, contractB) in new[] { (contracts[0], contracts[1]), (contracts[1], contracts[0]) })
                {
                    _indicators.Add(
                        new Rho(
                            contractA.Symbol, RiskFreeInterestRateModel, _dividendYieldProvider, 
                            contractB.Symbol, _optionPricingModel
                        )
                    );
                }
            }
        }
    }

    public override void OnData(Slice slice)
    {
        // Iterate through the indicators.
        foreach (var indicator in _indicators)
        {
            var option = indicator.OptionSymbol;
            var mirror = QuantConnect.Symbol.CreateOption(
                option.Underlying.Value, option.ID.Market, option.ID.OptionStyle, 
                option.ID.OptionRight == OptionRight.Call ? OptionRight.Put : OptionRight.Call, 
                option.ID.StrikePrice, option.ID.Date
            ).Value;
            
            // Check if price data is available for both contracts and the underlying asset.
            var q = slice.QuoteBars;
            var b = slice.Bars;
            if (q.ContainsKey(option) && q.ContainsKey(mirror) && b.ContainsKey(option.Underlying))
            {
                var dataPoints = new List<IndicatorDataPoint>
                {
                    new IndicatorDataPoint(option, q[option].EndTime, q[option].Close),
                    new IndicatorDataPoint(mirror, q[mirror].EndTime, q[mirror].Close),
                    new IndicatorDataPoint(
                        option.Underlying, b[option.Underlying].EndTime, b[option.Underlying].Close
                    )
                };
                foreach (var dataPoint in dataPoints)
                {
                    indicator.Update(dataPoint);
                }

                // Get the current value of the Rho indicator.
                var value = indicator.Current.Value;
            }
        }
    }
}
class ManualRhoIndicatorAlgorithm(QCAlgorithm):

    _indicators = []

    def initialize(self) -> None:
        self.set_start_date(2024, 1, 1)
        # Subscribe to the underlying asset.
        self._underlying = self.add_index('SPX').symbol
        # Set up the dividend yield provider for the underlying.
        self._dividend_yield_provider = DividendYieldProvider(self._underlying)
        # Define the Option pricing model.
        self._option_pricing_model = OptionPricingModelType.FORWARD_TREE

        # Set up a Scheduled Event to select contract and create the indicators every day before market open.
        self.schedule.on(
            self.date_rules.every_day(self._underlying),
            self.time_rules.at(9, 0),
            self._update_contracts_and_greeks
        )
        
    def _update_contracts_and_greeks(self) -> None:
        # Remove indicators on expired contracts.
        self._indicators = [indicator for indicator in self._indicators if indicator.option_symbol.id.date > self.time]
        
        # Get all the tradable Option contracts.
        chain = self.option_chain(self._underlying).data_frame
        if chain.empty:
            return
        
        # Filter the contracts down. For example, ATM contracts with atleast 1 month until expiry.
        chain = chain[chain.expiry > self.time + timedelta(30)]
        expiry = chain.expiry.min()
        chain = chain[chain.expiry == expiry]
        chain.loc[:, 'abs_strike_delta'] = abs(chain['strike'] - chain['underlyinglastprice'])
        abs_strike_delta = chain['abs_strike_delta'].min()
        chain = chain[chain['abs_strike_delta'] == abs_strike_delta]

        # Group the contracts into call/put pairs.
        contracts_pair_sizes = chain.groupby(['expiry', 'strike']).count()['right']
        paired_contracts = contracts_pair_sizes[contracts_pair_sizes == 2].index
        expiries = [x[0] for x in paired_contracts]
        strikes = [x[1] for x in paired_contracts]
        symbols = [
            idx[-1] for idx in chain[
                chain['expiry'].isin(expiries) & chain['strike'].isin(strikes)
            ].reset_index().groupby(['expiry', 'strike', 'right', 'symbol']).first().index
        ]
        pairs = [(symbols[i], symbols[i+1]) for i in range(0, len(symbols), 2)]

        for call, put in pairs:
            # Subscribe to both contracts.
            self.add_index_option_contract(call)
            self.add_index_option_contract(put)
            
            # Create and save the automatic Rho indicators.
            for contract_a, contract_b in [(call, put), (put, call)]:
                self._indicators.append(
                    Rho(
                        contract_a, self.risk_free_interest_rate_model, 
                        self._dividend_yield_provider, contract_b, self._option_pricing_model
                    ) 
                )

    def on_data(self, slice: Slice) -> None:
        # Iterate through the indicators.
        for indicator in self._indicators:
            option = indicator.option_symbol
            mirror = Symbol.create_option(
                option.underlying.value, option.id.market, option.id.option_style, 
                OptionRight.Call if option.id.option_right == OptionRight.PUT else OptionRight.PUT,
                option.id.strike_price, option.id.date
            )
            # Check if price data is available for both contracts and the underlying asset.
            q = slice.quote_bars
            b = slice.bars
            if option in q and mirror in q and option.underlying in b:
                data_points = [
                    IndicatorDataPoint(option, q[option].end_time, q[option].close),
                    IndicatorDataPoint(mirror, q[mirror].end_time, q[mirror].close),
                    IndicatorDataPoint(
                        option.underlying, b[option.underlying].end_time, b[option.underlying].close
                    )
                ]
                for data_point in data_points:
                    indicator.update(data_point)

                # Get the current value of the Rho indicator.
                value = indicator.current.value

For more information about the Rho constructor, see Using R Indicator.

Volatility Smoothing

The default IV smoothing method uses the one contract in the pair that's at-the-money or out-of-money to calculate the IV. To change the smoothing function, pass a mirrorOptionmirror_option argument to the indicator method or constructor and then call the SetSmoothingFunctionset_smoothing_function method of the ImpliedVolatilityimplied_volatility property of the indicator.

// Example: Average IV of the call-put pair.
rhoIndicator.ImpliedVolatility.SetSmoothingFunction((iv, mirrorIv) => (iv + mirrorIv) * 0.5m);
# Example: Average IV of the call-put pair.
rho_indicator.implied_volatility.set_smoothing_function(lambda iv, mirror_iv: (iv + mirror_iv) * 0.5)

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: