Example about filter method of the data feed please!
-
Hello,
When working with a different data format, it includes some extra information: delta, and momentum_power besides traditional data: open, high, low, close and volume. By extending pandas data feed, it is easy to add extra information to cerebro.
The issue happens when resampling or replaying data to higher time frame, the delta and momentum_power data becomes NAN.
I read on the document: https://www.backtrader.com/docu/filters/
Filters at work
Given an existing data feed/source you use the addfilter method of the data feed:data = MyDataFeed(dataname=myname)
data.addfilter(filter, *args, **kwargs)
cerebro.addata(data)And even if it happens to be compatible to the resample/replay filter the following can also be done:
data = MyDataFeed(dataname=myname)
data.addfilter(filter, *args, **kwargs)
cerebro.replaydata(data)Filter Interface
A filter must conform to a given interface, being this:A callable which accepts this signature:
callable(data, *args, **kwargs)
I like this simple interface, because it is not complicated case. However, I'm not family with this define syntax. Is there any simple example for reference?
For example: I want sum/add(delta) and average/mean(momentum_power). How to define the callable interface for this case?
Thank you very much!
-
Got it. It looks like example below the document:
def simple_filter(data):
Or take a deep look at dataseries.py
Thank you!
-
By the way, backtrader had simple filter function: addfilter_simple, but there is not any document to use it. Will this function be helpful in the case: sum(field) when resample data?
-
@quỳnh-h-nguyễn Probably the following post could be relevant:
https://community.backtrader.com/topic/2812/resample-for-extra-information-in-lines -
Thanks @vladisld so much!
I read your guide very carefully. And I think there is only one way to do:
- Re-implementing _Bar class.
- Re-implementing Resample/Replay class to use new _Bar class.
- Then re-implementing resample/replay in the feed class to use customized Resample/Replay.