logging timestamp produces an entirely different number
hi, i'm still pretty new to using backtrader and haven't really gotten the grasp of it. I've been trying to log my order executions but the log time is always completely off. My data was fed in using this code
fromdate = datetime.datetime.strptime('2020-1-01', '%Y-%m-%d') todate = datetime.datetime.strptime('2021-01-08', '%Y-%m-%d') data = bt.feeds.GenericCSVData( dataname='2021_hourly.csv', dtformat=2, compression=30, timeframe=bt.TimeFrame.Minutes, fromdate=fromdate, todate=todate) cerebro.adddata(data)
and my data looks something like this. The first column corresponds to the datetime for each row
but somehow when i try to log with the following code
def log(self, txt, dt=None): ''' logging function for this strat '' dt = datetime.datetime.fromtimestamp(self.data.datetime) print('%s, %s' % (dt, txt)) print('') def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: return if order.status in [order.Completed, order.Canceled, order.Margin]: if order.isbuy(): self.log('Buy Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: self.log('Sell Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.bar_executed = len(self) self.order = None
it produces an output like this in which the dates and time are completely off
if i change dt to the following instead
dt = self.data.datetime
the output will produce the date as this number which is completely different from the unix datetime fed into cerebro
could anyone help identify what the problem is? would really appreciate it, thanks!
dt = datetime.datetime.fromtimestamp(self.data.datetime)
your print log is converting the timestamp which is the issue here. Give it a try with following
def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas.datetime.date(0) print('%s, %s' % (dt.isoformat(), txt))
dt = dt or self.datas.datetime.date(0)
thank you so much for the help, it does work now!