1

I'm trying to import some data from a .csv file into backtrader but hitting the error ValueError: year x is out of range.

Here is a line from the csv that I'm looking to import

1577836800000,7195.24000000,7255.00000000,7175.15000000,7200.85000000,16792.38816500,1577923199999,121214452.11606228,194010,8946.95553500,64597785.21233434,0

and here is the code to ingest the data into backtrader and plot using matplotlib:

import backtrader as bt
import matplotlib

class RSIStrategy(bt.Strategy):

    def __init__(self):
        self.rsi = bt.talib.RSI(self.data, period=14)

    def next(self):
        if self.rsi < 30 and not self.position:
           self.buy(size=1)

        if self.rsi > 70 and self.position:
            self.close()

cerebro = bt.Cerebro()

data = bt.feeds.GenericCSVData (dataname= 'daily_2021.csv', dtformat=2)

cerebro.adddata(data)

cerebro.addstrategy(RSIStrategy)

cerebro.run()

cerebro.plot()

daily_2021.csv is the source of the data.

I've seen mentioned it elsewhere that it maybe due to how unix timestamp works and that I would need to /1000 the date but not sure how.

Hope someone can help.

1 Answers1

1

I missed a line in a script to retrieve the data and put it into the CSV that was causing the error

for candlestick in candlesticks:
    candlestick[0] = candlestick[0] / 1000
    candlestick_writer.writerow(candlestick)

The above corrected the error