Timers
-
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?
-
Timers
go tonotify_timer
which receives thetimer
,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. -
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()
-
@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 thisdef 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
-
This post is deleted!