Test a buy-low-sell-high stock strategy against years of historical candlestick data to measure real returns.
Optimize a moving-average strategy by searching a grid of window lengths to find the best-performing settings.
Visually inspect every trade entry and exit on an interactive chart to understand why a strategy won or lost.
Backtest the same strategy across stocks, crypto, or forex by swapping in different candlestick data files.
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.
← kernc on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.