backtrader.filters.session module

Session Filter Module - Session bar filling.

This module provides the SessionFiller filter for adding missing bars over gaps within a trading session.

Classes:

SessionFiller: Fills missing bars within a session.

示例

>>> data = bt.feeds.GenericCSVData(dataname='data.csv')
>>> data.addfilter(bt.filters.SessionFiller())
>>> cerebro.adddata(data)
class backtrader.filters.session.SessionFiller[源代码]

基类:ParameterizedBase

Bar Filler to add missing bars over gaps in a session.

This class has been refactored from MetaParams to the new ParameterizedBase system for Day 36-38 of the metaprogramming removal project.

How to use it:

  • Instantiate the class (1 instance per filter needed)

  • addfilter it to the data with data.addfilter(filter_instance)

Bar fill logic:

  • The fill_price will be used to fill open, high, low and close

    If None then the close price of the last (previous) bar will be used.

  • Volume will be set to fill_vol

  • openinterest will be set to fill_oi

参数:
  • (def (- Skip_first_fill) --

    None):

    Price to be used to fill missing bars. If None will be used the closing price of the previous bar

  • (def --

    float('NaN')):

    Value to use to fill the missing volume

  • (def --

    float('NaN')):

    Value to use to fill the missing Open Interest

  • (def --

    True):

    Upon seeing the 1st valid bar do not fill from the sessionstart up to that bar

fill_price

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

fill_vol

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

fill_oi

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

skip_first_fill

Advanced parameter descriptor with type checking and validation.

This descriptor replaces the metaclass-based parameter system with a more modern and maintainable approach. It provides:

  • Automatic type checking and conversion

  • Value validation

  • Default value handling

  • Documentation support

  • Python 3.6+ __set_name__ support

MAXDATE = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
__init__(data, **kwargs)[源代码]

Initialize the SessionFiller.

参数:
  • data -- The data feed to apply the filter to.

  • **kwargs -- Additional keyword arguments passed to parent class.

__call__(data)[源代码]
Params:
  • data: the data source to filter/process

返回:

  • False (always) because this filter does not remove bars from the

stream

The logic (starting with a session end control flag of MAXDATE)

  • If new bar is over session end (never true for 1st bar)

    Fill up to the session end. Reset sessionend to MAXDATE & fall through

  • If the session end is flagged as MAXDATE

    Recalculate session limits and check whether the bar is within them

    If so, fill up and record the last seen tim

  • Else ... the incoming bar is in the session, fill up to it

class backtrader.filters.session.SessionFilterSimple[源代码]

基类:ParameterizedBase

This class can be applied to a data source as a filter and will filter out intraday bars which fall outside the regular session times (ie: pre/post market data)

This class has been refactored from MetaParams to the new ParameterizedBase system for Day 36-38 of the metaprogramming removal project.

This is a "simple" filter and must NOT manage the stack of the data (passed during init and __call__)

It needs no "last" method because it has nothing to deliver

Bar Management will be done by the SimpleFilterWrapper class made which is added durint the DataBase.addfilter_simple call

__init__(data, **kwargs)[源代码]

Initialize the SessionFilterSimple.

参数:
  • data -- The data feed to apply the filter to.

  • **kwargs -- Additional keyword arguments passed to parent class.

__call__(data)[源代码]

Return Values:

  • False: nothing to filter

  • True: filter current bar (because it's not in the session times)

class backtrader.filters.session.SessionFilter[源代码]

基类:ParameterizedBase

This class can be applied to a data source as a filter and will filter out intraday bars which fall outside the regular session times (ie: pre/post market data)

This class has been refactored from MetaParams to the new ParameterizedBase system for Day 36-38 of the metaprogramming removal project.

This is a "non-simple" filter and must manage the stack of the data (passed during init and __call__)

It needs no "last" method because it has nothing to deliver

__init__(data, **kwargs)[源代码]

Initialize the SessionFilter.

参数:
  • data -- The data feed to apply the filter to.

  • **kwargs -- Additional keyword arguments passed to parent class.

__call__(data)[源代码]

Return Values:

  • False: data stream was not touched

  • True: data stream was manipulated (bar outside session times and

  • removed)