Hello everybody
I was trying to create a strategy that would sell a short put spread (bullish) with a SMA 200 days filter.
So it would only short the put spread if the momentum is bullish following the 200 days SMA.
The problem I have is that the strategy doesn't mind the SMA crossover condition.
Do you guys have an idea on how to fix this?
(Please excuse me for the irrelevant files and name of the notebook as only main.py is relevant)
Thanks a lot
AlMoJo
sorry by the way I have just realized that I was comparing the option chain price with the SMA price
Mia Alissi
Here are the functional bugs found in 'AlMoJo's' code that could cause the SMA 200 filter or spread logic not to work as intended (excluding naming, allocation, and import issues):
main.py
self.SetWarmUp(200, Resolution.Daily)andself.SetWarmup(200, Resolution.Daily)are called. Only the properly casedSetWarmUpwill work; having both is redundant, and the lowercase variant will cause an exception if reached..Expiration(0, 1)(contracts expiring in one day or less), but later the code tries to select contracts expiring close to 30 days, so the chain will likely be empty and never result in trades.if not self.sma200.IsReady:after it's already checked at the beginning ofOnData. This is unnecessary but not harmful.f"Price={price:.2f}, SMA200={self.sma200.Current.Value:.2f}")is not assigned or logged—this orphaned line (with an extra closing parenthesis and indentation) will cause a syntax error and prevent the script from running.openSpreadis set after trades but is only checked forany(self.Portfolio[x].Invested for x in self.openSpread). If one leg is assigned or expires, the other may remain inopenSpreadand block new trades, potentially causing logic to stall on assignment/expiry.execution.py
No code issues identified in the given execution model. All main functionality issues relevant to your SMA filtering and spread trading live in main.py.
Summary:
Address the expiration filter, typo redundancy, f-string syntax error, and index/logic checks around the spread construction. These are necessary to restore reliability of the 200-SMA momentum filter and ensure spread logic functions as expected.
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.
Mia Alissi
If you compare an option contract's price to the SMA of the underlying asset, the units are not compatible: option prices are often much smaller and behave differently (they are premiums, not share prices). The SMA is calculated from the price history of the underlying security, so you must compare the current price of the underlying asset itself to its SMA.
Correct Approach:
chain.Underlying.Priceorself.Securities[underlying].Price), which is in the same units as the SMA.self.sma.Current.Value).Why This Works:
Both values are in "USD per share" (or whatever the currency/quote asset is), so it is a clean momentum comparison.
Sample Logic Patch
What Was Wrong Before:
If you did
if some_option.Price > self.sma200.Current.Value:you were comparing an option contract's price (in dollars per option, often $2–$10) to a stock price (often $200+)—which is a meaningless comparison.Summary:
This structure will result in consistent, meaningful bullish/bearish signals for your spread logic.
Sources: Equity Options - QuantConnect.com, Combining Indicators - QuantConnect.com, US Futures Security Master - QuantConnect.com, Indicators - QuantConnect.com, Indicators - QuantConnect.com
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.
AlMoJo
I was able to fix it by changing from OnData to a daily scheduler :)
AlMoJo
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!