explaingit

kernc/backtesting.py

8,356PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

A Python library for testing trading strategies against historical market price data to see how they would have performed before risking real money.

Mindmap

mindmap
  root((backtesting.py))
    What it does
      Test strategies
      Historical data
      Performance metrics
    Tech stack
      Python
      pip install
    Use cases
      Stock trading
      Crypto testing
      Forex backtesting
    Audience
      Algo traders
      Python developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Test a buy-low-sell-high stock strategy against years of historical candlestick data to measure real returns.

USE CASE 2

Optimize a moving-average strategy by searching a grid of window lengths to find the best-performing settings.

USE CASE 3

Visually inspect every trade entry and exit on an interactive chart to understand why a strategy won or lost.

USE CASE 4

Backtest the same strategy across stocks, crypto, or forex by swapping in different candlestick data files.

Tech stack

Pythonpip

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

Backtesting.py is a Python library that lets you test trading strategies against historical price data before risking real money. The idea is simple: you describe how your strategy decides to buy or sell, then run it against past market prices to see what would have happened. The library handles all the number crunching and produces a detailed summary of how the strategy performed. To use it, you write a small Python class that describes two things: how to set up any indicators or signals you want to track, and what to do on each new price bar (buy, sell, or do nothing). The library comes with a few example strategies and sample data so you can get started immediately without pulling in outside data sources. Installation is a single pip command. After a run, the library outputs a table of performance numbers: total return, annualized return, max drawdown, win rate, Sharpe ratio, and about two dozen more metrics. It also generates an interactive chart so you can visually inspect where the strategy entered and exited positions. You can see which trades were winners and which ones were not. The library includes a built-in optimizer that can search over a range of parameter values (for example, trying different moving-average window lengths) to find which settings worked best on the historical data. It is designed to work with any indicator library you prefer, so you are not locked into a particular set of technical analysis tools. It supports any financial instrument that comes as candlestick data: stocks, crypto, forex, futures. The README is straightforward and the API is small. The project does note a list of alternative backtesting frameworks in a separate document if this one does not fit your needs.

Copy-paste prompts

Prompt 1
Write a backtesting.py Strategy class that buys when the 20-day SMA crosses above the 50-day SMA and sells when it crosses below, then run it on the included sample data.
Prompt 2
Using backtesting.py's Backtest.optimize(), find the best EMA window lengths for a momentum strategy on BTC-USD hourly data and print the top 5 parameter combinations.
Prompt 3
Show me how to load a custom CSV file of candlestick data into backtesting.py and run a simple RSI strategy on it.
Prompt 4
Explain this backtesting.py results table to me in plain English: what do Sharpe ratio, max drawdown, and win rate actually mean for evaluating this strategy?
Open on GitHub → Explain another repo

← kernc on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.