GenericCSVData does not take time, only takes date
-
import backtrader as bt
class Database:
def __init__(self, timeStamp): self.timeStamp= timeStamp.upper() def getBtCSVdata(self): ''' This method will return backtrader CSV data to feed directly ''' fn = 'D://Data//your_csv_file.csv' if self.timeStamp == 'DAY': # For daily data dateformat = '%Y-%m-%d' timeformat = None else: # For 1min or 5min or 15min or 30min data dateformat = '%Y-%m-%d %H:%M:%S' timeformat = '%H:%M:%S' btCSVData = bt.feeds.GenericCSVData(dataname=fn, header = False, dtformat = dateformat, tmformat = timeformat, nullvalue = 0.0, datetime = 0, open = 1, high = 2, low = 3, close = 4, volume = 5, openinterest = -1) return btCSVData
#==========================================================
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.datetime(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0])
if name == 'main':
cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) # Create a Data Feed ds = Database('1min') #ds.updateFromAlphavantage() data1 = ds.getBtCSVdata() # Add the Data Feed to Cerebro cerebro.adddata(data1) # Set our desired cash start cerebro.broker.setcash(100000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
#=================================
My CSV data are as follows:2019-03-13 15:25:00,148.70,148.80,148.70,148.70,92180,0.00
2019-03-13 15:26:00,148.55,148.65,148.55,148.60,102998,0.00
2019-03-13 15:27:00,148.55,148.60,148.45,148.45,98636,0.00
2019-03-13 15:28:00,148.50,148.60,148.50,148.55,73246,0.00
2019-03-13 15:29:00,148.55,148.75,148.45,148.75,75646,0.00
#=======================================
The output of backtrader is follows:Starting Portfolio Value: 100000.00
2019-03-13T23:59:59.999989, Close, 148.60
2019-03-13T23:59:59.999989, Close, 148.45
2019-03-13T23:59:59.999989, Close, 148.55
2019-03-13T23:59:59.999989, Close, 148.75
Final Portfolio Value: 100000.00
#====================
How do I import the CORRECT time data in my programme? -
You need to specify
timeframe
andcompression
for data feed added. -
Thank you @ab_trader very much to help me.
-
@ab_trader where do you add these parameters?
-
Have a look here.
-
@run-out great, thank you.
-
@benmercerdev I had same problem. I solved it by following the suggestion of ab_trader. Now my custom csv method looks like:
class my_custom_csv(bt.feeds.GenericCSVData): params = ( ('dtformat', '%Y-%m-%d %H:%M:%S'), ... ('timeframe', bt.TimeFrame.Minutes), ('compression', 1), )
Adding timeframe and compression solved it for me.
-
@P-Jr-G these parameters are already in the data feed class, no need to add them one more time when you define your own data feed class. You just need to call your data feed as follows:
data = my_custom_csv(path_to_data_file, timeframe=bt.TimeFrame.YourTimeFrame_here)