live trading: notify_timer is not called if timer is set after eos
I need to get a timer notification after a trading session ends (including after-hours trading) - let's say 5 min after eos. Unfortunately such timers are not fired until the next session begins (or at all)
Here is the code used to set such a timer (in strategy ctor):
(_, eos) = self.data._calendar.schedule(datetime.combine(date.today(), time())) offset = timedelta(hours=4, minutes=5) if self.p.outside_rth else timedelta(minutes=5) self.add_timer(when=eos.time(), offset=offset, tzdata=self.data)
(it doesn't matter if the absolute time is used as in the above code or a Timer.SESSION_END constant)
It seems the problem is that the timers are only checked if at least one data feed produces new bar inside `celebro._runnext' method.
def _runnext(self, runstrats): ... ... if d0ret or lastret: # if any bar, check timers before broker self._check_timers(runstrats, dt0, cheat=True) if self.p.cheat_on_open: for strat in runstrats: strat._next_open() if self._event_stop: # stop if requested return ... ...
However, after trading session - no data feed produces any bars, which means that _check_timers will not be called and no
notify_timernotification will be fired.
Am I correct ?
Any suggestions how to overcome that without touching the backtrader code ?
After looking at it a little bit more, the problem is not only that
self._check_timersmethod is not called anymore if no data feed produces any bars.
Even if it could be called, the timestamp (stored in
dt0variable) passed to the
self._check_timerscall will be the same as the last timestamp returned by the data feed.
This timestamp would also be frozen in case the data feed has reached the end of trading session - so it doesn't make any sense to call the
self._check_timersat all in this case.
Currently I don't see any elegant solution for this - so any advise would be appreciated.
Am I the only one facing this issue? Or am I missing something obvious?
I've opened a bug issue in my fork ( issues are closed in the official repository ) if somebody would like to discuss the solution outside the forum :
The proposal is to introduce the new type of Timer that will use the real-time clock and could be scheduled using 'crontab' syntax to be used in live trading scenarios. There is a prototype implemented in my fork ( still not final) - open to discussion of cause: