I am trying to get the information about the option contracts: namely the expiry date and the type (whether they are puts or calls), straight from the Portfolio holding object. So far in backtesting I have had to save the option contracts that I trade to my own list and access that contract info later on to check the expiry. The idea is I want to be able to close out the trade before the option expires. I am trying to improve the algo so that it doesn't need to "remember" the contracts from when they are traded. The algo should be able to tell the expiry of the contracts just from accessing them in the Portfolio object.
The way I am currently accessing the portfolio holdings is like this:
for sym in self.Portfolio.Keys:
holding = self.Portfolio[sym]
if holding.HoldStock and holding.Symbol.SecurityType == SecurityType.Option:
'''We have an Option'''
I would like to find attributes in the holding object like holding.Symbol.Type ( put or call ) and holding.Symbol.Expiry. Does something like this exist? It doesn't seem like it according to the documentation here: https://www.quantconnect.com/lean/documentation/topic7826.html
I am afraid that storing my traded options in my own list will not work when I need to take the live strategy offline and restart, etc. The algo should be able to know the details of the option without having to save them from the time of the trade, imo.
Thanks!
Andrew Chan
Not sure if you solved this, but had the same issue, you can access it like this:
optionClass=self.Securities[sym]
then you can use the OptionClass
https://www.quantconnect.com/lean/documentation/topic27617.html
expiry=optionClass.Expiry.date()
strike=optionClass.StrikePrice
etc.
Alexandre Catarino
Thank you for the contribution, Andrew Chan .
The information about expiry and strike price can be found in the SecurityIdentifier object that is an attribute of the Symbol object:
for kvp in self.Securities: symbol = kvp.Key id = symbol.ID security_type = id.SecurityType if security_type = SecurityType.Option expiry = id.Date strike_price = id.StrikePrice
Â
Diego Munoz
Is there a way to get invested Option Greeks from self.Securities as well?
Varad Kabade
Hi Diego Munoz,
Unfortunately, it is not possible currently. We have an open GH issue subscribe to the following for updates:
https://github.com/QuantConnect/Lean/issues/5067
We recommend saving the Contract object in a dictionary keyed by the symbol so we can access the greeks.
Best,
Varad Kabade
Tim De Lise
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!