Hey guys, I've reviewed the options documentation and there are many types of data that can be accessible from an options contract. I was just looking for suggestions on how to best pull some of this information from a currently invested option.
In the on data section I have the following:
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
So is there a way for me to pull other data from the information stored in “option_invested”? like the Expiry date and the underlying symbol?
I tried the following but it doesn't work:
self.optionsinvested = option_invested
qqqexpiry = self.optionsinvested.ID.expiry
optionsunderlyingsymbol = self.optionsinvested.ID.UnderlyingSymbol
I also tried to just fill option_invested with only QQQ options with the following but it doesn't work:
option_investedqqq = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option and x.UnderlyingSymbol=="QQQ"]
David Eldringhoff
Actually I found a work around for the expiry. But does anyone have an idea about how to code that underlying symbol line?
Arthur Asenheimer
Under the assumption that self.qqq references to the QQQ Symbol, you can get the desired options as follows:
If you don't have a reference to the QQQ symbol you can still try something like
But I recommend to use Symbols as they're uniquely determined.
David Eldringhoff
Hey thanks for the response. I do have qqq as a symbol in initialize.
stockqqq = self.AddEquity("QQQ", Resolution.Minute)
self.stockqqq = stockqqq.Symbol
When I add the following line of code:
optionInvestedQQQ = [symbol for symbol, holding in self.Portfolio.items() if holding.Invested and holding.Type == SecurityType.Option and symbol.UnderlyingSymbol.Equals(self.stockqqq)]
It returns an error:
Runtime Error: AttributeError : 'Symbol' object has no attribute 'UnderlyingSymbol'
at <listcomp>
optionInvestedQQQ = [symbol for symbol in main.py: line 79 (Open Stacktrace)
The following code works well:
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
But doesnt ID the underlying asset. I tried adding
and symbol.UnderlyingSymbol.Equals(self.stockqqq)
and x.UnderlyingSymbol.Equals(self.stockqqq)
To the option_invested line and they essentially kicked back the same runtime error.
Thanks,
signed - stuck and frustrated quant lol
Arthur Asenheimer
Hey David,
sorry about that. It should be symbol.Underyling.Equals(self.stockqqq).
Please replace UnderlyingSymbol with Underlying and it will work. It's already the Symbol object so that we can get the underlying symbol directly by using symbol.Underlying. If you have the security object you would need security.UnderlyingSymbol, if I remember correctly.
Fred Painchaud
Hi,
Sorry to intervene.
Looking at
https://lean-api-docs.netlify.app/classQuantConnect_1_1Symbol.html
Symbol does have “Underlying” indeed.
But guessing from the API, I would check first with “HasUnderlying”. I guess in that context, it should ALWAYS have an underlying symbol BUT if it does not, symbol.Underlying.Equals will throw an exception.
So:
optionInvestedQQQ = [symbol for symbol, holding in self.Portfolio.items() if holding.Invested and holding.Type == SecurityType.Option and symbol.HasUnderlying and symbol.Underlying.Equals(self.stockqqq)]
Just a very very slight addition.
Fred
David Eldringhoff
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!