Live Trading Deployment
Relevant source files
The following files were used as context for generating this wiki page:
The Live Trading Deployment feature in Backtrader provides a unified interface for executing trading strategies in real-time market environments. It extends the core backtesting engine by integrating with external broker APIs and live data feeds, allowing strategies developed in a backtesting environment to be deployed with minimal code changes. The system supports multiple execution modes, including Live Trade (real money execution), Paper Trade (real-time simulation), and Replay (historical stream simulation).
Sources: README.rst:62-84, README.md:21-41, PLAN.md:52-58
System Architecture
The live deployment architecture is built upon a Store-Broker-Data model. A "Store" acts as the central coordinator for a specific provider (e.g., Interactive Brokers or Visual Chart), managing the connection and providing both a Broker and Data Feed instances to the Cerebro engine.
Core Components
| Component | Description |
|---|---|
| Cerebro | The central orchestrator that manages data feeds, strategies, and the broker. |
| Store | Manages the underlying connection to the brokerage (e.g., VCStore for Visual Chart). |
| Live Broker | Handles order execution, position tracking, and account management (e.g., VCBroker). |
| Live Data Feed | Streams real-time ticks and bars from the provider. |
| Decoder | Specifically for IB, deserealizes messages and dispatches them to wrapper methods. |
Sources: README.rst:65-70, samples/vctest/vctest.py:126-146, backtrader/stores/ibstores/decoder.py:1-40
Deployment Logic Flow
The following diagram illustrates the initialization and data flow when deploying a strategy for live trading using the Visual Chart store as an example.
The diagram shows the transition from configuration to the active live trading state where the strategy receives live data status notifications.
Sources: samples/vctest/vctest.py:122-192, PLAN.md:52-58
Broker Integrations
The platform supports several live trading integrations, primarily focusing on Interactive Brokers, Oanda, and Visual Chart.
Supported Providers
- Interactive Brokers (IB): Requires
IbPyandpytz. It utilizes a complexDecoderto handle a wide range of messages including order status, portfolio updates, and market depth. - Visual Chart (VC): Requires a specific fork of
comtypes. - Oanda: REST API-based integration (v20 streaming was not supported in initial implementation).
Sources: README.rst:65-71, README.md:24-28, backtrader/stores/ibstores/decoder.py:46-135
Live Notification System
Strategies in live mode use specialized notification methods to react to market and connection events. Key status updates include:
LIVE: Indicates the feed has finished downloading historical data and is now streaming real-time bars.DATA NOTIF: General data status changes.STORE NOTIF: Messages from the store/brokerage provider.
def notify_data(self, data, status, *args, **kwargs):
print("***** DATA NOTIF:", data._getstatusname(status), *args)
if status == data.LIVE:
self.datastatus = 1 # Strategy is now live
Sources: samples/vctest/vctest.py:61-75
Data Management in Live Mode
Live deployment handles data through different processing modes to ensure strategies have necessary context before executing live trades.
Real-time Processing Modes
- Resampling: Transforming tick data or smaller timeframe bars into larger timeframes (e.g., 1-second ticks to 5-minute bars) during live streaming.
- Replaying: Simulating the growth of a bar as ticks arrive, allowing the strategy to see intermediate price action before a bar closes.
- Historical Download: Automatically fetching historical data upon startup to "prime" indicators before the live stream begins.
Sources: README.rst:76-78, README.md:31-37, samples/vctest/vctest.py:157-179
Order and Execution Handling
Live orders go through several states, communicated from the broker to the strategy via notify_order.
This sequence shows the interaction between the strategy and the external broker API during a trade lifecycle.
Sources: samples/vctest/vctest.py:84-98, backtrader/stores/ibstores/decoder.py:165-180
Configuration Options
When running a live strategy (e.g., via btrun or custom scripts), several parameters control the live behavior.
| Parameter | Type | Default | Description |
|---|---|---|---|
exactbars | int | 1 | If > 0, limits data accumulation to save memory in long-running live sessions. |
historical | bool | False | If True, performs only historical download and exits. |
qcheck | float | 0.5 | Timeout for periodic notification/resampling checks. |
broker | bool | False | Flag to enable live broker instead of simulated one. |
trade | bool | False | Enable/Disable actual buy/sell operations in the strategy. |
Sources: samples/vctest/vctest.py:118-121, samples/vctest/vctest.py:206-218, the_backtradersold_setup.py:115
Agentic Framework (Planned/WIP)
Newer modular developments (Agentic Trading) introduce a framework for "Observing" agents that use pydantic-ai tools to reason over market data. This system plans to wrap the Interactive Brokers API into structured tools:
InteractiveBrokersClientTool: For submitting and monitoring orders.MarketDataTool: For subscribing to live data.VectorLoggerTool: To annotate trade decisions for audit and backtesting.
Sources: PLAN.md:1-25, PLAN.md:60-70
The Live Trading Deployment system ensures a smooth transition from research to production by providing standardized interfaces for multiple brokers and sophisticated data streaming capabilities. Its modular design allows for both traditional strategy execution and emerging agentic workflows.