backtrader.feeds.pandafeed module

Pandas Data Feed Module - Pandas DataFrame integration.

This module provides data feeds for loading market data from Pandas DataFrames.

Classes:

PandasDirectData: Uses DataFrame tuples as data source. PandasData: Uses DataFrame columns as data source.

Example

>>> import pandas as pd
>>> df = pd.read_csv('data.csv')
>>> data = bt.feeds.PandasData(dataname=df)
>>> cerebro.adddata(data)
class backtrader.feeds.pandafeed.PandasDirectData[source]

Bases: DataBase

Uses a Pandas DataFrame as the feed source, iterating directly over the tuples returned by “itertuples”.

This means that all parameters related to lines must have numeric values as indices into the tuples

Note

  • The dataname parameter is a Pandas DataFrame

  • A negative value in any of the parameters for the Data lines indicates it’s not present in the DataFrame it is

datafields = ['datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest']
__init__(*args, **kwargs)

Initialize a LineMultiple instance.

Sets up the internal state for managing multiple lines, including line type indicator, lines collection, clock reference, and line iterator tracking.

Initializes:

_ltype: Line type indicator (None for base LineMultiple). lines: Collection of line objects (creates if not exists). _clock: Clock reference for synchronization. _lineiterators: Dictionary tracking registered lineiterators. _minperiod: Minimum period requirement (defaults to 1).

start()[source]

Start the PandasDirect data feed.

Creates iterator from DataFrame.

frompackages = ()
packages = ()
class backtrader.feeds.pandafeed.PandasData[source]

Bases: DataBase

Uses a Pandas DataFrame as the feed source, using indices into column names (which can be “numeric”)

This means that all parameters related to lines must have numeric values as indices into the tuples

Params:

  • nocase (default True) case-insensitive match of column names

Note

  • The dataname parameter is a Pandas DataFrame

  • Values possible for datetime

    • None: the index contains the datetime

    • -1: no index, autodetect column

    • >= 0 or string: specific colum identifier

  • For other lines parameters

    • None: column not present

    • -1: autodetect

    • >= 0 or string: specific colum identifier

datafields = ['datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest']
__init__(*args, **kwargs)

Initialize a LineMultiple instance.

Sets up the internal state for managing multiple lines, including line type indicator, lines collection, clock reference, and line iterator tracking.

Initializes:

_ltype: Line type indicator (None for base LineMultiple). lines: Collection of line objects (creates if not exists). _clock: Clock reference for synchronization. _lineiterators: Dictionary tracking registered lineiterators. _minperiod: Minimum period requirement (defaults to 1).

start()[source]

Start the Pandas data feed.

Resets index and converts column names to indices.

frompackages = ()
packages = ()