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/

    Best approach to limit strategy to certain times of day?

    General Discussion
    3
    5
    152
    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.
    • B
      benmercerdev last edited by

      I'd like to limit my strategy/backtesting not only to certain days of data, but also to certain times during those days, for example +/- 30 minutes of BOD and EOD.

      Can anyone recommend the best approach to this?

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        You might wish to try using this library DateTimeRange.

        from datetimerange import DateTimeRange
        

        Set the current date and time:

        # Current bar datetime, date, and time.
        dt = self.data.datetime.datetime()
        date = self.data.datetime.date()
        

        Set the regular trading hours (rth) as:

        # str HH:MM:SS
        self.p.rth_start
        self.p.rth_end
        

        Then use this code to set the trading time range:

         # Create Regular Trading Hours range for the current trading day.
                rth = DateTimeRange(
                    datetime.combine(
                        date, datetime.strptime(self.p.rth_start, "%H:%M:%S").time()
                    ),
                    datetime.combine(
                        date, datetime.strptime(self.p.rth_end, "%H:%M:%S").time()
                    ),
                )
        

        Then check if the datetime is in regular trading hours. If so, then 'return'.

        # Only trade in regular trading hours and intraday bars.
                if dt not in rth and self.p.time_frequency != 'D':
                    return
        
        1 Reply Last reply Reply Quote 2
        • vladisld
          vladisld last edited by

          You may also utilize the trading calendars:

              def in_session(data_feed, sos_delta=timedelta(), eos_delta=timedelta()):
                  dt = data_feed.num2date(tz=timezone.utc, naive=False).replace(tzinfo=None)
                  (sos, eos) = data_feed._calendar.schedule(datetime.combine(dt.date(), time()))
          
                  if eos_delta:
                      eos -= delta
          
                  if sos_delta:
                      sos += delta
          
                  return sos <= dt <= eos
          
          1 Reply Last reply Reply Quote 1
          • B
            benmercerdev last edited by

            Excellent, thanks, guys. Let me give these a go.

            1 Reply Last reply Reply Quote 0
            • B
              benmercerdev last edited by

              I ended up using a version of @run-out 's suggestion without the datetimerange, using two custom trade hour params with corresponding params in my strategy

              cth_start = datetime.datetime.strptime(args.cth_start, '%H:%M:%S').time()
              cth_end = datetime.datetime.strptime(args.cth_end, '%H:%M:%S').time()
              

              and then within my strategy simply:

              def next(self):
                          if (self.data.datetime.time() < self.p.cth_start) or (self.data.datetime.time() > self.p.cth_end):
                              return
              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }