HI Everyone! I am very new to QuantConnect and having problems with my first algorithm.
I get the error: "BacktestingRealTimeHandler.Run(): There was an error in a scheduled event SPY: EveryDay: SPY: 20 min after MarketOpen. The error was TypeError : No method matches given arguments for History" when I try to run the following code:
import numpy as np
class QuantumModulatedPrism(QCAlgorithm):
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2019,9,1)
self.SetEndDate(2020,12,1)
self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
self.Debug(self.symbol)
self.Log(self.symbol)
self.lookback=20
self.ceiling,self.floor=30,10
self.initialStopRisk=0.98
self.trailingStopRisk=0.9
self.Schedule.On(self.DateRules.EveryDay(self.symbol),\
self.TimeRules.AfterMarketOpen(self.symbol,20),\
Action(self.EveryMarketOpen))
def OnData(self, data):
self.Plot("Data Cahart", self.symbol, self.Securities[self.symbol].Close)
def EveryMarketOpen(self):
close=self.History(self.symbol, 31, Resolution.Daily)["close"]
todayvol = np.std(close[1:31])
yesterdayvol=np.std(close[0:30])
deltavol = (todayvol-yesterdayvol)/todayvol
self.lookback = self.lookback*(1+deltavol)
if self.lookback>self.ceiling:
self.lookback = self.ceiling
elif self.lookback<self.floor:
self.lookback=self.floor
self.high = self.History(self.symbol, self.lookback, Resolution.Daily)["high"]
if not self.Securities[self.symbol].Invested and \
self.Securities[self.symbol].Close >= max(self.high[:-1]):
self.SetHoldings(self.symbol, 1)
self.brakeoutlvl = max(self.high[:-1])
self.highestPrice = self.breakoulvl
if self.Securities[self.symbol].Invested:
if not self.Transactions.GetOpenOrders(self.symbol):
self.stopMarketTicket= self.StopMarketOrder(self.symbol, \
-self.Protfolio[self.symbol].Quantity, \
self.initialStopRisk*self.breakoutlvl)
if self.Securities[self.symbol].Close>self.highestPrice and \
self.initalStopRisk*self.breakoutlvl < self.Securities[self.symbol].Close*self.trailingStopRisk:
self.highestPrice=self.Securities[self.symbol].Close
updateFields=UpdateOrderFields()
updateFields.StopPrice=self.Securities[self.symbol].Close*self.trailingStopRisk
self.stopMarketTicket.Update(updateFields)
self.Debug(updateFields.StopPrice)
self.Plot("Data Chart", "Stop Price", self.stopMarketTicket.Get(OrderField.StopPrice))
I get the error when the scheduler is executed, but I have no clue why do I get it.
Thank you in advance for your help and support!
Cole S
self.History wants an Integer for number of periods to lookback. You are passing in self.lookback and it looks like it would be a float. Try int(self.lookback) and that will most likely solve the issue.
Hunsnowboarder02
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!