Quick answer
Backtrader is a mature, pure-Python event-driven backtesting library. You add data feeds and a bt.Strategy to Cerebro, which steps through bars, fills orders through a simulated broker, and reports through analyzers and plots. It has stores for live trading with Interactive Brokers and OANDA. Development has been quiet for years, but its API is stable and widely taught.
Backtrader at a glance
- Category
- Backtest + live trading engines
- Language
- Python
- License
- GPL-3.0
- Does
- Backtesting, Live trading, Optimization, Performance analytics, Indicators
- Markets
- Stocks, ETFs, Futures, Forex, Crypto
- Best for
- Bar-based strategies on a few instruments, learning event-driven backtesting, and projects that value a stable API.
- In AlgoGP
- Built-in bridge: add it to any project
Pick it if
Bar-based strategies on a few instruments, learning event-driven backtesting, and projects that value a stable API.
Watch out for
Little upstream development; some modern pandas/matplotlib versions need workarounds.
Backtrader strengths and limitations
Strengths
- Clear event-driven model with indicators, sizers, analyzers and observers.
- Multiple data feeds and timeframes, resampling and replay.
- Built-in plotting and optimization over parameter grids.
- Large community, books and examples.
Limitations
- Little upstream development; some modern pandas/matplotlib versions need workarounds.
- Slow for large universes or tick data.
- Live stores (IB, OANDA v1) are dated; many users bridge to newer clients.
Backtrader example
pip install backtraderimport backtrader as bt
class SmaCross(bt.Strategy):
params = dict(fast=50, slow=200)
def __init__(self):
self.cross = bt.ind.CrossOver(bt.ind.SMA(period=self.p.fast), bt.ind.SMA(period=self.p.slow))
def next(self):
if not self.position and self.cross > 0:
self.buy()
elif self.position and self.cross < 0:
self.close()Use Backtrader with AlgoGP
AlgoGP has a built-in Backtrader bridge. Describe the strategy to the copilot and backtest it in AlgoGP, then open the project’s APIs & keys tab and add Backtrader under Frameworks. The project gains these files, saved as a new version and included in every export:
frameworks/algogp_bridge.pyframeworks/tests/test_bridge.pyframeworks/backtrader_strategy.pyrequirements.txtgains:backtrader,pandas
python frameworks/backtrader_strategy.pyBacktrader connects to Interactive Brokers, OANDA, Yahoo Finance. Each guide shows how to create the API key, which you can store in the project’s Keys & secrets.
Backtrader: frequently asked questions
Is Backtrader still maintained?
Upstream development is largely inactive, but the library is stable and community forks patch compatibility issues.
Can Backtrader trade live?
Yes, through IBStore and OandaStore, though many traders now pair it with ib_async or a broker REST API.
What replaces Backtrader for speed?
vectorbt for vectorized research, NautilusTrader or LEAN for fast event-driven engines.
Backtrader alternatives
- backtesting.pyPythonSmall, fast Python backtesting library: subclass Strategy, call Backtest.run(), get stats, an interactive Bokeh chart and built-in optimization.
- vectorbtPythonVectorized backtesting on pandas and Numba: test thousands of parameter combinations in seconds, with interactive Plotly analytics.
- NautilusTraderPython, RustHigh-performance, Rust-core trading platform with a Python API: the same strategy runs in a nanosecond-resolution backtester and live across many venues.
- Zipline ReloadedPythonThe maintained fork of Quantopian’s Zipline: an event-driven, pipeline-based backtester for US equities with data bundles and realistic slippage models.
Independent guide. AlgoGP is not affiliated with Backtrader. Features, licenses and maintenance change; check the official documentation. Nothing here is investment advice.