backtrader.store module

Store Module - Data storage and broker connection management.

This module provides base classes for Store implementations, which manage connections to external data sources and brokers. It includes singleton pattern support and parameter management for store classes.

Classes:

SingletonMixin: Mixin class to implement singleton pattern. StoreParams: Parameter management for Store classes. Store: Base class for all Store implementations.

示例

Using a store to get data and broker: >>> store = bt.observers.backends.ViStore() >>> data = store.getdata() >>> broker = store.getbroker()

class backtrader.store.SingletonMixin[源代码]

基类:object

Mixin class to make a class a singleton without using metaclasses.

This mixin ensures only one instance of the class exists. The instance is created on first instantiation and reused on subsequent calls.

static __new__(cls, *args, **kwargs)[源代码]

Create and return the singleton instance.

参数:
  • *args -- Positional arguments passed to the class constructor.

  • **kwargs -- Keyword arguments passed to the class constructor.

返回:

The single instance of this class.

返回类型:

SingletonMixin

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

Initialize the singleton instance only once.

参数:
  • *args -- Positional arguments passed to the parent __init__.

  • **kwargs -- Keyword arguments passed to the parent __init__.

备注

If the singleton has already been initialized, this method returns immediately without reinitializing.

class backtrader.store.StoreParams[源代码]

基类:object

Simple parameter management for Store classes.

This class provides automatic parameter initialization from the class-level params tuple, creating a self.p attribute with all parameter values.

__init__()[源代码]

Initialize parameters from the class-level params tuple.

Parses the params tuple defined at class level and creates a self.p object with all parameter values as attributes.

class backtrader.store.Store[源代码]

基类:SingletonMixin, StoreParams

Base class for all Stores.

Stores manage connections to external data sources and brokers. They provide data feeds and broker instances, and handle notifications from the external service.

_started

Whether the store has been started.

params

Tuple of parameter definitions.

broker

The broker instance associated with this store.

BrokerCls

The broker class to use (None by default).

DataCls

The data class to use (None by default).

params = ()
__init__()[源代码]

Initialize the Store instance.

Sets up internal state for broker, environment, cerebro, data sources, and notifications.

getdata(*args, **kwargs)[源代码]

Returns DataCls with args, kwargs

classmethod getbroker(*args, **kwargs)[源代码]

Returns broker with *args, **kwargs from registered BrokerCls

BrokerCls = None
DataCls = None
start(data=None, broker=None)[源代码]

Start the store and initialize connections.

参数:
  • data -- Data feed to register with the store (optional).

  • broker -- Broker instance to register with the store (optional).

备注

On first call, initializes the notification queue and data list. Subsequent calls can add data feeds or set the broker.

stop()[源代码]

Stop the store and clean up resources.

This method should be overridden by subclasses to perform any necessary cleanup.

put_notification(msg, *args, **kwargs)[源代码]

Add a message to the notification queue.

参数:
  • msg -- The notification message.

  • *args -- Additional positional arguments to store with the message.

  • **kwargs -- Additional keyword arguments to store with the message.

get_notifications()[源代码]

Return the pending "store" notifications