TopGit
GitHub Repo Review

backtrader: a Python backtesting library for trading

mementum/backtrader
BTopGit review image for mementum/backtrader
Review by Topgit.dev for mementum/backtrader, with GitHub repository stats and README context.
Quick verdict

backtrader earns its keep as a batteries-included backtesting engine: the Cerebro engine, 122 built-in indicators, and a simulated broker cover most of what a Python trader would otherwise hand-roll. The catch is a metaclass-heavy API and reliance on the aging IbPy wrapper for live Interactive Brokers trading. Solid for offline strategy testing; treat the live-trading path with more caution.

Stars
★ 23.5k
Forks
⑂ 5.3k
Contributors
👥 56
Language
Python
License
GPL-3.0
Topic
Backend
Updated
Aug 2024
Homepage
GitHub

Why you need to backtest before risking real capital

A trading strategy that looks good on a spreadsheet can still lose money once slippage, commissions, and order-fill timing get involved. Paper profits don't survive contact with real fills. Backtesting against historical price data is how you catch a strategy that only works because it was tuned on the same data it's later tested on, or one that assumes fills happen at the exact close price when they don't. backtrader exists so a Python trader can plug a strategy into historical data, run it through realistic order types and commission schemes, check the results, then switch that same strategy code to a live Interactive Brokers or Oanda feed.

What is backtrader?

backtrader is a Python library for backtesting and live trading of algorithmic strategies. It centers on the Cerebro engine, which wires together data feeds, a strategy class, 122 built-in indicators, and a simulated broker so you can run a strategy against historical data before risking real money. Written entirely in Python, it drops into an existing project as a pip install rather than running as a separate application.

Key capabilities: data feeds, indicators, and order types

  • ✓The Cerebro engine coordinates strategies, data feeds, and broker simulation in a single run
  • ✓122 built-in indicators, plus TA-Lib indicator support when the python ta-lib package is installed
  • ✓Features simulated broker trading with Market, Close, Limit, Stop, StopLimit, StopTrail, as well as StopTrailLimit and OCO order types, with bracket orders and slippage support
  • ✓Data feeds from CSV files, online sources, pandas DataFrames, and blaze
  • ✓Multiple data feeds, multiple strategies, and multiple timeframes in one run, with integrated resampling and replaying
  • ✓Live trading feeds for Interactive Brokers, Visual Chart, and Oanda
  • ✓Analyzers such as TimeReturn, Sharpe Ratio, and SQN, plus a pyfolio integration the README marks deprecated
  • ✓Sizers for automated position staking, trading calendars, and schedulers
How this repository's GitHub stars have grown over time. Source: star-history.com.View the star history ↗

Installing backtrader and its dependencies

Install with `pip install backtrader`; the core library has no external dependencies. For charts, install the plotting extra instead: `pip install backtrader[plotting]`, which needs matplotlib 1.4.1 or newer. It supports Python >= 3.2, and also runs under pypy and pypy3 (without plotting, since matplotlib isn't supported there). Live trading against Interactive Brokers needs IbPy installed separately; the README says it isn't on PyPI, so you install it with `pip install git+https://github.com/blampe/IbPy.git`. You can also skip pip entirely and drop the backtrader directory from the source distribution straight into your project.

Writing your first backtest with backtrader

A minimal backtrader script has three moving parts: a `bt.Strategy` (or `bt.SignalStrategy`) subclass that defines your logic, a `bt.Cerebro` instance that runs everything, and one or more data feeds. The README's own example builds a simple moving-average crossover: define two `bt.ind.SMA` indicators inside the strategy, wire a `bt.ind.CrossOver` between them, and register it with `self.signal_add(bt.SIGNAL_LONG, crossover)`. Outside the strategy class, create `cerebro = bt.Cerebro()`, add the strategy with `cerebro.addstrategy(...)`, load historical data with `cerebro.adddata(...)`, then call `cerebro.run()` to execute the backtest and `cerebro.plot()` to chart it if matplotlib is installed. The same sample ships as `sigsmacross/sigsmacross2.py` in the project's examples.

Strengths

  • ✓Ships with 122 built-in indicators, so most common technical-analysis building blocks don't need to be hand-coded
  • ✓The broker simulation supports a wide range of order types (Market, Limit, Stop, StopLimit, StopTrail, bracket, OCO) instead of just fill-at-close
  • ✓The same strategy class can run in backtest mode and then against a live Interactive Brokers or Oanda feed
  • ✓Self-contained with no required external dependencies for the core library; only plotting needs matplotlib
  • ✓Accepts data from CSV files, online sources, or a pandas DataFrame directly

Limitations and known constraints

  • △The API leans on Python metaclasses to auto-register indicators and strategies, which makes stepping through unfamiliar code in a debugger harder than a more explicit design
  • △Live trading with Interactive Brokers depends on IbPy, a third-party wrapper the README says isn't published on PyPI and must be installed from a GitHub fork
  • △The pyfolio analytics integration is marked deprecated in the README, so it can't be relied on for tearsheet reporting
  • △Plotting requires matplotlib and doesn't work under pypy, so headless or pypy setups lose the charting workflow
  • △The README's own notes (a Travis CI badge, a 2018 note about Yahoo Finance API reliability) are old, so parts of the documentation may not reflect the current state of the project

Alternatives

zipline — Quantopian's original event-driven backtesting engine, tied more tightly to a specific data bundle formatbacktesting.py — a much smaller, single-file backtesting library with a simpler API if you don't need backtrader's live-broker integrationsvectorbt — a vectorized, pandas/NumPy-based backtesting library built for speed across many parameter combinations, rather than backtrader's step-by-step event loopPyAlgoTrade — an older event-driven Python backtesting library with a narrower indicator set than backtrader's 122

Frequently asked questions about backtrader

Is backtrader free to use for commercial trading?

backtrader is released under the GPL-3.0 license, which allows free use for commercial trading. GPL-3.0 does require that if you distribute a modified version of backtrader itself, that version stays open source under the same license — using it internally to trade doesn't trigger that requirement.

Can backtrader connect to Interactive Brokers for live trading?

backtrader does support live trading with Interactive Brokers, using IB as one of its live data feed and order-execution destinations. It needs the third-party IbPy wrapper installed separately — the README notes IbPy isn't on PyPI, so you install it via `pip install git+https://github.com/blampe/IbPy.git`.

Does backtrader support Python 3 only or also Python 2?

backtrader's README has a section titled 'Python 2/3 Support' that lists Python >= 3.2 as the requirement, and it also runs under pypy and pypy3 (without plotting). The README doesn't give a specific Python 2 minimum version, so check your Python 2 setup against the library before relying on it.

How do I create a custom indicator in backtrader?

backtrader lists 'easy development of custom indicators' as a feature, but the exact subclassing steps aren't detailed in the README facts covered here. The project's separate documentation and indicator reference cover the pattern, and the library ships 122 built-in indicators to start from before you need to write your own.

What order types does backtrader support for backtesting?

backtrader's broker simulation supports Market, Close, Limit, Stop, StopLimit, StopTrail, and StopTrailLimit orders, plus OCO (one-cancels-other) and bracket orders. It also models slippage, configurable volume-filling strategies, and continuous cash adjustment for future-like instruments.

Is backtrader still actively maintained?

The GitHub facts available here don't include recent commit activity, so backtrader's current release cadence can't be confirmed from this review alone. The README carries some older signals — a Travis CI badge, a 2018 note about the Yahoo Finance API, and a pyfolio integration marked deprecated — worth checking the repository's own commit history before depending on it for new work.

Best use cases

  • •Testing a moving-average crossover or other rules-based strategy against years of historical CSV or pandas data before funding it
  • •Comparing order-execution assumptions (market vs. limit vs. stop) to see how fill logic changes a strategy's returns
  • •Running the same strategy code in backtest mode and then against a live Interactive Brokers or Oanda feed without a rewrite
  • •Writing a custom indicator when none of the 122 shipped ones fit

Who should try it — and who should skip

backtrader fits Python developers who already know how to code and want control over strategy logic, order types, and commission modeling rather than a drag-and-drop backtesting UI. It also suits anyone who wants to backtest and then live-trade through Interactive Brokers or Oanda using the same strategy class. Skip it if you want a no-code tool, if you need vectorized backtesting across huge parameter sweeps (the step-by-step Cerebro engine isn't built for that), or if you're not willing to work through metaclass-heavy internals when something breaks.

Related repositories

Source & attribution

Facts and feature details sourced from the mementum/backtrader GitHub repository and its README (https://github.com/mementum/backtrader).

GitHub data · last synced Aug 15, 2026Reviewed by Henry
← Back to TopGit

Still deciding about backtrader?

One click hands the question to an AI along with this page — see what it says about backtrader.

GitHub