How do I check if an option is already in a portfolio? I couldn’t find anything in the docs. Should be something simple like if len(options) == 0: continue
I’m thinking it has something to do with the Portfolio object but I couldn’t find the right keyword
With regular stocks I think it’s if Portfolio.Invested.
Any help appreciated. Thanks!
Michael Manus
just search in the forum:
https://www.quantconnect.com/forum/discussion/4691/trading-options-for-multiple-symbols/p1/comment-13783
https://www.quantconnect.com/forum/discussion/3938/loop-through-self-securities/p1/comment-11890
https://www.quantconnect.com/forum/discussion/4905/currently-open-position-at-end-of-backtest/p1/comment-14355
:)
Halldor Andersen
Hi Jonathan.
You can use len(options_invested) to check if you have exposure to options, as you have done in your code. However, you need to remove following in the method OnData() to get the list of options held in portfolio:
if self.Portfolio.Invested: return
When you have an option contract in your portfolio, self.Portfolio.Invested will be True, so the rest of the code in the method OnData() is not executed.
For further information, check out this documentation section on QuantConnect Options API.
Jonathan Moore
thanks!
Jonathan Moore
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!