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


  • administrators

    Use the link below to go the original post

    Click here to see the full blog post



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



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


  • administrators

    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



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



  • 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()


  • But it's still not working. Any ideas?



  • 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()

  • administrators

    @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


Log in to reply
 

});