BT Multiple timers ?
-
Are multiple notify_timer() functions possible in BT?
If yes - how ? -
I mean - in the same strategy.
-
I am new to backtrader but this seems to work for me.
I added two timers to my strategy.
'''
# Timer to support rebalancing weekcarry over in case of holiday
self.add_timer(
name = "rebalance",
when=bt.Timer.SESSION_START,
weekdays=[self.p.rebal_weekday],
weekcarry=True, # if a day isn't there, execute on the next
)
self.add_timer(
name = "reposition",
when=bt.Timer.SESSION_START,
allow=RepositionTimer(),
weekcarry=True, # if a day isn't there, execute on the next
)
'''I added a parameter called "name." I can then check kwargs in notify_timer to determine which timer was sent to the handler.
'''
def notify_timer(self, timer, when, *args, **kwargs):
if kwargs['name'] == 'rebalance':
print("Rebalanceing at {}".format(when))
self.rebalance_portfolio()
if kwargs['name'] == 'reposition':
print("Repositioning at {}".format(when))
self.rebalance_positions()
print("Unknown Timer at {}".format(when))
'''