3.9 Filters

2020-04-21  本文已影响0人  wanggs66

Purpose

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.

Filters at work

data = MyDataFeed(dataname=myname)
data.addfilter(filter, *args, **kwargs)
cerebro.addata(data)

data = MyDataFeed(dataname=myname)
data.addfilter(filter, *args, **kwargs)
cerebro.replaydata(data)

This shows that filter is compatible to the resample and replay filter.

Filter Interface

A filter must conform to a given interface, being this:

callable(data, *args, **kwargs)

or

A Sample Filter

Session filter : only keep data between sesssionstart and sessionend
class SessionFilter(object):
def init(self, data):
pass

    def __call__(self, data):
        if data.p.sessionstart <= data.datetime.time() <= data.p.sessionend:
            # bar is in the session
            return False  # tell outer data   loop the bar can be processed

        # bar outside of the regular session times
        data.backwards()  # remove bar from data stack
        return True  # tell outer data loop to fetch a new bar

Data Pseudo-API for Filters

Removing the physical storage is a delicate operation and is only meant as a hack for internal operations.

If stash=False the bar added to the stack will be consumed immediately by the system at the beginning of the next iteration.

If stash=True the bar will undergo the entire loop processing including potentially being reparsed by filters

上一篇下一篇

猜你喜欢

热点阅读