I am trying to test some very basic options strategy where I sell OTM call and put contracts and short the underlying in some cases. Now I'm trying to create some indicator to know If I still hold the put contract I sold.
for that I iterate self.Portfolio as follows:
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type == SecurityType.Option]
option_invested = [x for x in option_invested if x.OptionRight == OptionRight.Put]
Which results the following error:
Runtime Error: AttributeError : 'Symbol' object has no attribute 'OptionRight'
at <listcomp>
option_invested = [x for x at option_invested if x.OptionRight == OptionRight.Put]
Which is really odd because according to the documentation a symbol object has OptionRight attribute. So the only option is that the first filter (first line in code) doesn't filter any non-option positions ?
Yarden Gur
Ok so I've found the following snippet to be working to be only extracting put options from portfolio:
Though I'm still a bit confused about what type of objects are stored in self.Portfolio annd what why do they have ID if they are symbol objects?
Fred Painchaud
Hi Yarden,
self.Portfolio contains SecurityHolding objects. Or more precisely, it is a dictionary of Symbol → SecurityHolding. So when you iterate over it, depending on how you do it, you either iterate over Symbol objects, SecurityHolding objects or tuples of (Symbol, SecurityHolding).
Fred
Yarden Gur
Hi Fred,
Thanks for your answer. So If I unnderstannd correctly the SecurityHolding can be of any class (ie equity, forex, crypto, options etc.)?
Fred Painchaud
Hi Yarden,
Correct. In fact, SecurityHolding is the base class for all types of security holdings. You can get the current type with the .Type property (on a SecurityHolding object).
Types are of type SecurityType (it's an enumeration):
Thus, SecurityType.Equity, for instance.
Fred
Yarden Gur
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!