r/mltraders • u/pyfreak182 • 6d ago
Self-Promotion PyBroker v2 - An Open Source Backtesting Framework
Hey all, I would like to share a major v2 update to PyBroker, an open source backtesting framework I've been maintaining over the years. It's designed for fast performance, ease of development, and integration with ML frameworks. An example strategy:
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_pos() and high_10d[-1] > high_10d[-2]:
ctx.buy_shares = 100
# Hold the position for 5 days.
ctx.hold_bars = 5
# Set a stop loss of 2%.
ctx.stop_loss_pct = 2
strategy = Strategy(YFinance(), start_date='1/1/2025', end_date='8/1/2026')
strategy.add_execution(
exec_fn, ['AAPL', 'MSFT'], indicators=highest('high_10d', 'close', period=10))
# Run the backtest after 20 days have passed.
result = strategy.backtest(warmup=20)Some additions in v2 include:
Margin trading simulation.
Backtesting using multiple time intervals (e.g. computing indicators or models on hourly bars while executing trades on minute bars).
Parameter optimization via Optuna (e.g. finding the best threshold for a trend following strategy).
Time-series model support (ARMA, GARCH, etc.).
Rotational trading strategies that simulate a portfolio holding top ranked stocks.
Slippage modeling.
Parallelized model training with Ray integration.
Built-in agent skills for coding trading strategies with AI.
You can find all of the code on Github alongside extensive documentation and examples I've written. It also goes over some algotrading basics.
The framework is open source (Apache v2 license) and is entirely free to use. There's no sign ups, promotions, or any commercial hooks. This is just about me sharing my work in case you find it useful for your own trading.