Supported Indicators

Implied Volatility

Introduction

Implied Volatility indicator that calculate the IV of an option using Black-Scholes Model

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

Using IV Indicator

To create an automatic indicator for ImpliedVolatility, call the IViv helper method from the QCAlgorithm class. The IViv method creates a ImpliedVolatility 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 ImpliedVolatilityAlgorithm : QCAlgorithm
{
    private Symbol _symbol, _option, _mirrorOption;
    private ImpliedVolatility _iv;

    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);

        _iv = IV(_option, _mirrorOption);
    }

    public override void OnData(Slice data)
    {
        if (_iv.IsReady)
        {
            // The current value of _iv is represented by itself (_iv)
            // or _iv.Current.Value
            Plot("ImpliedVolatility", "iv", _iv);
        }
    }
}
class ImpliedVolatilityAlgorithm(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._iv = self.iv(self._option, self._mirror_option)

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

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

You can manually create a ImpliedVolatility 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 ImpliedVolatilityAlgorithm : QCAlgorithm
{
    private Symbol _symbol, _option, _mirrorOption;
    private ImpliedVolatility _impliedvolatility;

    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);

        _impliedvolatility = new ImpliedVolatility(_option, interestRateModel, dividendYieldModel, _mirrorOption);
    }

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

        if (_impliedvolatility.IsReady)
        {
            // The current value of _impliedvolatility is represented by itself (_impliedvolatility)
            // or _impliedvolatility.Current.Value
            Plot("ImpliedVolatility", "impliedvolatility", _impliedvolatility);
        }
    }
}
class ImpliedVolatilityAlgorithm(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._impliedvolatility = ImpliedVolatility(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._impliedvolatility.update(IndicatorDataPoint(self._symbol, bar.end_time, bar.close))
        bar = slice.quote_bars.get(self._option)
        if bar:
            self._impliedvolatility.update(IndicatorDataPoint(self._option, bar.end_time, bar.close))
        bar = slice.quote_bars.get(self._mirror_option)
        if bar:
            self._impliedvolatility.update(IndicatorDataPoint(self._mirror_option, bar.end_time, bar.close))

        if self._impliedvolatility.is_ready:
            # The current value of self._impliedvolatility is represented by self._impliedvolatility.current.value
            self.plot("ImpliedVolatility", "impliedvolatility", self._impliedvolatility.current.value)

For more information about this indicator, see its referencereference.

Visualization

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

ImpliedVolatility line plot.

Indicator History

To get the historical data of the ImpliedVolatility 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 ImpliedVolatilityAlgorithm : QCAlgorithm
{
    private Symbol _symbol, _option, _mirrorOption;
    private ImpliedVolatility _iv;

    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);

        _iv = IV(_option, _mirrorOption);

        var indicatorHistory = IndicatorHistory(_iv, new[] { _symbol, _option, _mirrorOption }, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_iv, new[] { _symbol, _option, _mirrorOption }, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_iv, new[] { _symbol, _option, _mirrorOption }, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
    }
}
class ImpliedVolatilityAlgorithm(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._iv = self.iv(self._option, self._mirror_option)

        indicator_history = self.indicator_history(self._iv, [self._symbol, self._option, self._mirror_option], 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._iv, [self._symbol, self._option, self._mirror_option], timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._iv, [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: