Reputation: 11
Please help with this error, this is the error I am getting while feeding my own csv data to the backtrader.
data sample is as follows
I am new to python and the python community
error:
C:\Python\python.exe C:\Users\harif\PycharmProjects\untitled\venv\learning\dataframe1.py Traceback (most recent call last): File "C:\Users\harif\PycharmProjects\untitled\venv\learning\dataframe1.py", line 32, in fromdate=datetime(2, 10, 2016), ValueError: day is out of range for month
Process finished with exit code 1
import backtrader as bt
# Create a subclass of Strategy to define the indicators and logic
class SmaCross(bt.Strategy):
# list of parameters which are configurable for the strategy
params = dict(
pfast=10, # period for the fast moving average
pslow=30 # period for the slow moving average
)
def __init__(self):
sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
def next(self):
if not self.position: # not in the market
if self.crossover > 0: # if fast crosses slow to the upside
self.buy() # enter long
elif self.crossover < 0: # in the market & cross to the downside
self.close() # close long position
cerebro = bt.Cerebro() # create a "Cerebro" engine instance
# Create a data feed
data = bt.feeds.GenericCSVData(dataname=r'C:\Hard_Drive\Tick_Data\tickdata_2\sp.csv',
fromdate=datetime(2, 10, 2016),
todate=datetime(1, 10, 2019))
print(data)
# cerebro.adddata(data) # Add the data feed
# cerebro.addstrategy(SmaCross) # Add the trading strategy
# cerebro.run() # run it all
# cerebro.plot() # and plot it with a single command
Upvotes: 1
Views: 368
Reputation: 1384
Date time object accepts values in this order.
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]
There are 2 ways of fixing this.
datetime(2016, 10, 2)
datetime(day=2, month=10, year=2016),
Upvotes: 1