For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Problem with DateTime format
-
Hi,
I'm just getting into the use of backtrader and I'm struggling to get my DateTime format to work. I want to write a python script that prints all closing values of for example the 5 minute chart on a given day.
The date and time in the csv file that I load are in "2020-11-02 22:30:00" format. Whenever I run the program dates work just fine, however time is always stuck on the same value:
I have written the following code:
import backtrader as bt import datetime class PrintClose(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close def log(self, txt, dt=None): dt = self.datas[0].datetime.datetime() print(f'{dt.isoformat()} {txt}') #Print date and close def next(self): self.log('Close: ', self.dataclose[0]) #Instantiate Cerebro engine cerebro = bt.Cerebro() data = bt.feeds.GenericCSVData( dataname='BTCUSD_PERP-5m-2020-11_Corrected.csv', fromdate=datetime.datetime(2020, 11, 1), todate=datetime.datetime(2020, 11, 2), nullvalue=0.0, dtformat=("%Y-%m-%d %H:%M:%S"), timeformat=("%H:%M:%S"), time=-1, datetime=0, high=2, low=3, open=1, close=4, volume=5, openinterest=-1 ) cerebro.adddata(data) cerebro.addstrategy(PrintClose) cerebro.run()
-
@giebels try to specify the correct timeframe and compression for you data feed. If not specified, it defaults to daily prices, and it seems your data are in minutes time format.
-
Thanks for the help! That solved the problem.