class TemplateA(QCAlgorithm): def Initialize(self): self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage) # Defaults to margin account. Ready to be used on IBKR self.SetStartDate(2020, 3, 3) # Set Start Date self.initialcash = 10000 # Set Strategy Cash self.SetCash(self.initialcash) self.Resolution = Resolution.Minute self.NFLX=self.AddEquity("NFLX", self.Resolution) self.ROKU=self.AddEquity("ROKU", self.Resolution) self.SetWarmup(10, self.Resolution) self.Schedule.On(self.DateRules.EveryDay(self.ROKU.Symbol),self.TimeRules.Every(timedelta(minutes=60)),self.trade) ## Risk Management self.numberOfOrdersPerDay= 6 self.numberOfSharesPerTrade= -2 #short shares need the minus sign self.MaxNumberOfSharesLong= 20 self.MaxNumberOfSharesShort= -20 #short shares need the minus sign self.takeProfit = 1.10 # means a 10% take profit self.stopLoss = 0.95 # means a 5% stop loss def OnData(self, slice): #Take profit logic if self.Portfolio.Invested: if self.Portfolio.TotalPortfolioValue > self.initialcash * self.takeProfit:# means a 5% take profit target, if the initial portfolio value is 100000 with 1.05 you will take profit when the value of portfolio is greater than 105 000 $. self.Liquidate() self.Debug("Take profit triggered") #Stop loss logic if self.Portfolio.Invested: if self.Portfolio.TotalPortfolioValue < self.initialcash * self.stopLoss: # means a 10% stop loss. In this case 0.9 means that the portfolio is valued a 90% of the original value, so if the initial value is 100 000 $ it means 90 000$, a 10% stop loss. if you set self.initialcash*0.5 means a 50% stop loss and so on. self.Liquidate() self.Debug("Stop Loss triggered") def trade(self): NFLX = self.History(self.NFLX.Symbol, timedelta(7), Resolution.Daily)["close"].unstack(level=0) NFLX_latest_close = NFLX['NFLX'][-1] NFLX_first_close = NFLX['NFLX'][-5] NFLX_percentage_move = (NFLX_latest_close - NFLX_first_close)/NFLX_first_close*100 ROKU = self.History(self.ROKU.Symbol, timedelta(7), Resolution.Daily)["close"].unstack(level=0) ROKU_latest_close = ROKU['ROKU'][-1] ROKU_first_close = ROKU['ROKU'][-5] ROKU_percentage_move = (ROKU_latest_close - ROKU_first_close)/ROKU_first_close*100 exchange = self.Securities["ROKU"].Exchange if exchange.ExchangeOpen and self.MaxNumberOfSharesShort<self.Portfolio["ROKU"].Quantity<self.MaxNumberOfSharesLong: marketTicket = self.MarketOrder("ROKU", self.numberOfSharesPerTrade) self.Debug("Market Order Fill Price: {0}".format(marketTicket.AverageFillPrice)) self.Debug("ROKU shares held "+str(self.Portfolio["ROKU"].Quantity))

Hi everybody!

I'm developing a simple strategy which should trade ROKU and NFLX according to some returns criteria (still haven't developed that part).

 

For now, with the code above, I get:

BacktestingRealTimeHandler.Run(): There was an error in a scheduled event ROKU: EveryDay: Every 60 min. The error was IndexError : index out of bounds

I don't have this error if I choose different backtesting periods...

I think it's related to the hourly scheduling function...can anyone help?

 

Thanks!