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/

    BT Multiple timers ?

    General Code/Help
    2
    3
    145
    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.
    • C
      chewbacca last edited by

      Are multiple notify_timer() functions possible in BT?
      If yes - how ?

      1 Reply Last reply Reply Quote 0
      • C
        chewbacca last edited by

        I mean - in the same strategy.

        1 Reply Last reply Reply Quote 0
        • T
          techydoc last edited by

          I am new to backtrader but this seems to work for me.

          I added two timers to my strategy.

          '''
          # Timer to support rebalancing weekcarry over in case of holiday
          self.add_timer(
          name = "rebalance",
          when=bt.Timer.SESSION_START,
          weekdays=[self.p.rebal_weekday],
          weekcarry=True, # if a day isn't there, execute on the next
          )
          self.add_timer(
          name = "reposition",
          when=bt.Timer.SESSION_START,
          allow=RepositionTimer(),
          weekcarry=True, # if a day isn't there, execute on the next
          )
          '''

          I added a parameter called "name." I can then check kwargs in notify_timer to determine which timer was sent to the handler.

          '''
          def notify_timer(self, timer, when, *args, **kwargs):
          if kwargs['name'] == 'rebalance':
          print("Rebalanceing at {}".format(when))
          self.rebalance_portfolio()
          if kwargs['name'] == 'reposition':
          print("Repositioning at {}".format(when))
          self.rebalance_positions()
          print("Unknown Timer at {}".format(when))
          '''

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