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 = 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


  • 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.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.