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/

    Potential bug in resampler

    General Discussion
    2
    5
    1020
    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.
    • M
      mpskowron last edited by

      Not sure about it yet, but seems like resampler doesn't work for daily timeframe (still investigating if it is sth my code is doing or backtrader bug). Not sure if this is the cause yet, but I think I found a bug in resamplefilter.py::_BaseResampler::init():
      Seems like self.componly can never be True:

       self.componly = (self.subdays and
                               data._timeframe == self.p.timeframe and
                               not (self.p.compression % data._compression)
                               )
      ...
      self.componly = self.componly and not self.subdays
      
      1 Reply Last reply Reply Quote 0
      • M
        mpskowron last edited by

        Did some more investigation about it and found out that indeed it is a bug - resampler just don't work at all for timeframe == days. To fix:
        The code snippet I placed in previous message:

         self.componly = (self.subdays and
                                 data._timeframe == self.p.timeframe and
                                 not (self.p.compression % data._compression)
                                 )
        ...
        self.componly = self.componly and not self.subdays
        

        should be changed to (add 'not' before self.subdays and remove the last line):

        self.componly = (not self.subdays and
                                 data._timeframe == self.p.timeframe and
                                 not (self.p.compression % data._compression)
                                 )
        

        Additionally in resamplefilter.py::Resampler::call() change this:

                if consumed:
                    self.bar.bupdate(data)  # update new or existing bar
                    data.backwards()  # remove used bar
        
                # if self.bar.isopen and (onedge or (docheckover and checkbarover))
                cond = self.bar.isopen()
                if cond:  # original is and, the 2nd term must also be true
                    if not onedge:  # onedge true is sufficient
                        if docheckover:
                            cond = self._checkbarover(data, fromcheck=fromcheck,
                                                      forcedata=forcedata)
        

        to:

                if consumed:
                    self.bar.bupdate(data)  # update new or existing bar
        
                # if self.bar.isopen and (onedge or (docheckover and checkbarover))
                cond = self.bar.isopen()
                if cond:  # original is and, the 2nd term must also be true
                    if not onedge:  # onedge true is sufficient
                        if docheckover:
                            cond = self._checkbarover(data, fromcheck=fromcheck,
                                                      forcedata=forcedata)
                if consumed:
                    data.backwards()  # remove used bar
        

        This is just a quick fix I came up with, it can probably be fixed in a much prettier way.

        B 1 Reply Last reply Reply Quote 1
        • M
          mpskowron last edited by

          Next bug related to resampling, I found it while I tried to resample with time_frame = TimeFrame.Minutes with data compression=60 and target compression = 60*24. Problem is located in bactrader.analyzer._get_subday_cmpkey().
          Lines:

                  dtcmp = dt.replace(hour=ph, minute=pm, second=ps, microsecond=pus)
                  dtcmp -= tadjust
                  if extradays:
                      dt += datetime.timedelta(days=extradays)
                  dtkey = dtcmp
          
                  return dtcmp, dtkey
          

          should be changed to:

                  if extradays:
                      dt += datetime.timedelta(days=extradays)
                  dtcmp = dt.replace(hour=ph, minute=pm, second=ps, microsecond=pus)
                  dtcmp -= tadjust
                  dtkey = dtcmp
          
                  return dtcmp, dtkey
          

          Basically, the extradays adjustment was done too late.

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @mpskowron last edited by

            @mpskowron said in Potential bug in resampler:

            This is just a quick fix I came up with, it can probably be fixed in a much prettier way.

            Better late than never. The fix for the compression only behavior of days (or weeks for example) is good. Alternate implementation aren't any better.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Available at: https://github.com/backtrader/backtrader/commit/b8dda02d83bb4bc1bef9b4b37bbca2c0062d7123 (development branch)

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }