Greeks and Implied Volatility
Key Concepts
Introduction
LEAN provides multiple ways for you to get the Greeks and implied volatility (IV) of Option contracts. There is a daily, pre-calculated dataset based on the end of the previous trading day. There is also a stream of values that are calculated on-the-fly as your algorithm runs.
Greeks on Universes
The Greeks and IV values in the filter function are the daily, pre-calculated values based on the end of the previous trading day.
public override void Initialize() { var option = AddOption("SPY"); option.SetFilter(universe => universe.IncludeWeeklys().Delta(0.3m, 0.7m).Expiration(0, 7); }
def initialize(self): option = self.add_option("SPY") option.set_filter(lambda universe: universe.include_weeklys().delta(0.3, 0.7).expiration(0,7))
To calculate the values, we use our implementation of the forward tree pricing model, which accounts for the interest rate and dividend payments.
You can't customize the Greeks and IV values that the filter function receives. However, you can create indicators to customize how the Greeks and IV are calculated for the contracts already in your universe.
Greek History
The Greeks and IV values that you get from a history request of the Option universe are the daily, pre-calculated values based on the end of the previous trading day.
public class OptionHistoryAlgorithm : QCAlgorithm { public override void Initialize() { SetStartDate(2020, 1, 1); var option = AddOption("SPY"); var history = History<OptionUniverse>(option.Symbol, 5); foreach (var chain in history) { var endTime = chain.EndTime; var filteredContracts = chain.Data .Select(contract => contract as OptionUniverse) .Where(contract => contract.Greeks.Delta > 0.3m); foreach (var contract in filteredContracts) { var price = contract.Price; var iv = contract.ImpliedVolatility; } } } }
class OptionHistoryAlgorithm(QCAlgorithm): def initialize(self): self.set_start_date(2020, 1, 1) option = self.add_option('SPY') # DataFrame example: history_df = self.history(option.symbol, 5, flatten=True) # OptionUniverse objects example: history = self.history[OptionUniverse](option.symbol, 5) for chain in history: end_time = chain.end_time filtered_contracts = [c for c in chain if c.greeks.delta > 0.3] for contract in filtered_contracts: symbol = contract.symbol expiry = contract.id.date strike = contract.id.strike_price price = contract.close iv = contract.implied_volatility
You can't customize the Greeks and IV values that you get from a history request. However, you can create indicators to customize how the Greeks and IV are calculated for the contracts already in your universe.
Greeks on Data Events
The Greeks and IV values that you get from the current Slice are from the price model calculations. These values aren't the same as the values you get in the universe filter function.
public class OptionDataEventsAlgorithm : QCAlgorithm { public override void Initialize() { var option = AddOption("GOOG"); option.PriceModel = OptionPriceModels.CrankNicolsonFD(); } public override void OnData(Slice data) { foreach (var chain in data.OptionChains.Values) { foreach (var contract in chain) { var iv = contract.ImpliedVolatility; var delta = contract.Greeks.Delta; } } } }
class OptionDataEventsAlgorithm(QCAlgorithm): def initialize(self): option = self.add_option("GOOG") option.price_model = OptionPriceModels.crank_nicolson_fd() def on_data(self, data): for canonical_symbol, chain in data.option_chains.items(): for contract in chain: iv = contract.implied_volatility delta = contract.greeks.delta
To view all the models LEAN supports, see Supported Models.
Greek Indicators
The Greeks and IV values that you get from the indicators are calculated in the same way as the values you get in the filter function, but they can reflect intraday prices instead of just the daily closing prices.
Examples
For detailed examples on using Option Greeks and Implied Volatility indicators in algorithm, you may refer to the Greeks Indicators page examples.