Backtrader Community

    • 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/

    Date and time handling in the __init()__

    General Code/Help
    date-time warmup
    2
    4
    1481
    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.
    • A
      ab_trader last edited by ab_trader

      Hi @backtrader

      Could you please advice the way of date & time handling using __init()__. Say I have data feed and want to have the following indicator values along the history:

      from the data beginning to 2000/12/31 it should be 1.0
      after 2001/01/01 it should be 2.0

      I've developed an indicator using next(). It simply compares last data feed dates and dates shown, and set values accordingly . Is there a way to make the same thing in the __init()__? It seems to me that it might be faster.

      • If my answer helped, hit reputation up arrow at lower right corner of the post.
      • Python Debugging With Pdb
      • New to python and bt - check this out
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by backtrader

        Comparison is implemented for datetime.time instances and lines, but not for datetime.datetime or datetime.date, which forbids a clean implementation of that idea with something like:

        def __init__(self):
            self.lines.myline = 1 + (self.data.datetime >= datetime.date(2001, 1, 1))
        

        But there is something which can be done even if not so elegant

        def __init__(self):
            self.lines.myline = 1 + (self.data.datetime >= date2num(datetime.date(2001, 1, 1)))
        

        Instead of directly comparing with the datetime.date instance, the comparison is done against the numeric value.

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          @backtrader Thank you!

          I've used bt.date2num() function for transformation. Also I've found out that you rewrote if operator which can be applied to lines. That helped me also.

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Edited the snippet from above to reflect that the correct usage is with date2num (obviously)

            Of course using the If from backtrader allows for much more complex scenarios. The 1 + boolean in the snippet was just meant to solve the hypothetical case presented above (1 until date x, 2 from that point onwards)

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors