Quant League is evolving into Strategies, our new home for sharing, discovering, and exploring trading strategies, with improved organization and a better overall experience. Q4-2025 will be the final Quant League.
LEAN is the open-source algorithmic trading engine powering QuantConnect.
Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join Our Discord Channel
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
This research is under review. To publish this research attract three community upvotes.
This research is under review. To publish this research attract three community upvotes.
This discussion is a draft. Click here to publish this discusison.
Algorithmic Trading Video Series #12 | Options Trading
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.
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.
Spacetime
11.1k Pro
,
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
11.1k Pro
,
another suggestion: I think we need to use the self.SetWarmUp() method for this.
Spacetime
11.1k Pro
,
hmm another suggestion: should check if self.Securities[self.equity].Price exists before using it
Louis
141.8k Pro
,
Thanks for the suggestions
David E
12.9k Pro
,
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
45.8k Pro
,
Hi David
We recommend the following way:
[x.Symbol for x in self.Securities.Values if x.Invested and x.Type == SecurityType.Option and x.Underlying.Value == "QQQ"]
Best Louis
David E
12.9k Pro
,
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
31.1k Pro
,
Hi David, An alternative would be to store the Contract symbol to check :
QQQholdings = [x.Symbol for x in self.Securities.Values if x.Invested and x.Type == SecurityType.Option and x.Symbol in self.Contracts]
#here the contracts is a dictionary/Set to check if we have bought the contract or not
Best, Varad Kabade
David E
12.9k Pro
,
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
31.1k Pro
,
Hi David Eldringhoff, You can add different filters in list comprehension to get the desired contracts:
To get only SPY/QQQ call or put contracts we can add another condition like x.UnderlyingSymbol == self.spy(here we store the SPY symbol)
to differentiate between calls and puts we can add x.Right==OptionRight.Put(or call)
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
50 Pro
,
How do I ensure the market is open before I buy call? or is the order automatically placed after the market open?
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!
Allocate to this Strategy
Institutional clients can contact the author and allocate capital to this strategy.
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
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!