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/

    Timers

    Blog
    3
    9
    1310
    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.
    • B
      backtrader administrators last edited by

      Use the link below to go the original post

      Click here to see the full blog post

      1 Reply Last reply Reply Quote 0
      • RandyT
        RandyT last edited by

        Timers look like a great addition and should solve some of the challenges had in the past. Great work.

        1 Reply Last reply Reply Quote 0
        • benjomeyer
          benjomeyer last edited by

          I have a question about timers. I want to create two timers, one to initiate some sell activities on a Monday and another to initiate buy activities on a Wednesday. I understand how to create two timers, but how do I associate each timer with a different activity/function?

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Timers go to notify_timer which receives the timer, when it is happening and any extra *args and **kwargs you may created the timer with. You can use any of the latter to differentiate timers.

            Or you can simply use the timer id to associate different logic to each timer.

            Docs - Timers

            1 Reply Last reply Reply Quote 0
            • benjomeyer
              benjomeyer last edited by

              OK, so I tried this:

                  self.add_timer(
                      when=bt.Timer.SESSION_START,
                      weekdays=[self.params.buy_day],
                      weekcarry=True,
                      timername='buytimer',
                      )
              
                  self.add_timer(
                      when=bt.Timer.SESSION_START,
                      weekdays=[self.params.sell_day],
                      weekcarry=True,
                      timername='selltimer',
                      )
              

              and then referred to these timers here:

              def notify_timer1(self, timer1, when, *args, **kwargs):
                  if timername=='buytimer':
                      self.buy_stocks()
              
              def notify_timer2(self, timer2, when, *args, **kwargs):
                  if timername=='selltimer':
                      self.sell_stocks()
              

              Is this what you mean? Something's not quite working.

              benjomeyer B 2 Replies Last reply Reply Quote 0
              • benjomeyer
                benjomeyer @benjomeyer last edited by

                The timers reference had some obvious errors, it's meant to be this:

                def notify_timer(self, timer, when, *args, **kwargs):
                    if timername=='buytimer':
                        self.buy_stocks()
                    if timername=='selltimer':
                        self.sell_stocks()
                benjomeyer 1 Reply Last reply Reply Quote 0
                • benjomeyer
                  benjomeyer @benjomeyer last edited by

                  But it's still not working. Any ideas?

                  1 Reply Last reply Reply Quote 0
                  • benjomeyer
                    benjomeyer last edited by

                    Got it sorted! Thanks.

                    def notify_timer(self, timer, when, timername, *args, **kwargs):
                        if timername=='buytimer':
                            self.buy_stocks()
                        if timername=='selltimer':
                            self.sell_stocks()
                    1 Reply Last reply Reply Quote 0
                    • B
                      backtrader administrators @benjomeyer last edited by

                      @benjomeyer said in Timers:

                              timername='selltimer',
                      

                      Given how you pass it ... you could probably try this better

                      def notify_timer(self, timer, when, timername):
                          if timername=='buytimer':
                              self.buy_stocks()
                          elif timername=='selltimer':
                              self.sell_stocks()
                      

                      given that you have no additional *args or **kwargs. But a more general approach I would suggest this

                      def notify_timer(self, timer, when, **kwargs):
                          timername = kwargs.get('timername', None)
                          if timername=='buytimer':
                              self.buy_stocks()
                          elif timername=='selltimer':
                              self.sell_stocks()
                      

                      where you can also decide if a default logic for None is needed.

                      Note: notice the usage of elif

                      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(); }); }