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[源代码]

基类:object

Enumeration 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).

返回类型:

str

classmethod TFrame(name)[源代码]

Get TimeFrame constant from name.

参数:

name -- String name like 'Days', 'Minutes'.

返回:

TimeFrame constant.

classmethod TName(tframe)[源代码]

Get name string from timeframe constant.

参数:

tframe -- TimeFrame constant.

返回:

Name of the timeframe.

返回类型:

str

class backtrader.dataseries.DataSeries[源代码]

基类:LineSeries

Base 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.

返回类型:

list

getwritervalues()[源代码]

Get current values for writing.

返回:

List of current values including data name, length, and line values.

返回类型:

list

getwriterinfo()[源代码]

Get information about the data series.

返回:

Dictionary with name, timeframe, and compression info.

返回类型:

OrderedDict

get_name()[源代码]

Get the name of this data series.

返回:

Data series name.

返回类型:

str

frompackages = ()
packages = ()
class backtrader.dataseries.OHLC[源代码]

基类:DataSeries

DataSeries with OHLCV lines but no datetime line.

Lines:

close, low, high, open, volume, openinterest

frompackages = ()
packages = ()
class backtrader.dataseries.OHLCDateTime[源代码]

基类:OHLC

DataSeries with datetime line plus OHLCV lines.

This is the full-featured data series for financial data.

frompackages = ()
packages = ()
class backtrader.dataseries.SimpleFilterWrapper[源代码]

基类:object

Wrapper 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

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

Initialize the filter wrapper.

参数:
  • data -- Data source to filter.

  • ffilter -- Filter class or callable.

  • *args -- Positional arguments for the filter.

  • **kwargs -- Keyword arguments for the filter.

__call__(data)[源代码]

Apply the filter to the data.

参数:

data -- Data source to filter.

返回:

True if bar was filtered (removed), False otherwise.

返回类型:

bool