1. 程式人生 > 實用技巧 >Backtrader中文筆記之 Feed Data - Filters

Backtrader中文筆記之 Feed Data - Filters

Filters

This functionality is a relatively late addition to backtrader and had to be fitted to the already existing internals. This makes it to be not as flexible and 100% feature full as wished, but it can still serve the purpose in many cases.

這個功能是對backtrader的一個相對較晚的新增,現在已經安裝到內部。這使得它不能像希望的那樣靈活和100%的功能,但在許多情況下它仍然可以達到目的。

Although the implementation tried to allow plug and play filter chaining, the pre-existing internals made it difficult to ensure that could always be achieved. As such, some filters may be chained and some others may not.

儘管該實現嘗試允許即插即用過濾器連結,但由於預先存在的內部結構,很難確保始終能夠實現這一點。因此,一些過濾器可能是連鎖的,而另一些則不是。

Purpose

目的

      • Transform the values provided by a data feed to deliver a different data feed

The implementation was started to simplify the implementation of the two obvious filters which can be directly used via the cerebro API. These are:

    • Resampling (cerebro.resampledata)

      Here the filter transforms the timeframe

      and compression of the incoming data feed. For example:

  • (Seconds, 1) -> (Days, 1)
    

    That means that the original data feed is delivery bars with a resolution of 1 Second. The Resampling filter intercepts the data and buffers it until it can deliver a 1 Day bar. This will happen when a 1 Second bar from the next day is seen.

  • Replaying (cerebro.replaydata)

    For the same timeframes as above, the filter would use the 1 Second resolution bars to rebuild the 1 Day bar.

    That means that the 1 Day bar is delivered as many times as 1 Second bars are seen, updated to contain the latest information.

    This simulates, for example, how an actual trading day has developed.

    Note

    The length of the data, len(data) and therefore the length of the strategy remain unchanged as long as the day doesn’t change.