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 theCerebro.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 theoutput = 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 NaNI 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
-
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 deliveryhigh
andlow
are constanly updated if exceeded until delivery (max
andmin
)close
is always updated until deliveryvolume
starts at0
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. - How do you resample the
-
@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.