backtesting
Backtest trading strategies in Python
Decision gist · record as of 2026-08-14
Yes, if you are developing or researching trading strategies and want a lightweight, well-maintained framework with low install friction. The AGPL-3.0 license is a blocker for closed-source commercial use unless you obtain a separate license; for open-source or internal research, it poses no issue. No known security vulnerabilities. Active maintenance and a large user base make it a reliable choice for backtesting.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires Python 3.9 or later.
- Low friction install with just three runtime dependencies (numpy, pandas, bokeh).
- Active maintenance with recent commits and a substantial user base; last release was 23 days ago.
License · maintenance · safety
AGPL-3.0 (agpl) — Licensed under AGPL-3.0, which requires that any modifications or derivative works you distribute must also be open-source under the same license. Suitable for internal research or open-source projects, but commercial closed-source use requires careful review or a separate license.
last release 2026-07-22 (23 days) · last repo commit 2026-08-05 · 8,840 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 183,712 downloads/mo, #10,062 on PyPI
Alternatives
Verify before relying
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG
class SmaCross(Strategy):
def init(self):
price = self.data.Close
self.ma1 = self.I(SMA, price, 10)
self.ma2 = self.I(SMA, price, 20)
def next(self):
if crossover(self.ma1, self.ma2):
self.buy()
elif crossover(self.ma2, self.ma1):
self.sell()
bt = Backtest(GOOG, SmaCross, commission=.002)
stats = bt.run()
bt.plot()- Whether the built-in optimizer (SAMBO-based) is suitable for your strategy complexity or parameter space size
- Performance characteristics when backtesting very large datasets or high-frequency strategies
- Whether the package supports live trading or only historical backtesting
What it is and what it does
Backtesting.py is a Python framework for testing trading strategies against historical OHLC candlestick data. You define a strategy class with entry and exit logic, pass it historical price data (from any source), and the framework simulates trades, calculates performance metrics (Sharpe ratio, Sortino ratio, max drawdown, win rate, etc.), and generates interactive visualizations via bokeh. It handles position sizing, commissions, and order management automatically.
The package is built on numpy and pandas for fast numerical computation and data handling. It includes a built-in optimizer for parameter tuning, a library of common technical indicators and base strategies, and produces detailed trade-by-trade results as DataFrames. It's designed to be indicator-agnostic—you bring your own indicators or use the provided utilities—and supports any financial instrument with candlestick data (stocks, crypto, forex, futures, etc.).
Use it for
- Test a moving-average crossover or other rule-based strategy on years of historical stock or crypto data to measure profitability and risk.
- Optimize strategy parameters (e.g., moving-average periods) by running the built-in optimizer across a grid or search space.
- Compare multiple strategy variants side-by-side to see which has the best Sharpe ratio, lowest drawdown, or highest win rate.
- Generate equity curves and trade logs as pandas DataFrames for further analysis or reporting in Jupyter notebooks.
- Validate a trading idea before committing capital by simulating it on historical data with realistic commission and slippage assumptions.
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes, if you are developing or researching trading strategies and want a lightweight, well-maintained framework with low install friction.
The AGPL-3.0 license is a blocker for closed-source commercial use unless you obtain a separate license; for open-source or internal research, it poses no issue. No known security vulnerabilities. Active maintenance and a large user base make it a reliable choice for backtesting.
Install
backtesting on PyPI
Before you install
Low friction install with just three runtime dependencies (numpy, pandas, bokeh). Active maintenance with recent commits and a substantial user base; last release was 23 days ago.
Requires Python 3.9 or later.
License in practice
Licensed under AGPL-3.0, which requires that any modifications or derivative works you distribute must also be open-source under the same license. Suitable for internal research or open-source projects, but commercial closed-source use requires careful review or a separate license.
Quickstart
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG
class SmaCross(Strategy):
def init(self):
price = self.data.Close
self.ma1 = self.I(SMA, price, 10)
self.ma2 = self.I(SMA, price, 20)
def next(self):
if crossover(self.ma1, self.ma2):
self.buy()
elif crossover(self.ma2, self.ma1):
self.sell()
bt = Backtest(GOOG, SmaCross, commission=.002)
stats = bt.run()
bt.plot()
Verify before relying
- Whether the built-in optimizer (SAMBO-based) is suitable for your strategy complexity or parameter space size
- Performance characteristics when backtesting very large datasets or high-frequency strategies
- Whether the package supports live trading or only historical backtesting
Package facts
| License | AGPL-3.0 agpl |
| Python support | Supports the current Python release >=3.9 |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | 3 packagesnumpypandasbokeh |
| Maintenance | Actively maintained 23 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 183,712 / month, #10,062 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | Framework :: JupyterIntended Audience :: Financial and Insurance IndustryIntended Audience :: Science/ResearchLicense :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)Operating System :: OS IndependentProgramming Language :: Python :: 3 :: OnlyTopic :: Office/Business :: Financial :: InvestmentTopic :: Scientific/Engineering :: Visualization |
Evidence: backtesting-0.6.6-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “algorithmic trading simulation”
- backtestingBacktesting.py lets you define and test trading strategies against…
- lumibotLumibot is a Python framework for building, backtesting, and running…
- backtraderBacktrader is a Python backtesting and live trading engine that…
Give your agent the search over MCP, or paste the wish link into any chat.
More Visualization packages
matplotlib creates static, animated, and interactive visualizations in Python, producing publication-quality figures in multiple formats for scripts, shells, web servers, and graphical interfaces.
Install it if you need to visualize data, generate publication-quality figures, or embed plots in applications.
ContourPy calculates contours of 2D quadrilateral grids using C++11 algorithms wrapped in Python, offering serial and multithreaded implementations without requiring Matplotlib as a dependency.
Plotly is an interactive, browser-based graphing library that creates charts and visualizations from Python, rendering them as HTML that can be viewed in Jupyter notebooks, standalone files, or web applications.
Generates DOT language source code for graph structures and renders them using the Graphviz graph drawing software installed on your system.
Install it if you need to generate or render graphs from Python.
Streamlit transforms Python scripts into interactive web applications with minimal code, enabling rapid development of data dashboards, reports, and chat interfaces without requiring web development expertise.
Leather is a lightweight Python charting library for quick, no-frills data visualization. It generates charts without requiring perfect styling or extensive configuration.
See also backtrader · finlab · lumibot · mplfinance · vectorbt · nautilus_trader · gs-quant · freqtrade · quantstats · empyrical-reloaded