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/

    line info lost during resampling

    General Discussion
    3
    3
    788
    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.
    • J
      JohnShiu last edited by JohnShiu

      Hi,
      I start using backtrader recently and have nice experience with it. This is the best package I found so far for doing backtest.
      However, I have got a problem when using the Cerebro.resampledata

      First of all, I wrote a customized pandasdata class, in which I added a field "code", to fit the data format in my database:

      class PandasData_Customize(feeds.PandasData):
          params = (("code", "code"), )
          lines = ("code", )
          datafields = [............., "code"]
      
      

      The "code" would be something like ES, DX, etc.... It is supposed to be unique for an asset, i.e. data.code is unique for each data in self.datas

      It has been working well (daily data) with Cerebro.adddata(PandasData_Customize(dataname=df))
      The data.code from the output = Cerebro.run() are correct.

      Recently, I am trying to backtest using monthly time frame, with:
      Cerebro.resampledata(PandasData_Customize(dataname=df), timeframe=TimeFrame.Months)
      But the data.code is always NaN

      I tried to find hints from the "Resampling" session in the doc, but it seems the case for customized datafeeds is not mentioned yet.
      I also read some source codes from the cerebro.py and resamplerfilter.py, but still not sure which part I should modify to get a correct resampling of the custom line I added.
      Any hints/idea?

      Thanks

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

        The problem is:

        • How do you resample the code?

        See the usual values:

        • open it is only taken 1 at the beginning of a resample cycle and never changed again until delivery
        • high and low are constanly updated if exceeded until delivery (max and min)
        • close is always updated until delivery
        • volume starts at 0 and is incremented with the incoming data

        The platform cannot know what to do with an unknown value and therefore nothing is done. The given line doesn't exist for the resampler.

        Your option:

        • Implement a filter which is plugged into the resampled instance, looks into the original data feed and resamples code with your desired policy.

          See: Docs - Filters

        Hint: the resampled data instance contains a data attribute which points to the original non-resampled data instance.

        M 1 Reply Last reply Reply Quote 0
        • M
          malro @backtrader last edited by

          @backtrader it seems that doesn't work now. Custom filter should be added after Resampler filter which always return True.
          From the sources of feed.py:

                  for ff, fargs, fkwargs in self._filters:
                      # previous filter may have put things onto the stack
                      if self._barstack:
                          for i in range(len(self._barstack)):
                              self._fromstack(forward=True)
                              retff = ff(self, *fargs, **fkwargs)
                      else:
                          retff = ff(self, *fargs, **fkwargs)
          
                      if retff:  # bar removed from systemn
                          break  # out of the inner loop
          

          And that means that no filters will be executed after Resampler.
          So, what is the right way to resample custom lines?
          Thanks.

          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(); }); }