Supported Indicators

Gamma

Introduction

Option Gamma indicator that calculate the gamma of an option

To view the implementation of this indicator, see the LEAN GitHub repository.

Using G Indicator

To create an automatic indicators for Gamma, call the G helper method from the QCAlgorithm class. The G method creates a Gamma object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initialize method.

public class GammaAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private Symbol _option, _mirrorOption;
    private Gamma _g;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _option = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_option, Resolution.Daily);
        _mirrorOption = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_mirrorOption, Resolution.Daily);
        _g = G(_option, _mirrorOption);
    }

    public override void OnData(Slice data)
    {
        if (_g.IsReady)
        {
            // The current value of _g is represented by itself (_g)
            // or _g.Current.Value
            Plot("Gamma", "g", _g);
            // Plot all properties of g
            Plot("Gamma", "impliedvolatility", _g.ImpliedVolatility);
            Plot("Gamma", "riskfreerate", _g.RiskFreeRate);
            Plot("Gamma", "dividendyield", _g.DividendYield);
            Plot("Gamma", "price", _g.Price);
            Plot("Gamma", "oppositeprice", _g.OppositePrice);
            Plot("Gamma", "underlyingprice", _g.UnderlyingPrice);
        }
    }
}
class GammaAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.option, Resolution.Daily)
        self.mirror_option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.mirror_option, Resolution.Daily)
        self.g = self.G(self.option, self.mirror_option)

    def OnData(self, slice: Slice) -> None:
        if self.g.IsReady:
            # The current value of self.g is represented by self.g.Current.Value
            self.Plot("Gamma", "g", self.g.Current.Value)
            # Plot all attributes of self.g
            self.Plot("Gamma", "impliedvolatility", self.g.ImpliedVolatility.Current.Value)
            self.Plot("Gamma", "riskfreerate", self.g.RiskFreeRate.Current.Value)
            self.Plot("Gamma", "dividendyield", self.g.DividendYield.Current.Value)
            self.Plot("Gamma", "price", self.g.Price.Current.Value)
            self.Plot("Gamma", "oppositeprice", self.g.OppositePrice.Current.Value)
            self.Plot("Gamma", "underlyingprice", self.g.UnderlyingPrice.Current.Value)

The following reference table describes the G method:

G()1/1

            Gamma QuantConnect.Algorithm.QCAlgorithm.G (
    Symbol                                   symbol,
    *Symbol                                  mirrorOption,
    *Nullable<Decimal>                 riskFreeRate,
    *Nullable<Decimal>                 dividendYield,
    *OptionPricingModelType                  optionModel,
    *Nullable<OptionPricingModelType>  ivModel,
    *Nullable<Resolution>              resolution
   )
        

Creates a new Gamma indicator for the symbol The indicator will be automatically updated on the symbol's subscription resolution.

If you don't provide a resolution, it defaults to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

For more information about plotting indicators, see Plotting Indicators.

You can manually create a Gamma indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update method with time/number pair or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

public class GammaAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private Symbol _option, _mirrorOption;
    private Gamma _g;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _option = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_option, Resolution.Daily);
        _mirrorOption = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_mirrorOption, Resolution.Daily);
        _g = new Gamma(_option, interest_rate_model, dividend_yield_model, _mirrorOption);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
        {      
            _g.Update(new IndicatorDataPoint(_symbol, bar.EndTime, bar.Close));
        }
        if (data.QuoteBars.TryGetValue(_option, out bar))
        {      
            _g.Update(new IndicatorDataPoint(_option, bar.EndTime, bar.Close));
        }
        if (data.QuoteBars.TryGetValue(_mirrorOption, out bar))
        {      
            _g.Update(new IndicatorDataPoint(_mirrorOption, bar.EndTime, bar.Close));
        }
   
        if (_g.IsReady)
        {
            // The current value of _g is represented by itself (_g)
            // or _g.Current.Value
            Plot("Gamma", "g", _g);
            // Plot all properties of g
            Plot("Gamma", "impliedvolatility", _g.ImpliedVolatility);
            Plot("Gamma", "riskfreerate", _g.RiskFreeRate);
            Plot("Gamma", "dividendyield", _g.DividendYield);
            Plot("Gamma", "price", _g.Price);
            Plot("Gamma", "oppositeprice", _g.OppositePrice);
            Plot("Gamma", "underlyingprice", _g.UnderlyingPrice);
        }
    }
}
class GammaAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.option, Resolution.Daily)
        self.mirror_option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.mirror_option, Resolution.Daily)
        self.g = Gamma(self.option, interest_rate_model, dividend_yield_model, self.mirror_option)

    def OnData(self, slice: Slice) -> None:
        bar = slice.Bars.get(self.symbol)
        if bar:
            self.g.Update(IndicatorDataPoint(self.symbol, bar.EndTime, bar.Close))
        bar = slice.QuoteBars.get(self.option)
        if bar:
            self.g.Update(IndicatorDataPoint(self.option, bar.EndTime, bar.Close))
        bar = slice.QuoteBars.get(self.mirror_option)
        if bar:
            self.g.Update(IndicatorDataPoint(self.mirror_option, bar.EndTime, bar.Close))
        if self.g.IsReady:
            # The current value of self.g is represented by self.g.Current.Value
            self.Plot("Gamma", "g", self.g.Current.Value)
            # Plot all attributes of self.g
            self.Plot("Gamma", "impliedvolatility", self.g.ImpliedVolatility.Current.Value)
            self.Plot("Gamma", "riskfreerate", self.g.RiskFreeRate.Current.Value)
            self.Plot("Gamma", "dividendyield", self.g.DividendYield.Current.Value)
            self.Plot("Gamma", "price", self.g.Price.Current.Value)
            self.Plot("Gamma", "oppositeprice", self.g.OppositePrice.Current.Value)
            self.Plot("Gamma", "underlyingprice", self.g.UnderlyingPrice.Current.Value)

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.

public class GammaAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private Symbol _option, _mirrorOption;
    private Gamma _g;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _option = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_option, Resolution.Daily);
        _mirrorOption = QuantConnect.Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450m, new DateTime(2023, 12, 22));
        AddOptionContract(_mirrorOption, Resolution.Daily);
        _g = new Gamma(_option, interest_rate_model, dividend_yield_model, _mirrorOption);
        RegisterIndicator(_symbol, _g, Resolution.Daily);
        RegisterIndicator(_option, _g, Resolution.Daily);
        RegisterIndicator(_mirrorOption, _g, Resolution.Daily);
    }

    public override void OnData(Slice data)
    {
        if (_g.IsReady)
        {
            // The current value of _g is represented by itself (_g)
            // or _g.Current.Value
            Plot("Gamma", "g", _g);
            // Plot all properties of g
            Plot("Gamma", "impliedvolatility", _g.ImpliedVolatility);
            Plot("Gamma", "riskfreerate", _g.RiskFreeRate);
            Plot("Gamma", "dividendyield", _g.DividendYield);
            Plot("Gamma", "price", _g.Price);
            Plot("Gamma", "oppositeprice", _g.OppositePrice);
            Plot("Gamma", "underlyingprice", _g.UnderlyingPrice);
        }
    }
}
class GammaAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Put, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.option, Resolution.Daily)
        self.mirror_option = Symbol.CreateOption("SPY", Market.USA, OptionStyle.American, OptionRight.Call, 450, datetime(2023, 12, 22))
        self.AddOptionContract(self.mirror_option, Resolution.Daily)
        self.g = Gamma(self.option, interest_rate_model, dividend_yield_model, self.mirror_option)
        self.RegisterIndicator(self.symbol, self.g, Resolution.Daily)
        self.RegisterIndicator(self.option, self.g, Resolution.Daily)
        self.RegisterIndicator(self.mirror_option, self.g, Resolution.Daily)

    def OnData(self, slice: Slice) -> None:
        if self.g.IsReady:
            # The current value of self.g is represented by self.g.Current.Value
            self.Plot("Gamma", "g", self.g.Current.Value)
            # Plot all attributes of self.g
            self.Plot("Gamma", "impliedvolatility", self.g.ImpliedVolatility.Current.Value)
            self.Plot("Gamma", "riskfreerate", self.g.RiskFreeRate.Current.Value)
            self.Plot("Gamma", "dividendyield", self.g.DividendYield.Current.Value)
            self.Plot("Gamma", "price", self.g.Price.Current.Value)
            self.Plot("Gamma", "oppositeprice", self.g.OppositePrice.Current.Value)
            self.Plot("Gamma", "underlyingprice", self.g.UnderlyingPrice.Current.Value)

The following reference table describes the Gamma constructor:

Gamma()1/10

            Gamma QuantConnect.Indicators.Gamma (
    string                      name,
    Symbol                      option,
    IRiskFreeInterestRateModel  riskFreeRateModel,
    IDividendYieldModel         dividendYieldModel,
    *Symbol                     mirrorOption,
    *OptionPricingModelType     optionModel,
    *OptionPricingModelType?    ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()2/10

            Gamma QuantConnect.Indicators.Gamma (
    Symbol                      option,
    IRiskFreeInterestRateModel  riskFreeRateModel,
    IDividendYieldModel         dividendYieldModel,
    *Symbol                     mirrorOption,
    *OptionPricingModelType     optionModel,
    *OptionPricingModelType?    ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()3/10

            Gamma QuantConnect.Indicators.Gamma (
    string                    name,
    Symbol                    option,
    PyObject                  riskFreeRateModel,
    PyObject                  dividendYieldModel,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()4/10

            Gamma QuantConnect.Indicators.Gamma (
    Symbol                    option,
    PyObject                  riskFreeRateModel,
    PyObject                  dividendYieldModel,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()5/10

            Gamma QuantConnect.Indicators.Gamma (
    string                      name,
    Symbol                      option,
    IRiskFreeInterestRateModel  riskFreeRateModel,
    *decimal                    dividendYield,
    *Symbol                     mirrorOption,
    *OptionPricingModelType     optionModel,
    *OptionPricingModelType?    ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()6/10

            Gamma QuantConnect.Indicators.Gamma (
    Symbol                      option,
    IRiskFreeInterestRateModel  riskFreeRateModel,
    *decimal                    dividendYield,
    *Symbol                     mirrorOption,
    *OptionPricingModelType     optionModel,
    *OptionPricingModelType?    ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()7/10

            Gamma QuantConnect.Indicators.Gamma (
    string                    name,
    Symbol                    option,
    PyObject                  riskFreeRateModel,
    *decimal                  dividendYield,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()8/10

            Gamma QuantConnect.Indicators.Gamma (
    Symbol                    option,
    PyObject                  riskFreeRateModel,
    *decimal                  dividendYield,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()9/10

            Gamma QuantConnect.Indicators.Gamma (
    string                    name,
    Symbol                    option,
    *decimal                  riskFreeRate,
    *decimal                  dividendYield,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Gamma()10/10

            Gamma QuantConnect.Indicators.Gamma (
    Symbol                    option,
    *decimal                  riskFreeRate,
    *decimal                  dividendYield,
    *Symbol                   mirrorOption,
    *OptionPricingModelType   optionModel,
    *OptionPricingModelType?  ivModel
   )
        

Initializes a new instance of the Gamma class.

Visualization

The following image shows plot values of selected properties of Gamma using the plotly library.

Gamma line plot.

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: