Is there an example on how to get Greeks Values from inside an Alpha Model?

I saw that the Option Universe Selection model inside the Wizard returns Option object, but Greeks are inside OptionContracts objects and they also need a Pricing Model and a little bit of WarmUp.

I'm, quite new to Python so I tried something, but I always get 0 as the value of Delta. It will be useful to have a simple example on how to get Greeks inside an Alpha Model

from Execution.ImmediateExecutionModel import ImmediateExecutionModel from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel from OptionsUniverseSelectionModel import OptionsUniverseSelectionModel from QuantConnect.Securities.Option import OptionPriceModels from QuantConnect import Resolution from QuantConnect import OptionRight import numpy as np tickers = ['SPY','QQQQ'] symbolList = [] weight = 1/len(tickers) class CannaMaxLeverageAlphaModel(AlphaModel): def __init__(self): self.emitted = [] self.previousSymbols = [] pass def OnSecuritiesChanged(self, algorithm, changes): alloptions = changes.AddedSecurities for ticker in symbolList: alloptions = [x for x in alloptions if x.Symbol.ID!=ticker.ID.Underlying ] # myOptions = [x for x in alloptions if x.Right==OptionRight.Call] maxExpiry = sorted(alloptions, key = lambda x: x.Expiry, reverse=True)[0] myOptions = [x for x in alloptions if x.Expiry==maxExpiry.Expiry] equities=[x for x in changes.AddedSecurities if x.Symbol.ID in [ticker.ID.Underlying for ticker in symbolList]] for x in myOptions: #pricing models: https://www.quantconnect.com/lean/documentation/topic27704.html x.PriceModel = OptionPriceModels.CrankNicolsonFD() #symbols = [x.Symbol for x in myOptions] #history = algorithm.History(symbols, timedelta(days=7), Resolution.Minute) def Update(self, algorithm, data): insights = [] #https://www.quantconnect.com/lean/documentation/topic24200.html chain = [x.Value for x in data.OptionChains] contracts = [[]] * len(tickers) for x in range(len(tickers)): #https://www.quantconnect.com/lean/documentation/topic24233.html contracts[x] = [y for y in chain[x] if y.Right==OptionRight.Call] maxExpiry = sorted(contracts[x], key = lambda y: y.Expiry, reverse=True)[0] contracts[x] = [y for y in contracts[x] if y.Expiry==maxExpiry.Expiry] symbols = [] for x in range(len(contracts)): qta = [0] * len(contracts[x]) capacity = [0] * len(contracts[x]) i = 0 for y in contracts[x]: qta[i] = round(algorithm.CalculateOrderQuantity(y.Symbol, weight),0) capacity[i] = qta[i] * y.Greeks.Delta i = i+1 imax = capacity.index(np.max(capacity)) #emit insights once if not (contracts[x][imax].Symbol in self.emitted): self.emitted.append(contracts[x][imax].Symbol) symbols.append(contracts[x][imax].Symbol) insights=[] if symbols != []: for x in symbols: insights.append(Insight.Price(x,timedelta(days=28), InsightDirection.Up)) if self.previousSymbols != []: for y in self.previousSymbols: insights.append(Insight.Price(y,timedelta(28), InsightDirection.Flat)) self.previousSymbols = symbols return insights

 

Author