Each algorithm I run returns no trades on historic data. I loaded the code below from one of the contributors on the forum to see what it showed and it returned no orders or returns as well. Is there something in the QC Cloud setting that I need to change? I'm new to the platform and a little lost. Thanks for your help.
from AlgorithmImports import *
class SquareMagentaHamster(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1) # Set Start Date
self.SetEndDate(2019, 1, 1)
self.SetCash(1000000) # Set Strategy Cash
index = self.AddIndex("SPX", Resolution.Minute)
self.spx = index.Symbol
option = self.AddOption(index.Symbol, Resolution.Minute)
option.SetFilter(self.UniverseFunc)
self.symbol = option.Symbol
self.indicator = SimpleMovingAverage(200)
self.RegisterIndicator(self.spx, self.indicator, Resolution.Daily)
self.entryTicket = None
self.SLTicket = None
self.TPTicket = None
self.entryTime = datetime.min
self.AddRiskManagement(TrailingStopRiskManagementModel(0.15))
def UniverseFunc(self, universe: OptionFilterUniverse) -> OptionFilterUniverse:
return universe.IncludeWeeklys().Strikes(1, 2).Expiration(timedelta(45), timedelta(50))
def OnData(self, data: Slice):
if not(data.ContainsKey(self.spx) and data[self.spx] is not None): return
if not self.indicator.IsReady: return
option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
if option_invested:
for option in option_invested:
if self.Time + timedelta(1) > option.ID.Date:
self.Liquidate(option, "Option too close to expiration.")
lvl = self.indicator.Current.Value
atSMA = self.Securities[self.spx].AskPrice > lvl - 2 and self.Securities[self.spx].AskPrice < lvl + 2
if atSMA and not self.Portfolio.Invested:
self.BuyCall(data)
self.BuyPut(data)
def BuyCall(self, data: Slice) -> None:
# Get the OptionChain
chain = data.OptionChains.get(self.symbol, None)
if not chain: return
# Get the furthest expiry date of the contracts
expiry = sorted(chain, key = lambda x: x.Expiry, reverse=True)[0].Expiry
# Select the call Option contracts with the furthest expiry
calls = [i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Call]
if len(calls) == 0: return
call_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UnderlyingLastPrice))
if len(call_contracts) == 0:
return
self.call = call_contracts[0]
self.entryTicket = self.MarketOrder(self.call.Symbol, 5)
def BuyPut(self, data: Slice) -> None:
# Get the OptionChain
chain = data.OptionChains.get(self.symbol, None)
if not chain: return
# Get the furthest expiry date of the contracts
expiry = sorted(chain, key = lambda x: x.Expiry, reverse=True)[0].Expiry
# Select the call Option contracts with the furthest expiry
puts = [i for i in chain if i.Expiry == expiry and i.Right == OptionRight.Put]
if len(puts) == 0: return
put_contracts = sorted(puts, key = lambda x: abs(x.Strike - x.UnderlyingLastPrice))
if len(put_contracts) == 0:
return
self.put = put_contracts[0]
self.entryTicket = self.MarketOrder(self.put.Symbol, 5)
Mia Alissi
From the code you've shared, it seems like you're trying to implement an options trading strategy on the SPX index. The algorithm is designed to buy call and put options when the SPX price is close to a 200-day simple moving average.
If you're not seeing any trades, it could be due to a few reasons:
The conditions for buying options (i.e., the SPX price being close to the 200-day SMA) might not be met during the backtest period. You can add some logging to your algorithm to check if this condition is being met.
The options filter in the
UniverseFunc
method might not be returning any options. This method is set to include weekly options that are 1-2 strikes from the current price and expiring in 45-50 days. If no such options are available, no trades will be made. You can also add some logging in this method to check if it's returning any options.The options you're trying to trade might not have enough liquidity. If the bid-ask spread is too wide or if there's not enough volume, your market orders might not get filled. You can check the historical data for the options you're trying to trade to see if this is the case.
There might be an issue with the data or the brokerage model. The algorithm is set to use the default brokerage model, which might not accurately represent the trading conditions for options on the SPX index. You might need to adjust the brokerage model or the data normalization mode.
If you're still having issues, I would recommend posting your question on the QuantConnect Forum. The community there is very helpful and might be able to provide more specific advice based on your situation.
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.
Danny Bannister
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!