Supported Indicators
Theta
Introduction
Option Theta indicator that calculate the theta of an option
To view the implementation of this indicator, see the LEAN GitHub repository.
Using T Indicator
To create an automatic indicator for Theta, call the Tt helper method from the QCAlgorithm class. The Tt method creates a Theta 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 ThetaAlgorithm : QCAlgorithm
{
private Symbol _symbol, _option, _mirrorOption;
private Theta _t;
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);
_t = T(_option, _mirrorOption);
}
public override void OnData(Slice data)
{
if (_t.IsReady)
{
// The current value of _t is represented by itself (_t)
// or _t.Current.Value
Plot("Theta", "t", _t);
}
}
} class ThetaAlgorithm(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._t = self.t(self._option, self._mirror_option)
def on_data(self, slice: Slice) -> None:
if self._t.is_ready:
# The current value of self._t is represented by self._t.current.value
self.plot("Theta", "t", self._t.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a Theta 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 ThetaAlgorithm : QCAlgorithm
{
private Symbol _symbol, _option, _mirrorOption;
private Theta _theta;
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);
_theta = new Theta(_option, interestRateModel, dividendYieldModel, _mirrorOption);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_theta.Update(new IndicatorDataPoint(_symbol, bar.EndTime, bar.Close));
if (data.QuoteBars.TryGetValue(_option, out var quoteBar))
_theta.Update(new IndicatorDataPoint(_option, quoteBar.EndTime, quoteBar.Close));
if (data.QuoteBars.TryGetValue(_mirrorOption, out quoteBar))
_theta.Update(new IndicatorDataPoint(_mirrorOption, quoteBar.EndTime, quoteBar.Close));
if (_theta.IsReady)
{
// The current value of _theta is represented by itself (_theta)
// or _theta.Current.Value
Plot("Theta", "theta", _theta);
}
}
} class ThetaAlgorithm(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._theta = Theta(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._theta.update(IndicatorDataPoint(self._symbol, bar.end_time, bar.close))
bar = slice.quote_bars.get(self._option)
if bar:
self._theta.update(IndicatorDataPoint(self._option, bar.end_time, bar.close))
bar = slice.quote_bars.get(self._mirror_option)
if bar:
self._theta.update(IndicatorDataPoint(self._mirror_option, bar.end_time, bar.close))
if self._theta.is_ready:
# The current value of self._theta is represented by self._theta.current.value
self.plot("Theta", "theta", self._theta.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the Theta 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 ThetaAlgorithm : QCAlgorithm
{
private Symbol _symbol, _option, _mirrorOption;
private Theta _t;
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);
_t = T(_option, _mirrorOption);
var indicatorHistory = IndicatorHistory(_t, new[] { _symbol, _option, _mirrorOption }, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_t, new[] { _symbol, _option, _mirrorOption }, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_t, new[] { _symbol, _option, _mirrorOption }, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
}
} class ThetaAlgorithm(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._t = self.t(self._option, self._mirror_option)
indicator_history = self.indicator_history(self._t, [self._symbol, self._option, self._mirror_option], 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._t, [self._symbol, self._option, self._mirror_option], timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._t, [self._symbol, self._option, self._mirror_option], datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)