# Backtrader — Python Algorithmic Trading Framework > A feature-rich Python framework for backtesting and live trading strategies with support for multiple data feeds, brokers, and advanced analytics. ## Install Save in your project root: # Backtrader — Python Algorithmic Trading Framework ## Quick Use ```bash pip install backtrader ``` ```python import backtrader as bt class SmaCross(bt.SignalStrategy): def __init__(self): sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30) self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2)) cerebro = bt.Cerebro() cerebro.addstrategy(SmaCross) data = bt.feeds.YahooFinanceCSVData(dataname='data.csv') cerebro.adddata(data) cerebro.run() cerebro.plot() ``` ## Introduction Backtrader is a Python framework for backtesting trading strategies against historical data. It provides a flexible architecture with built-in indicators, analyzers, and plotting capabilities, making it a popular choice for quantitative traders who want to prototype and evaluate strategies quickly. ## What Backtrader Does - Backtests trading strategies against historical OHLCV data from CSV, Pandas, or live feeds - Provides 120+ built-in technical indicators with the ability to create custom ones - Supports multiple simultaneous data feeds and timeframes in a single strategy - Connects to live brokers like Interactive Brokers for paper and real trading - Generates detailed performance reports with Sharpe ratio, drawdown, and trade analysis ## Architecture Overview Backtrader uses a Cerebro engine that orchestrates data feeds, strategies, brokers, and analyzers. Data feeds emit bars that are consumed by strategy instances, which produce orders routed through a broker emulator or live broker adapter. The architecture uses Python's object model with lines (time-series arrays) as the core data structure for indicators and signals. ## Self-Hosting & Configuration - Install via pip with no external dependencies required for basic backtesting - Feed data from CSV files, Pandas DataFrames, or real-time broker connections - Configure commission schemes, slippage models, and position sizing in the Cerebro engine - Extend with custom analyzers, observers, and sizers for specialized reporting - Run headless for batch optimization or with matplotlib plotting for visual analysis ## Key Features - Event-driven backtesting engine that processes bars sequentially for realistic simulation - Multi-timeframe and multi-data support within a single strategy - Built-in optimization with parameter grid search and walk-forward analysis - Live trading integration with Interactive Brokers via the IBPy adapter - Extensive plotting with trade markers, indicator overlays, and portfolio curves ## Comparison with Similar Tools - **Zipline** — Was Quantopian's engine; Backtrader is more flexible and still maintained by the community - **VeighNa** — Full trading platform with GUI; Backtrader is a lighter library focused on backtesting - **QuantConnect** — Cloud-based with C# support; Backtrader is local-first and Python-only - **Freqtrade** — Crypto bot with built-in execution; Backtrader is asset-class agnostic ## FAQ **Q: Is Backtrader still maintained?** A: The core library is stable and feature-complete. Community forks and extensions continue development. **Q: Can I use Backtrader for live trading?** A: Yes, it supports live trading via Interactive Brokers and other broker adapters. **Q: What data formats are supported?** A: CSV, Pandas DataFrames, and real-time feeds from brokers or custom sources. **Q: Does it support crypto trading?** A: Yes, through CCXT integration or custom data feed adapters. ## Sources - https://github.com/mementum/backtrader - https://www.backtrader.com --- Source: https://tokrepo.com/en/workflows/asset-c91240a2 Author: AI Open Source