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.
示例
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[源代码]¶
基类:
objectProvides 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.
示例
>>> faker = DTFaker(data) >>> print(faker.datetime()) # Current localized time
- __init__(data, forcedata=None)[源代码]¶
Initialize the DTFaker with current time.
- 参数:
data -- The underlying data source.
forcedata -- Optional data source to force time from.
- __call__(idx=0)[源代码]¶
Return the localized datetime.
- 参数:
idx -- Index (ignored, for compatibility).
- 返回:
Localized datetime object.
- datetime(idx=0)[源代码]¶
Return the localized datetime.
- 参数:
idx -- Index (ignored, for compatibility).
- 返回:
Localized datetime object.
- date(idx=0)[源代码]¶
Return the localized date.
- 参数:
idx -- Index (ignored, for compatibility).
- 返回:
Date object.
- time(idx=0)[源代码]¶
Return the localized time.
- 参数:
idx -- Index (ignored, for compatibility).
- 返回:
Time object.
- class backtrader.resamplerfilter.Resampler[源代码]¶
基类:
_BaseResamplerThis 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¶
- class backtrader.resamplerfilter.Replayer[源代码]¶
基类:
_BaseResamplerThis 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)[源代码]¶
Process the data for replaying.
Manages bar replaying with session information and time alignment.
- 参数:
data -- The data source to replay.
fromcheck -- Whether this is being called from a periodic check.
forcedata -- Optional data source to force timing from.
- 返回:
True if a new bar was generated, False otherwise.
- 返回类型:
- class backtrader.resamplerfilter.ResamplerTicks[源代码]¶
基类:
ResamplerResampler for tick-level data.
- params = (('timeframe', 1),)¶
- class backtrader.resamplerfilter.ResamplerSeconds[源代码]¶
基类:
ResamplerResampler for seconds-level data.
- params = (('timeframe', 3),)¶
- class backtrader.resamplerfilter.ResamplerMinutes[源代码]¶
基类:
ResamplerResampler for minute-level data.
- params = (('timeframe', 4),)¶
- class backtrader.resamplerfilter.ResamplerDaily[源代码]¶
基类:
ResamplerResampler for daily data.
- params = (('timeframe', 5),)¶
- class backtrader.resamplerfilter.ResamplerWeekly[源代码]¶
基类:
ResamplerResampler for weekly data.
- params = (('timeframe', 6),)¶
- class backtrader.resamplerfilter.ResamplerMonthly[源代码]¶
基类:
ResamplerResampler for monthly data.
- params = (('timeframe', 7),)¶
- class backtrader.resamplerfilter.ResamplerYearly[源代码]¶
基类:
ResamplerResampler for yearly data.
- params = (('timeframe', 8),)¶
- class backtrader.resamplerfilter.ReplayerTicks[源代码]¶
基类:
ReplayerReplayer for tick-level data.
- params = (('timeframe', 1),)¶
- class backtrader.resamplerfilter.ReplayerSeconds[源代码]¶
基类:
ReplayerReplayer for seconds-level data.
- params = (('timeframe', 3),)¶
- class backtrader.resamplerfilter.ReplayerMinutes[源代码]¶
基类:
ReplayerReplayer for minute-level data.
- params = (('timeframe', 4),)¶
- class backtrader.resamplerfilter.ReplayerDaily[源代码]¶
基类:
ReplayerReplayer for daily data.
- params = (('timeframe', 5),)¶