Hello,
Below you can find the backtest and code from my latest video covering options trading inside of QuantConnect. Simply click the “Clone Algorithm” button to copy the code.
If you're new to the series, make sure to check out the overview of my full free algorithmic trading course here.
Let me know if you have any questions or comments.
Cheers,
Louis
Spacetime
one suggestion:
you may receive a divisible by zero error for quantity, so you should check if self.call.AskPrice is zero or not before using it
Spacetime
another suggestion: I think we need to use the self.SetWarmUp() method for this.
Spacetime
hmm another suggestion: should check if self.Securities[self.equity].Price exists before using it
Louis
Thanks for the suggestions
David E
Hey Louis,
First, I just wanted to thank you in advance for these wonderful videos. I think you’re doing a great job with these. Very detailed and helps us more novice coders understand some of these theories better.
I have a question. I’m trying to use self.Portfolio.invested to and not invested to help trigger buy sells. However, if I want to trade more than just 1 stock or option then this doesn’t work. I tried self.Portfolio[“QQQ”].invested but that really only works for the stock and not underlying derivatives. lets say I wanted to produce an algo that buys a call in QQQ when QQQ hits a buy signal but also a call in SPY when SPY hits a buy signal. How can I strict my algo to check for portfolio investments of options from different stocks?
I see your algo does this…
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option] if option_invested:
this may help but really only allows for 1 option at a time. Any suggestions?
thanks
david
Louis Szeto
Hi David
We recommend the following way:
Best
Louis
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.
David E
Plugged this into Ondata and then logged QQQholdings so I can see how it results and as soon as I buy an option the algo hits an error on that line of code.
QQQholdings = [x.Symbol for x in self.Securities.Values if x.Invested and x.Type == SecurityType.Option and x.Underlying.Value == "QQQ"]
Runtime Error: AttributeError : 'Equity' object has no attribute 'Value' at <listcomp> QQQholdings = [x.Symbol for x at self.Securities.Values if x.Invested and x.Type == SecurityType.Option and x.Underlying.Value == "QQQ"] File "main.py" in main.py: line 85
Varad Kabade
Hi David,
An alternative would be to store the Contract symbol to check :
Best,
Varad Kabade
David E
Varad,
Thanks, if I do the following:
QQQholdings = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
It fills “QQQholdings” with the purchased call. Now I would like to fill a few parameters to differentiate calls from puts and calls of QQQ from calls of SPY. Is there a way to only fill it with calls or puts?
Example, how can I fill the following:
QQQcallholdings = with only QQQ calls
QQQputholdings = only QQQ puts
SPYcallholdings = Only SPY calls
SPYputholdings = Only SPY puts
I cant figure out how to extract the option of a specific underlying asset “QQQ” and I dont know how to add something to the following statement to distinguish calls from puts.
[x.Symbol for x in self.Securities.Values if x.Invested and x.Type == SecurityType.Option and x.Symbol in self.Contracts]
Can I just add something like “if x.Right == OptionRight.Call” or “if c.Right == OptionRight.Put”?
Varad Kabade
Hi David Eldringhoff,
You can add different filters in list comprehension to get the desired contracts:
We recommend going through the properties of the OptionContract class in the following doc for more information. Note that we need to add the above condition at the end of the existing list comprehension statement.
Best,
Varad Kabade
Usha Annangi
How do I ensure the market is open before I buy call? or is the order automatically placed after the market open?
Louis
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!