Hi,
I want to get my delta exposure for every option in my portfolio, this is what I do:
( I want to add up all the deltas to get total delta )
def getDelta( ):
for inst in self.Portfolio:
if inst.Value.Invested:
#here I try to get the delta but "inst" is OptionHolding object and not option
thanks
Daniel Chen
Hi Gil,
We provide Greeks for option contracts. You could use self.Portfolio[symbol].Value.Greeks to assess the Greeks provided by us. Thank you!
Gil Sapir
Hi,
this:
self.Portfolio[symbol]
does not return option contract, it returns optionHolding.
if you try what you suggested it will say "OptionHolding has no attribute Value"
and if you try:
self.Portfolio[symbol].Greeks
it will say "OptionHolding has no attribute Greeks"
this is the attrubutes option Holding has:
https://www.quantconnect.com/lean/documentation/topic27676.html
so than again, I can't see the greeks of my holdings
thanks
Daniel Chen
Hi Gil,
Please provide the full algorithm so that we can better help you solve the problem. Thanks!
Patrick Dietrich
I have the same Problem.
here is some example code:
for k in self.Portfolio:
symbol = k.Key
holding = k.Value
Console.WriteLine(f"k: {k}" )
Console.WriteLine(f"k.Value: {k.Value}" )
writes:
k: [NUGT 180615C00031000, QuantConnect.Securities.Option.OptionHolding]
k.Value: QuantConnect.Securities.Option.OptionHolding
So the portfolio contains objects of type QuantConnect.Securities.Option.OptionHolding.
How can we access the Greeks frome here?
This is the documentation for the class: https://www.quantconnect.com/lean/documentation/topic27673.html
Patrick Dietrich
The class havving the Greeks Object is QuantConnect.Data.Market.OptionContract.
It can be accessed e.g. via self.CurrentSlice.OptionChains. But how can I get one of those for the option contract which is already in my portfolio?
Mathieu Meynier
Hello,
Here is the code I use:
def get_greeks(self, slice): self.myOptionPortfolioKeys = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option] self.myOptionPortfolio = [] for chain in slice.OptionChains.Values: for i in chain: for o in self.myOptionPortfolioKeys: if i.Symbol == o: self.myOptionPortfolio.append(i) self.Delta = sum([opt.Greeks.Delta for opt in self.myOptionPortfolio]) self.Gamma = sum([opt.Greeks.Gamma for opt in self.myOptionPortfolio]) self.Vega = sum([opt.Greeks.Vega for opt in self.myOptionPortfolio])
Patrick Dietrich
Thanks, Mathieu.
I will try that. But there should be a simpler (and faster) way than iterating through all contracts. Maybe someone from QuantConnect could comment?
Also this solution has one problem:
If I filter the contracts in the OptionChains by expiration with SetFilter, the option in my portfolio may not be in the OptionChains anymore (because, after holding the option for some time, its expiry date may be sooner than the soonest which the filter allows).
Mathieu Meynier
Hey,
I may have complexified it.
Another solution that could work is to keep your options in a dictionnary and call them when you have to compute the greeks.
def initialize(self): self.myOptions = {} [...] def OnData(self, slice): if slice.OptionChains.Count == 0: return for i in slice.OptionChains: if i.Key != self.option_symbol: continue chain = i.Value puts = [x for x in chain if x.Right == OptionRight.Put and x.Expiry >= (self.Time+timedelta(days=365))] if len(puts) == 0: continue # Retrieve the desired OTM put OTM_puts = [x for x in puts if (x.Strike / x.UnderlyingLastPrice) <= 0.8] if len(OTM_puts) == 0: continue OTM_Put = sorted(OTM_puts, key = lambda x: x.Strike, reverse=True)[0] self.myOptions[OTM_Put.Symbol] = OTM_Put [...] for o in self.myOptions.keys(): self.Delta = self.Delta + self.myOptions[o].Greeks.Delta [...]
Patrick Dietrich
Hello Mathieu,
yes, that would be a simpler solution. However it does not work when restarting the algorithm with existing holdings in the potfolio. I have implemented a variation of your first solution now. It works fine, however I still think the API should make this easier.
Here is my current solution:
def get_option_for_option_holding(self, symbol): for chain in self.CurrentSlice.OptionChains.Values: for opt in chain: if opt.Symbol == symbol: return opt def get_option_holdings(self): options = [] for holding in self.Portfolio.Values: if holding.Invested and holding.Type == SecurityType.Option: option = self.get_option_for_option_holding(holding.Symbol) options.append((holding.Quantity, option)) return options # somewhere else in the program: options = self.get_option_holdings() for quantity, opt in options: dte = (opt.Expiry - self.Time).days message = f'{opt.Symbol} {dte}dte S:{opt.Strike} {quantity}x{opt.BidPrice} {opt.Greeks.Delta:.2f}' Console.WriteLine(message)
Derek Melchin
Hi everyone,
Thank you all for your contributions to the final solution posted above. It's well written, but a conditional should be placed between lines 11 and 12.
if option:
This is necessary as the get_option_for_option_holding method has the possibility of returning `None`.
See the attached algorithm for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
ForeverYoungNow
Thank you for sharing!
Gil Sapir
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!