Maintained

PyBroker guide

PyBroker bakes the safeguards ML strategies need, walk-forward training and bootstrapped statistics, into the framework.

Last reviewed

Quick answer

PyBroker is a Python framework built for machine-learning trading strategies. It trains models inside walk-forward windows so predictions never see future data, executes rules with a Numba-accelerated engine, and reports bootstrapped confidence intervals for metrics like Sharpe and drawdown. Data comes from Alpaca, Yahoo Finance or your own source.

PyBroker at a glance

Category
Research & vectorized backtesting
Language
Python
License
Apache-2.0 with Commons Clause
Does
Backtesting, Machine learning, Performance analytics
Markets
Stocks, ETFs, Crypto
Best for
ML-driven strategies where avoiding look-ahead and overfitting matters most.
In AlgoGP
Use the exported strategy object from your own code

Pick it if

ML-driven strategies where avoiding look-ahead and overfitting matters most.

Watch out for

Commons Clause license.

PyBroker strengths and limitations

Strengths

  • Walk-forward training built in.
  • Bootstrapped metrics show uncertainty, not just a point estimate.
  • Fast execution and caching.

Limitations

  • Commons Clause license.
  • Smaller community than vectorbt or Backtrader.

PyBroker example

pip install lib-pybroker
Install
import pybroker
from pybroker import Strategy, YFinance

def buy_dip(ctx):
    if not ctx.long_pos() and ctx.close[-1] < ctx.close[-2] * 0.97:
        ctx.buy_shares = ctx.calc_target_shares(1.0)
        ctx.hold_bars = 5

strategy = Strategy(YFinance(), start_date="2020-01-01", end_date="2024-01-01")
strategy.add_execution(buy_dip, ["AAPL", "MSFT"])
print(strategy.backtest().metrics_df)
Rule-based strategy skeleton.

Use PyBroker with AlgoGP

AlgoGP does not add PyBroker to projects automatically yet, but nothing stops you using them together. Every AlgoGP project exports plain Python with a framework-neutral strategy object, so you can call it from PyBroker yourself.

  1. 1

    Design and backtest in AlgoGP

    Describe the idea to the copilot and backtest it on AlgoGP’s normalized history.

  2. 2

    Add any bridge to get the adapter core

    Adding a bridged framework (for example Backtrader) writes frameworks/algogp_bridge.py, whose StrategyBridge.on_bar() returns the orders the strategy wants.

  3. 3

    Call it from PyBroker

    Feed PyBroker’s bars to StrategyBridge, place the returned orders with PyBroker, and report fills back with sync().

PyBroker connects to Alpaca, Yahoo Finance. Each guide shows how to create the API key, which you can store in the project’s Keys & secrets.

PyBroker: frequently asked questions

What is walk-forward analysis?

Repeatedly training on one period and testing on the next, so every result comes from data the model had not seen.

PyBroker alternatives

Independent guide. AlgoGP is not affiliated with PyBroker. Features, licenses and maintenance change; check the official documentation. Nothing here is investment advice.

Build a strategy