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/

    Getting executed time - Float to Datetime

    General Discussion
    2
    3
    2303
    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.
    • O
      Osofuego last edited by Osofuego

      Hi. I am trying to debug my execution times and am getting a weird float output when I use notify_order. I am trying to convert the float of 'order.executed.dt' to datetime to print a string.

      My question is how to convert this float to readable string, as i am getting weird errors. When I print the float as is I get '735480.4798611111' which equates to Sept 1970. The data I am using is 8/1/2014 10:31:00 AM.

      What am I doing wrong here?

      Here is the code

      
      def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enougth cash
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, Datetime: %s' %
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm,
                          datetime.datetime.strftime(datetime.datetime.fromtimestamp(order.executed.dt),'%Y-%m-%d %H:%M:%S')))
      
      1 Reply Last reply Reply Quote 2
      • B
        backtrader administrators last edited by

        The float is a timestamp as defined by matplotlib

        See for example here: Docs - Evaluating external historical performance for how to convert it with self.data.num2date.

        Using the method of the data feed guarantees appropriate timezone conversions. If you are using no timezones, you can simply use backtrader.num2date

        1 Reply Last reply Reply Quote 1
        • O
          Osofuego last edited by

          Solved. Thank you so much for the really quick reply!

          Here is the code for reference for anyone else.

          self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, Datetime: %s' %
                                  (order.executed.price,
                                   order.executed.value,
                                   order.executed.comm,
                                   bt.num2date(order.executed.dt)))
          
          
          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors