Quick answer
backtesting.py is a lightweight Python library for backtesting a single instrument. You subclass Strategy, declare indicators in init, place orders in next, then Backtest(data, Strategy).run() returns a full statistics table and plot() opens an interactive Bokeh chart. optimize() grid-searches parameters. It is AGPL-3.0 licensed.
backtesting.py at a glance
- Category
- Research & vectorized backtesting
- Language
- Python
- License
- AGPL-3.0
- Does
- Backtesting, Optimization, Performance analytics
- Markets
- Stocks, ETFs, Crypto, Forex, Futures
- Best for
- Single-instrument strategies where you want readable code, solid statistics and a chart fast.
- In AlgoGP
- Built-in bridge: add it to any project
Pick it if
Single-instrument strategies where you want readable code, solid statistics and a chart fast.
Watch out for
One instrument per backtest.
backtesting.py strengths and limitations
Strengths
- Tiny API, quick to learn.
- Excellent interactive charts.
- Built-in optimizer and heatmaps.
Limitations
- One instrument per backtest.
- No live trading.
- AGPL license matters for closed-source services.
backtesting.py example
pip install backtestingfrom backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import GOOG, SMA
class SmaCross(Strategy):
def init(self):
self.fast, self.slow = self.I(SMA, self.data.Close, 10), self.I(SMA, self.data.Close, 30)
def next(self):
if crossover(self.fast, self.slow):
self.position.close(); self.buy()
elif crossover(self.slow, self.fast):
self.position.close(); self.sell()
print(Backtest(GOOG, SmaCross, commission=0.002).run())Use backtesting.py with AlgoGP
AlgoGP has a built-in backtesting.py bridge. Describe the strategy to the copilot and backtest it in AlgoGP, then open the project’s APIs & keys tab and add backtesting.py 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/backtesting_py_strategy.pyrequirements.txtgains:backtesting
python frameworks/backtesting_py_strategy.pybacktesting.py: frequently asked questions
Can backtesting.py trade live?
No; use its results to choose parameters, then run live with another framework or AlgoGP’s exported live.py.
Does backtesting.py support multiple assets?
Not in one backtest; run one per instrument or use bt, vectorbt or an event-driven engine.
backtesting.py alternatives
- vectorbtPythonVectorized backtesting on pandas and Numba: test thousands of parameter combinations in seconds, with interactive Plotly analytics.
- BacktraderPythonThe classic Python event-driven backtesting library: Cerebro, data feeds, analyzers, sizers and plotting, with live stores for IB and OANDA.
- btPythonFlexible Python framework for portfolio-level backtests built from composable algos: select, weigh and rebalance on a schedule.
- PyBrokerPythonPython framework for machine-learning trading strategies: walk-forward analysis, bootstrapped metrics and a fast Numba backtesting engine.
Independent guide. AlgoGP is not affiliated with backtesting.py. Features, licenses and maintenance change; check the official documentation. Nothing here is investment advice.