Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

live trading: notify_timer is not called if timer is set after eos #1

Open
vladisld opened this issue Mar 25, 2020 · 1 comment
Open
Labels
bug Something isn't working

Comments

@vladisld
Copy link
Owner

vladisld commented Mar 25, 2020

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_timer notification will be fired.

After looking at it a little bit more, the problem is not only that self._check_timers method is not called anymore if no data feed produces any bars.

Even if it could be called, the timestamp (stored in dt0 variable) passed to the self._check_timers call 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_timers at all in this case.

@vladisld vladisld added the bug Something isn't working label Mar 25, 2020
@vladisld vladisld changed the title ive trading: notify_timer is not called if timer is set after eos live trading: notify_timer is not called if timer is set after eos Mar 25, 2020
@vladisld
Copy link
Owner Author

it seems the original timer implementation was designed to be used specifically for backtrading, while the live trading capabilities were added later.

However the current implementation is not fully suitable for live trading scenarios, since it fully depends on the time values provided by the data feed.

The proposed solution is to introduce another type of Timer class which will be used only for live trading scenarios and will use a real world time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant