Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    logging timestamp produces an entirely different number

    General Code/Help
    2
    3
    30
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kicgahi last edited by

      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

      Screenshot 2021-01-10 at 3.17.28 AM.png

      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[0])
              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
      Screenshot 2021-01-10 at 3.20.10 AM.png

      if i change dt to the following instead

      dt = self.data.datetime[0]
      

      the output will produce the date as this number which is completely different from the unix datetime fed into cerebroScreenshot 2021-01-10 at 3.22.52 AM.png

      could anyone help identify what the problem is? would really appreciate it, thanks!

      1 Reply Last reply Reply Quote 0
      • R
        rajanprabu last edited by

        @kicgahi said in logging timestamp produces an entirely different number:

        dt = datetime.datetime.fromtimestamp(self.data.datetime[0])

        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[0].datetime.date(0)
                print('%s, %s' % (dt.isoformat(), txt))
        
        
        K 1 Reply Last reply Reply Quote 0
        • K
          kicgahi @rajanprabu last edited by

          @rajanprabu said in logging timestamp produces an entirely different number:

          dt = dt or self.datas[0].datetime.date(0)

          thank you so much for the help, it does work now!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }