Maintained AlgoGP bridge

backtesting.py guide

backtesting.py is often the quickest route from an idea to a credible single-asset backtest with a chart.

Last reviewed · Also known as backtesting python, kernc backtesting

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 backtesting
Install
from 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())
SMA crossover with the bundled GOOG data.

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.py
  • frameworks/tests/test_bridge.py
  • frameworks/backtesting_py_strategy.py
  • requirements.txt gains: backtesting
python frameworks/backtesting_py_strategy.py
Run the AlgoGP strategy with backtesting.py.

backtesting.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

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

Build a strategy