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.

Example

Getting timeframe name: >>> TimeFrame.getname(TimeFrame.Days) # Returns ‘Day’ >>> TimeFrame.getname(TimeFrame.Days, 5) # Returns ‘Days’

class backtrader.dataseries.TimeFrame[source]

Bases: 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)[source]

Get the name for a timeframe.

Parameters:
  • tframe – TimeFrame constant (e.g., TimeFrame.Days).

  • compression – Compression factor. If None, uses 1.

Returns:

Name of the timeframe (singular or plural).

Return type:

str

classmethod TFrame(name)[source]

Get TimeFrame constant from name.

Parameters:

name – String name like ‘Days’, ‘Minutes’.

Returns:

TimeFrame constant.

classmethod TName(tframe)[source]

Get name string from timeframe constant.

Parameters:

tframe – TimeFrame constant.

Returns:

Name of the timeframe.

Return type:

str

class backtrader.dataseries.DataSeries[source]

Bases: 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).

Example

>>> 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()[source]

Get header names for writing data.

Returns:

List of header names including data name and line names.

Return type:

list

getwritervalues()[source]

Get current values for writing.

Returns:

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

Return type:

list

getwriterinfo()[source]

Get information about the data series.

Returns:

Dictionary with name, timeframe, and compression info.

Return type:

OrderedDict

get_name()[source]

Get the name of this data series.

Returns:

Data series name.

Return type:

str

frompackages = ()
packages = ()
class backtrader.dataseries.OHLC[source]

Bases: DataSeries

DataSeries with OHLCV lines but no datetime line.

Lines:

close, low, high, open, volume, openinterest

frompackages = ()
packages = ()
class backtrader.dataseries.OHLCDateTime[source]

Bases: OHLC

DataSeries with datetime line plus OHLCV lines.

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

frompackages = ()
packages = ()
class backtrader.dataseries.SimpleFilterWrapper[source]

Bases: 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)[source]

Initialize the filter wrapper.

Parameters:
  • data – Data source to filter.

  • ffilter – Filter class or callable.

  • *args – Positional arguments for the filter.

  • **kwargs – Keyword arguments for the filter.

__call__(data)[source]

Apply the filter to the data.

Parameters:

data – Data source to filter.

Returns:

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

Return type:

bool