--- id: backtesting version: "0.6.6" license: AGPL-3.0 license_treatment: agpl maintenance: active --- # backtesting — Backtest trading strategies in Python License: agpl · Maintenance: active · Downloads: 183.7K/mo ## 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 above — verify before relying. Backtesting.py lets you define and test trading strategies against historical price data, then visualize results and performance metrics like Sharpe ratio, drawdown, and win rate. 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 pip install backtesting uv add backtesting poetry add backtesting ## Installing backtesting 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. 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() Requires Python 3.9 or later. 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_current - Install friction: low - Maintenance: active - Downloads: 183.7K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags backtest trading strategies, algorithmic trading simulation, strategy performance analysis, historical price testing, trading strategy optimizer, ohlc candlestick backtesting, quantitative trading framework, trading-simulation, quantitative-finance, strategy-optimization [View on SkillFed](https://skillfed.io/packages/backtesting) · [View on PyPI](https://pypi.org/project/backtesting/)