For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

line info lost during resampling

  • 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 = 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 and, but still not sure which part I should modify to get a correct resampling of the custom line I added.
    Any hints/idea?


  • administrators

    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.

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

            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)):
                        retff = ff(self, *fargs, **fkwargs)
                    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?

Log in to reply