美文网首页
3.9 Filters

3.9 Filters

作者: wanggs66 | 来源:发表于2020-04-21 22:03 被阅读0次

    Purpose

    • Transform the values provided by a data feed to deliver a different data feed
    • Filter 一开始的设计是用于简化Resample 和 Replay 这两个接口
      • Resample : (Seconds, 1) -> (Days, 1) 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: 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.

    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:

    • A callable which accepts this signature:
    callable(data, *args, **kwargs)
    

    or

    • A class which can be instantiated and called
      • During instantiation the init method must support the signature:

        def init(self, data, *args, **kwargs)

      • The call method bears this signature:

        def call(self, data, *args, **kwargs)

    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

    • data.backwards(size=1, force=False): removes size bars from the data stream (default is 1) by moving the logical pointer backwards. If force=True, then the physical storage is also removed.

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

    • data.forward(value=float('NaN'), size=1): moves size bars the storage forward, increasing the physical storage if needed be and fills with value

    • data._addtostack(bar, stash=False): adds bar to a stack for later processing. bar is an iterable containing as many values as lines has the data feed.

    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

    • data._save2stack(erase=False, force=False): saves the current data bar to the stack for later processing. If erase=True then data.backwards will be invoked and will receive the parameter force

    • data._updatebar(bar, forward=False, ago=0): uses the values in the iterable bar to overwrite the values in the data stream ago positions. With the default ago=0 the current bar will updated. With -1, the previous one.

    相关文章

      网友评论

          本文标题:3.9 Filters

          本文链接:https://www.haomeiwen.com/subject/aadmihtx.html