I am writing a test algo in Python and after making some changes in another function, my history call (Last line of my code below) stopped working all of a sudden. I get the error:
BacktestingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: 12. The error was NameError : name 'timedelta' is not defined
I am not a programmer just a hobbiest but I can't figure out why this error would be happening all of sudden?
Here is my code.
import numpy as np
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
#some Globals
#stock="SPY"
#bond="BNDX"
#setting our benchmark aginst the SPY Ticker
self.SetBenchmark("SPY")
#Set Dates for backtesting
self.SetStartDate(2019,1,1) #Set Start Date
self.SetEndDate(2019,1,10) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
# Not sure if these need to be assigned to the variables or not but it started working once I assigned them to variables? They aren't referenced anywhere else.
stockEquity=self.AddEquity("SPY", Resolution.Daily)
#bondEquity=self.AddEquity(bond, Resolution.Daily)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(12,0), self.HelloWorldDaily)
def OnData(self, data):
pass
def HelloWorldDaily(self):
slices = self.History(["SPY"], timedelta(7))
Basher
You'll want to import timedelta beforehand:
from datetime import timedelta
Recommend going through some example algos on github.
Keith
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!