Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

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

    General Code/Help
    1
    4
    397
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • vladisld
      vladisld last edited by

      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.

      Am I correct ?
      Any suggestions how to overcome that without touching the backtrader code ?

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        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.

        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?

        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld last edited by

          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 :

          https://github.com/vladisld/backtrader/issues/1

          1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld last edited by

            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:

            https://github.com/vladisld/backtrader/commit/d3617d9df273719fa4cb8ecc7061d3b8123fb4db

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors