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 problem

    General Discussion
    2
    3
    1115
    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.
    • J
      junajo10 last edited by backtrader

      Hi,

      I would like to add a new timer when a timer begins and eliminate when the first timer finishes. is it possible?

      def buildTimers(self):
      #Start trading
      timers={}
      timerStart=self.add_timer(
      when=self.p.start,
      offset=self.p.offset,
      repeat=datetime.timedelta(seconds=360),
      weekdays=self.p.weekdays,
      )
      timers['timerStart']=timerStart.p.tid

      def notify_timer(self, timer, when, *args, **kwargs):
      for key, value in self.timers.items():
      if value==timer.p.tid and key=='timerStart':

      timerDimingFrequency=self.add_timer(
      when=self.p.start,
      offset=self.p.offset,
      repeat=datetime.timedelta(seconds=10),
      weekdays=self.p.weekdays,
      )

      I am not receiving the notification of the second timer, do you know what is the problem?

      Rgds,

      JJ

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

        Without the formatting it is difficult to see what the code is actually doing, but in any case the timers are installed during the initialization phase.

        Adding timers later is not supported.

        1 Reply Last reply Reply Quote 0
        • J
          junajo10 last edited by junajo10

          ok, thanks, i will wait impatiently this feature:

          class St(bt.Strategy):
          
            def buildTimers(self):
              #Start trading
              timers={}
              timerStart=self.add_timer(
                      when=self.p.start,
                      offset=self.p.offset,
                      repeat=self.p.repeat,
                      weekdays=self.p.weekdays,
                  )
              timers['timerStart']=timerStart.p.tid
             
              #Timer Bin Size
              timerBinSize=self.add_timer(
                      when=self.p.start,
                      offset=self.p.offset,
                      repeat=datetime.timedelta(seconds=self.bin_size),
                      weekdays=self.p.weekdays,
                  )
              timers['timerBinSize']=timerBinSize.p.tid
              #Start trading
              timerEnd=self.add_timer(
                      when=self.p.end,
                      offset=self.p.offset,
                      repeat=self.p.repeat,
                      weekdays=self.p.weekdays,
              )
              timers['timerEnd']=timerEnd.p.tid
              return timers
          
            def __init__(self):
              self.bin_size=self.p.bin_size
              self.limit_orders = []
              self.order_book=None
              self.timers=self.buildTimers()
          
           def build_timer2(self):
              timerDimingFrequency=self.add_timer(
                      when=self.p.start,
                      offset=self.p.offset,
                      repeat=datetime.timedelta(seconds=self.bin_size),
                      weekdays=self.p.weekdays,
                  )
              self.timers['timerDimingFrequency']=timerDimingFrequency.p.tid
            
           def notify_timer(self, timer, when, *args, **kwargs):        
              
              for key, value in self.timers.copy().items():
                   print(key,value)
                   if value==timer.p.tid:
                          if key=='timerStart':
                              print('strategy starts '+str(when) )
                          elif key=='timerBinSize':
                              print('bin starts ' +str(when) )
                              self.build_timer2()
                          elif key=='timerEnd':
          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(); }); }