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 indicator for Gamma, call the Gg helper method from the QCAlgorithm class. The Gg 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 Initializeinitialize method.

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

    public override void Initialize()
    {
        SetStartDate(2024, 9, 1);
        SetEndDate(2024, 9, 20);

        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _option = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920C00564000");
        _mirrorOption = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920P00564000");
        AddOptionContract(_option, Resolution.Daily);
        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);
        }
    }
}
class GammaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 9, 20)

        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920C00564000");
        self._mirror_option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920P00564000");
        self.add_option_contract(self._option, Resolution.DAILY);
        self.add_option_contract(self._mirror_option, Resolution.DAILY);

        self._g = self.g(self._option, self._mirror_option)

    def on_data(self, slice: Slice) -> None:
        if self._g.is_ready:
            # The current value of self._g is represented by self._g.current.value
            self.plot("Gamma", "g", self._g.current.value)

For more information about this method, see the QCAlgorithm classQCAlgorithm class.

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 Updateupdate method. The indicator will only be ready after you prime it with enough data.

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

    public override void Initialize()
    {
        SetStartDate(2024, 9, 1);
        SetEndDate(2024, 9, 20);

        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        var interestRateModel = new InterestRateProvider();
        var dividendYieldModel = new DividendYieldProvider(_symbol);
        
        _option = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920C00564000");
        _mirrorOption = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920P00564000");
        AddOptionContract(_option, Resolution.Daily);
        AddOptionContract(_mirrorOption, Resolution.Daily);

        _gamma = new Gamma(_option, interestRateModel, dividendYieldModel, _mirrorOption);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
            _gamma.Update(new IndicatorDataPoint(_symbol, bar.EndTime, bar.Close));
        if (data.QuoteBars.TryGetValue(_option, out var quoteBar))
            _gamma.Update(new IndicatorDataPoint(_option, quoteBar.EndTime, quoteBar.Close));
        if (data.QuoteBars.TryGetValue(_mirrorOption, out quoteBar))
            _gamma.Update(new IndicatorDataPoint(_mirrorOption, quoteBar.EndTime, quoteBar.Close));

        if (_gamma.IsReady)
        {
            // The current value of _gamma is represented by itself (_gamma)
            // or _gamma.Current.Value
            Plot("Gamma", "gamma", _gamma);
        }
    }
}
class GammaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 9, 20)

        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        interest_rate_model = InterestRateProvider()
        dividend_yield_model = DividendYieldProvider(self._symbol)

        self._option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920C00564000");
        self._mirror_option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920P00564000");
        self.add_option_contract(self._option, Resolution.DAILY);
        self.add_option_contract(self._mirror_option, Resolution.DAILY);

        self._gamma = Gamma(self._option, interest_rate_model, dividend_yield_model, self._mirror_option)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self._symbol)
        if bar:
            self._gamma.update(IndicatorDataPoint(self._symbol, bar.end_time, bar.close))
        bar = slice.quote_bars.get(self._option)
        if bar:
            self._gamma.update(IndicatorDataPoint(self._option, bar.end_time, bar.close))
        bar = slice.quote_bars.get(self._mirror_option)
        if bar:
            self._gamma.update(IndicatorDataPoint(self._mirror_option, bar.end_time, bar.close))

        if self._gamma.is_ready:
            # The current value of self._gamma is represented by self._gamma.current.value
            self.plot("Gamma", "gamma", self._gamma.current.value)

For more information about this indicator, see its referencereference.

Visualization

The following plot shows values for some of the Gamma indicator properties:

Gamma line plot.

Indicator History

To get the historical data of the Gamma indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.

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

    public override void Initialize()
    {
        SetStartDate(2024, 9, 1);
        SetEndDate(2024, 9, 20);

        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _option = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920C00564000");
        _mirrorOption = SymbolRepresentation.ParseOptionTickerOSI("SPY 240920P00564000");
        AddOptionContract(_option, Resolution.Daily);
        AddOptionContract(_mirrorOption, Resolution.Daily);

        _g = G(_option, _mirrorOption);

        var indicatorHistory = IndicatorHistory(_g, new[] { _symbol, _option, _mirrorOption }, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_g, new[] { _symbol, _option, _mirrorOption }, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_g, new[] { _symbol, _option, _mirrorOption }, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
    }
}
class GammaAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 9, 20)

        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920C00564000");
        self._mirror_option = SymbolRepresentation.parse_option_ticker_osi("SPY 240920P00564000");
        self.add_option_contract(self._option, Resolution.DAILY);
        self.add_option_contract(self._mirror_option, Resolution.DAILY);

        self._g = self.g(self._option, self._mirror_option)

        indicator_history = self.indicator_history(self._g, [self._symbol, self._option, self._mirror_option], 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._g, [self._symbol, self._option, self._mirror_option], timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._g, [self._symbol, self._option, self._mirror_option], datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)

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: