Low activity AlgoGP bridge

Backtrader guide

Backtrader is the library a generation of Python algo traders learned on; it is still a solid choice for bar-based strategies.

Last reviewed · Also known as bt.Strategy, Cerebro

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 backtrader
Install
import 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()
SMA crossover in Backtrader.

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.py
  • frameworks/tests/test_bridge.py
  • frameworks/backtrader_strategy.py
  • requirements.txt gains: backtrader, pandas
python frameworks/backtrader_strategy.py
Run the AlgoGP strategy with Backtrader.

Backtrader 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

Independent guide. AlgoGP is not affiliated with Backtrader. Features, licenses and maintenance change; check the official documentation. Nothing here is investment advice.

Build a strategy