backtrader.resamplerfilter module

Resampler and Filter Module - Data resampling and replay functionality.

This module provides classes for resampling data to different timeframes and replaying data at compressed timeframes. It includes the base resampler and replayer classes along with specific implementations for different time periods.

Key Classes:

Resampler: Base class for resampling data to different timeframes. Replayer: Base class for replaying data with session information. DTFaker: Provides fake datetime for live/real-time data feeds.

Example

Resampling daily data to weekly: >>> data = bt.feeds.GenericCSVData(dataname=’daily.csv’) >>> cerebro.adddata(data) >>> cerebro.resampledata(data, timeframe=bt.TimeFrame.Weeks)

class backtrader.resamplerfilter.DTFaker[source]

Bases: object

Provides fake datetime for data sources that need periodic checks.

This class is used for real-time data feeds that return None from _load to indicate that a check of the resampler and/or notification queue is needed. It provides the current time in both UTC and localized formats.

data

The underlying data source.

_dt

UTC-like time as numeric value.

_dtime

Localized datetime.

sessionend

Trading day end time.

Example

>>> faker = DTFaker(data)
>>> print(faker.datetime())  # Current localized time
__init__(data, forcedata=None)[source]

Initialize the DTFaker with current time.

Parameters:
  • data – The underlying data source.

  • forcedata – Optional data source to force time from.

__call__(idx=0)[source]

Return the localized datetime.

Parameters:

idx – Index (ignored, for compatibility).

Returns:

Localized datetime object.

datetime(idx=0)[source]

Return the localized datetime.

Parameters:

idx – Index (ignored, for compatibility).

Returns:

Localized datetime object.

date(idx=0)[source]

Return the localized date.

Parameters:

idx – Index (ignored, for compatibility).

Returns:

Date object.

time(idx=0)[source]

Return the localized time.

Parameters:

idx – Index (ignored, for compatibility).

Returns:

Time object.

num2date(*args, **kwargs)[source]

Convert numeric time to datetime.

Delegates to the underlying data source’s num2date method.

Returns:

Datetime object.

date2num(*args, **kwargs)[source]

Convert datetime to numeric time.

Delegates to the underlying data source’s date2num method.

Returns:

Float representing the datetime.

class backtrader.resamplerfilter.Resampler[source]

Bases: _BaseResampler

This class resamples data of a given timeframe to a larger timeframe.

Params

  • Bar2edge (default: True)

    Resamples using time boundaries as the target.For example, with a “ticks -> 5 seconds” the resulting 5-seconds bars will be aligned to xx:00, xx:05, xx:10 …

    # When resampling, use time boundary as target, for example if ticks data wants to resample to 5 seconds, bars will be formed at xx:00, xx:05, xx:10

  • Adjbartime (default: True)

    Use the time at the boundary to adjust the time of the delivered resampled bar instead of the last seen timestamp. If resampling to “5 seconds” the time of the bar will be adjusted, for example, to hh:mm:05 even if the last seen timestamp was hh:mm:04.33

    note::

    Time will only be adjusted if “bar2edge” is True. It wouldn’t make sense to adjust the time if the bar has not been aligned to a boundary

    # Adjust the last bar’s final time, when bar2edge is True, use the final boundary as the last bar’s time

  • Rightedge (default: True)

    Use the right edge of the time boundaries to set the time.

    If False and compressing to 5 seconds, the time of a resampled bar for seconds between hh:mm:00 and hh:mm:04 will be hh:mm:00 (the starting boundary

    If True, the used boundary for the time will be hh:mm:05 (the ending boundary) # Whether to use the right time boundary, for example if time boundary is hh:mm:00:hh:mm:05, if set to True, will use hh:mm:05 # Set to False, will use hh:mm:00

params = (('bar2edge', True), ('adjbartime', True), ('rightedge', True))
replaying = False
last(data)[source]

Called when the data is no longer producing bars

Can be called multiple times. It has the chance to (for example) produce extra bars which may still be accumulated and have to be delivered

__call__(data, fromcheck=False, forcedata=None)[source]

Called for each set of values produced by the data source

class backtrader.resamplerfilter.Replayer[source]

Bases: _BaseResampler

This class replays data of a given timeframe to a larger timeframe.

It simulates the action of the market by slowly building up (for ex.) a daily bar from tick/seconds/minutes data

Only when the bar is complete will the “length” of the data be changed effectively delivering a closed bar

Params

  • Bar2edge (default: True)

    Replays using time boundaries as the target of the closed bar.For example, with a “ticks -> 5 seconds” the resulting 5-second bars will be aligned to xx:00, xx:05, xx:10 …

  • Adjbartime (default: False)

    Use the time at the boundary to adjust the time of the delivered resampled bar instead of the last seen timestamp. If resampling to “5 seconds” the time of the bar will be adjusted, for example, to hh:mm:05 even if the last seen timestamp was hh:mm:04.33

    Note

    Time will only be adjusted if “bar2edge” is True. It wouldn’t make sense to adjust the time if the bar has not been aligned to a boundary

    Note if this parameter is True, an extra tick with the adjusted

    time will be introduced at the end of the replayed bar

  • Rightedge (default: True)

    Use the right edge of the time boundaries to set the time.

    If False and compressing to 5 seconds, the time of a resampled bar for seconds between hh:mm:00 and hh:mm:04 will be hh:mm:00 (the starting boundary

    If True, the used boundary for the time will be hh:mm:05 (the ending boundary)

params = (('bar2edge', True), ('adjbartime', False), ('rightedge', True))
replaying = True
__call__(data, fromcheck=False, forcedata=None)[source]

Process the data for replaying.

Manages bar replaying with session information and time alignment.

Parameters:
  • data – The data source to replay.

  • fromcheck – Whether this is being called from a periodic check.

  • forcedata – Optional data source to force timing from.

Returns:

True if a new bar was generated, False otherwise.

Return type:

bool

class backtrader.resamplerfilter.ResamplerTicks[source]

Bases: Resampler

Resampler for tick-level data.

params = (('timeframe', 1),)
class backtrader.resamplerfilter.ResamplerSeconds[source]

Bases: Resampler

Resampler for seconds-level data.

params = (('timeframe', 3),)
class backtrader.resamplerfilter.ResamplerMinutes[source]

Bases: Resampler

Resampler for minute-level data.

params = (('timeframe', 4),)
class backtrader.resamplerfilter.ResamplerDaily[source]

Bases: Resampler

Resampler for daily data.

params = (('timeframe', 5),)
class backtrader.resamplerfilter.ResamplerWeekly[source]

Bases: Resampler

Resampler for weekly data.

params = (('timeframe', 6),)
class backtrader.resamplerfilter.ResamplerMonthly[source]

Bases: Resampler

Resampler for monthly data.

params = (('timeframe', 7),)
class backtrader.resamplerfilter.ResamplerYearly[source]

Bases: Resampler

Resampler for yearly data.

params = (('timeframe', 8),)
class backtrader.resamplerfilter.ReplayerTicks[source]

Bases: Replayer

Replayer for tick-level data.

params = (('timeframe', 1),)
class backtrader.resamplerfilter.ReplayerSeconds[source]

Bases: Replayer

Replayer for seconds-level data.

params = (('timeframe', 3),)
class backtrader.resamplerfilter.ReplayerMinutes[source]

Bases: Replayer

Replayer for minute-level data.

params = (('timeframe', 4),)
class backtrader.resamplerfilter.ReplayerDaily[source]

Bases: Replayer

Replayer for daily data.

params = (('timeframe', 5),)
class backtrader.resamplerfilter.ReplayerWeekly[source]

Bases: Replayer

Replayer for weekly data.

params = (('timeframe', 6),)
class backtrader.resamplerfilter.ReplayerMonthly[source]

Bases: Replayer

Replayer for monthly data.

params = (('timeframe', 7),)