Project Overview
Relevant source files
The following files were used as context for generating this wiki page:
Slim Backtrader is a modernized fork of the original backtrader framework. It is a feature-rich Python platform designed for both backtesting trading strategies against historical data and executing live trading operations. The primary objective of this fork is to streamline the codebase by removing unnecessary features, updating syntax to modern standards, and improving performance through parallel processing support.
The system provides a modular architecture where users can define custom strategies, integrate various data feeds (CSV, Pandas, online sources), and utilize an extensive library of built-in indicators. It supports sophisticated broker simulation, including complex order types (Limit, Stop, OCO, Bracket) and flexible commission schemes, while emphasizing decision explainability and reproducibility.
Sources: README.md:1-24, README.rst:12-25, PLAN.md:1-12
System Architecture
The project architecture is centered around the Cerebro engine, which acts as the central control point for data management, strategy execution, and result analysis. The system is designed to be agentic and modular, allowing for composable trading strategies that function as data-driven jobs.
Core Components
| Component | Description |
|---|---|
| Cerebro | The cornerstone of the platform. It collects data feeds, attaches strategies, manages the broker simulation, and runs the backtest. |
| Data Feeds | Modules that provide market data to the system. Supports CSV, Pandas DataFrames, and live feeds like Interactive Brokers or Oanda. |
| Strategies | User-defined classes (derived from bt.Strategy) containing the logic for entry and exit signals. |
| Broker | Simulates a real-world broker, handling order execution, cash management, slippage, and position tracking. |
| Indicators | Mathematical transformations of data (e.g., SMA, RSI) used within strategies. Supports over 122 built-in indicators. |
| Analyzers | Tools for evaluating performance metrics such as Sharpe Ratio, Max Drawdown, and P&L. |
Sources: README.md:31-64, README.rst:58-94, strategies/moving_average_crossover.py:465-492
High-Level Data Flow
The following diagram illustrates how market data flows through the system to generate trade executions and performance reports.
The Cerebro engine coordinates the lifecycle of a backtest, starting from data ingestion to final performance analysis.
Sources: README.md:31-70, strategies/moving_average_crossover.py:465-510
Key Subsystems
Strategy Execution Engine
Strategies are implemented as Python classes that inherit from bt.Strategy. The engine executes the next() method for every new data point (bar) received. This lifecycle includes specific notification hooks for orders and trades.
Broker and Order Management
The simulated broker supports a wide array of order types and execution conditions. It handles:
- Order Types: Market, Close, Limit, Stop, StopLimit, StopTrail, OCO, and Bracket orders.
- Risk Management: Integrated support for stop losses, trailing stops, and volume filling strategies.
- Asset Support: Continuous cash adjustment for futures-like instruments and automated staking via Sizers.
Sources: README.md:58-68, README.rst:83-91
Data Writing and Logging
The Writer subsystem provides mechanisms to output the results of backtests or live sessions into structured formats. The WriterFile class manages headers, separators, and CSV formatting.
# backtrader/writer.py:102
class WriterFile(WriterBase):
params = (
("out", None), # Output stream (default sys.stdout)
("csv", False), # Toggle CSV output
("csvsep", ","), # CSV separator
("indent", 2), # Indentation for sections
("rounding", None), # Decimal rounding for floats
)
Sources: backtrader/writer.py:102-140
Modes of Operation
The system is designed to transition seamlessly between different environments:
- Replay Mode: Simulates historical trades using replayed market data for deterministic testing.
- Paper Trading: Uses live market data with simulated capital to validate strategies in real-time without financial risk.
- Live Trading: Connects to external brokers (e.g., Interactive Brokers via
IbPy) for real-market execution.
Sources: PLAN.md:43-50, PLANNING.md:4-30
Roadmap and Philosophy
The project follows an Agentic philosophy, where strategies are treated as composable, explainable, and testable tools. Future development focuses on:
- Parallel Processing: Improving support for multi-core optimization.
- Modernization: Updating aged implementations and removing deprecated integrations (e.g.,
pyfolio, interactive plotting). - Explainability: Enhancing logging to clearly show why specific trade decisions were made.
- Vector Memory: Integrating
pgvectorfor decision logs and embeddings to support more advanced reasoning.
Sources: README.md:8-20, PLAN.md:65-80
Summary
Slim Backtrader provides a robust framework for financial engineering, offering a balance between ease of use and deep customization. By providing a simulated environment that closely mimics real-world broker behavior, it allows developers to build, test, and deploy strategies with high confidence in reproducibility and performance.
Sources: README.md:1-24, PLAN.md:1-12