I have the following which works great if I have an option with underlying of QQQ:
optioninvestedQQQ = [symbol for symbol, holding in self.Portfolio.items() if holding.Invested and holding.Type == SecurityType.Option and symbol.Underlying.Equals(self.symbolqqq)]
How can I add to this code to fill with only a put or a call?
Thanks in advance
Yuri Lopukhov
You can check symbol.ID.OptionRight, it can be OptionRight.Call or OptionRight.Put
David Eldringhoff
Thanks for the response.
Tried this a few ways and kept getting errors. Here are the following attempts:
The following code:
putoptioninvestedQQQ = [symbol for symbol, holding in self.Portfolio.items() if holding.Invested and holding.Type == SecurityType.Option and symbol.Underlying.Equals(self.symbolqqq) and symbol.ID.OptionRight.Put]
Is met with the following error:
Runtime Error: 'int' object has no attribute 'Put'
at <listcomp>
putoptioninvestedQQQ = [symbol for symbol in main.py: line 175
at OnData
putoptioninvestedQQQ = [symbol for symbol in main.py: line 175
The following code:
putoptioninvestedQQQ = [symbol for symbol, holding in self.Portfolio.items() if holding.Invested and holding.Type == SecurityType.Option and symbol.Underlying.Equals(self.symbolqqq) and symbol.ID == OptionRight.Put]
Is met with the following error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
at <listcomp>
putoptioninvestedQQQ = [symbol for symbol in main.py: line 175
at OnData
putoptioninvestedQQQ = [symbol for symbol in main.py: line 175
Yuri Lopukhov
you should use symbol.ID.OptionRight == OptionRight.Put here
David Eldringhoff
excellent, worked like a charm. Thanks again for your help. I had one other question if you dont mind. Is there a way with similar code to determine if you are long or short on an option? If I sell a call or buy a call can I determine if my portfolio has one that is sold vs one that is bought?
Alexandre Catarino
Hi David Eldringhoff ,
We can use the IsLong and IsShort attributes of the SecurityHoldings object:
put_option_isLong_QQQ = [symbol for symbol, holding in self.Portfolio.items()
if holding.IsLong and holding.Type == SecurityType.Option and
symbol.Underlying.Equals(self.symbolqqq) and symbol.ID.OptionRight == OptionRight.Put]
put_option_isShort_QQQ = [symbol for symbol, holding in self.Portfolio.items()
if holding.IsShort and holding.Type == SecurityType.Option and
symbol.Underlying.Equals(self.symbolqqq) and symbol.ID.OptionRight == OptionRight.Put]
The sum of the two lists equals the invested list:
put_option_invested_QQQ = [symbol for symbol, holding in self.Portfolio.items()
if holding.Invested and holding.Type == SecurityType.Option and
symbol.Underlying.Equals(self.symbolqqq) and symbol.ID.OptionRight == OptionRight.Put]
Best regards,
Alex
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!