PyBroker: Backtest Machine Learning Trading Strategies Without the Usual Headaches
You've got a trading idea, maybe a model that predicts short-term price moves, and now you need to know whether it would've actually made money. Wiring up historical data, walkforward training, and realistic metrics from scratch is a lot of plumbing before you even get to test a hypothesis. PyBroker is a Python framework built specifically for that problem: developing and backtesting algorithmic trading strategies, with a particular focus on strategies that lean on machine learning.
What It Does
PyBroker is a backtesting engine for trading strategies written in Python. The core engine is built on NumPy and accelerated with Numba, which matters when you're running the same strategy across many instruments or many parameter combinations. You define trading rules and models, point the framework at a data source, and it handles the mechanics of executing those rules across multiple instruments.
The framework integrates trading signals across multiple time intervals, including daily, weekly, and monthly, so you're not locked into a single bar size. For data, it ships with access to historical prices from Alpaca, Yahoo Finance, and AKShare, and you can also wire up your own custom data source if none of those fit. Model training and backtesting use walkforward analysis, which simulates how a strategy would perform during actual trading rather than training and testing on overlapping data. It supports Python 3.11 and up on Windows, Mac, and Linux.
Why It's Cool
-
Walkforward analysis is the default, not an afterthought. A lot of backtesting libraries make it easy to accidentally overfit by training and evaluating on the same window. PyBroker treats walkforward as a first-class workflow, which is closer to how you'd actually deploy a model.
-
Bootstrapped trading metrics. Instead of handing you a single Sharpe ratio and calling it a day, PyBroker uses randomized bootstrapping to produce more reliable performance statistics. If you've ever stared at a backtest result and wondered how much of it was noise, this is a meaningful difference.
-
Optuna for parameter optimization. Tuning strategy parameters is tedious manual work. PyBroker integrates Optuna so you can search the parameter space programmatically instead of babysitting a grid search.
-
Caching across the board. Downloaded data, computed indicators, and trained models all get cached. During development you'll re-run the same backtest dozens of times, and not re-fetching or recomputing everything each time is a real quality-of-life win.
-
Parallelization for compute-heavy runs. Training and backtesting can be parallelized, which is the difference between iterating in minutes and iterating overnight when you're working across a universe of instruments.
-
Agent Skills for AI-assisted development. There's a set of Agent Skills designed to help AI agents write trading strategies and backtests using PyBroker. It's a newer angle for a backtesting library and worth a look if you use coding assistants.
-
Multi-interval signal integration. Combining daily, weekly, and monthly signals in one strategy is a common real-world need that many frameworks handle awkwardly. PyBroker supports it directly.
How to Try It
- Install via pip:
pip install -U lib-pybroker
Or clone the repository if you'd rather work from source:
git clone https://github.com/edtechre/pybroker
- Import the pieces you need and define an execution function. Here's the shape of a rule-based strategy from the README:
from pybroker import Strategy, YFinance, highest
def exec_fn(ctx):
# Get the rolling 10 day high.
high_10d = ctx.indicator('high_10d')
# Buy on a new 10 day high.
if not ctx.long_p
...
The ctx object gives your execution function access to indicators and position state, so you can express entry and exit logic without managing the bookkeeping yourself.
- Check the documentation for the walkthroughs. The README links to notebooks covering getting started with data sources, training a model with walkforward analysis, parameter optimization, configuring parallelization, and building a custom data source. Those notebooks are probably the fastest way to understand the intended workflow.
The repository is at github.com/edtechre/pybroker. Note that the license is Apache 2.0 with the Commons Clause, so it's worth reading the license page before using it in a commercial product.
Final Thoughts
PyBroker sits in a useful spot: more opinionated and ML-focused than a general-purpose backtester, but not so abstracted that you lose control over your strategy logic. The combination of walkforward analysis, bootstrapped metrics, and Optuna integration addresses the parts of backtesting that are most likely to mislead you. If you're building strategies that depend on trained models rather than pure technical rules, it's worth an afternoon of exploration. If you just need a simple vectorized backtest of a moving average crossover, it may be more framework than you need—but for anything involving model retraining over time, the walkforward-first design is the right default.
Follow @githubprojects for more developer tools and open source projects.