Timers problem



  • 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


  • administrators

    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.



  • 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':

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.