backtrader.dataseries module¶
DataSeries Module - Time-series data structures for financial data.
This module provides the DataSeries class and related utilities for handling time-series financial data. It defines common data line types (OHLCV) and timeframe specifications.
- Key Classes:
TimeFrame: Enumeration of time periods (Minutes, Days, Weeks, etc.). DataSeries: Base class for financial data feeds with OHLCV lines. OHLC: DataSeries with Open, High, Low, Close lines. OHLCDateTime: OHLC with datetime line.
示例
Getting timeframe name: >>> TimeFrame.getname(TimeFrame.Days) # Returns 'Day' >>> TimeFrame.getname(TimeFrame.Days, 5) # Returns 'Days'
- class backtrader.dataseries.TimeFrame[源代码]¶
基类:
objectEnumeration of time periods for financial data.
Defines constants for different time periods used in financial data: Ticks, MicroSeconds, Seconds, Minutes, Days, Weeks, Months, Years, and NoTimeFrame.
- Class Methods:
getname(tframe, compression): Get name for timeframe. TFrame(name): Get timeframe constant from name. TName(tframe): Get name string from timeframe constant.
- Ticks, MicroSeconds, Seconds, Minutes, Days, Weeks, Months, Years, NoTimeFrame
Timeframe constants.
- Ticks = 1¶
- MicroSeconds = 2¶
- Seconds = 3¶
- Minutes = 4¶
- Days = 5¶
- Weeks = 6¶
- Months = 7¶
- Years = 8¶
- NoTimeFrame = 9¶
- Names = ['', 'Ticks', 'MicroSeconds', 'Seconds', 'Minutes', 'Days', 'Weeks', 'Months', 'Years', 'NoTimeFrame']¶
- names = ['', 'Ticks', 'MicroSeconds', 'Seconds', 'Minutes', 'Days', 'Weeks', 'Months', 'Years', 'NoTimeFrame']¶
- classmethod getname(tframe, compression=None)[源代码]¶
Get the name for a timeframe.
- 参数:
tframe -- TimeFrame constant (e.g., TimeFrame.Days).
compression -- Compression factor. If None, uses 1.
- 返回:
Name of the timeframe (singular or plural).
- 返回类型:
- class backtrader.dataseries.DataSeries[源代码]¶
基类:
LineSeriesBase class for financial time-series data feeds.
DataSeries extends LineSeries to provide the standard OHLCV (Open, High, Low, Close, Volume, OpenInterest) data lines plus DateTime.
- _name¶
Name identifier for the data series.
- _compression¶
Compression factor for the timeframe.
- _timeframe¶
TimeFrame period (Days, Minutes, etc.).
- Lines:
DateTime: Timestamp of the bar. Open: Opening price. High: Highest price. Low: Lowest price. Close: Closing price. Volume: Trading volume. OpenInterest: Open interest (for derivatives).
示例
>>> data = DataSeries() >>> print(data.close[0]) # Current close price
- plotinfo = <backtrader.metabase.plotinfo_obj object>¶
- name = ''¶
- Close = 0¶
- Low = 1¶
- High = 2¶
- Open = 3¶
- Volume = 4¶
- OpenInterest = 5¶
- DateTime = 6¶
- LineOrder = [6, 3, 2, 1, 0, 4, 5]¶
- getwriterheaders()[源代码]¶
Get header names for writing data.
- 返回:
List of header names including data name and line names.
- 返回类型:
- getwritervalues()[源代码]¶
Get current values for writing.
- 返回:
List of current values including data name, length, and line values.
- 返回类型:
- getwriterinfo()[源代码]¶
Get information about the data series.
- 返回:
Dictionary with name, timeframe, and compression info.
- 返回类型:
- frompackages = ()¶
- packages = ()¶
- class backtrader.dataseries.OHLC[源代码]¶
基类:
DataSeriesDataSeries with OHLCV lines but no datetime line.
- Lines:
close, low, high, open, volume, openinterest
- frompackages = ()¶
- packages = ()¶
- class backtrader.dataseries.OHLCDateTime[源代码]¶
基类:
OHLCDataSeries with datetime line plus OHLCV lines.
This is the full-featured data series for financial data.
- frompackages = ()¶
- packages = ()¶
- class backtrader.dataseries.SimpleFilterWrapper[源代码]¶
基类:
objectWrapper for filters added via .addfilter to turn them into processors.
Filters are callables which
Take data as an argument
Return False if the current bar has not triggered the filter
Return True if the current bar must be filtered
The wrapper takes the return value and executes the bar removal if needed to be