https://github.com/edtechre/pybroker
Algorithmic Trading in Python with Machine Learning
https://github.com/edtechre/pybroker
ai algorithmic-trading algotrading artificial-intelligence backtesting crypto cryptocurrency data-science finance framework investment machine-learning python quantitative-finance stocks trading trading-strategies
Last synced: 1 day ago
JSON representation
Algorithmic Trading in Python with Machine Learning
- Host: GitHub
- URL: https://github.com/edtechre/pybroker
- Owner: edtechre
- License: other
- Created: 2023-01-16T01:11:52.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2026-11-21T19:21:36.000Z (5 days ago)
- Last Synced: 2026-11-22T20:54:25.765Z (4 days ago)
- Topics: ai, algorithmic-trading, algotrading, artificial-intelligence, backtesting, crypto, cryptocurrency, data-science, finance, framework, investment, machine-learning, python, quantitative-finance, stocks, trading, trading-strategies
- Language: Python
- Homepage: https://www.pybroker.com
- Size: 8.42 MB
- Stars: 3,726
- Watchers: 45
- Forks: 465
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Claude: CLAUDE.md
Awesome Lists containing this project
- awesome-quant - edtechre/pybroker - Algorithmic Trading in Python with Machine Learning `Python` · ⭐ 3,430 · forks 0 · updated 2026-05-11 · license NOASSERTION (Machine Learning for Trading)
- awesome-systematic-trading - PyBroker - Algorithmic trading in Python with machine learning: rule based and model driven strategies, walkforward analysis and bootstrapped significance tests on the results (Backtesting and Live Trading / General - Event Driven Frameworks)
- awesome-systematic-trading - PyBroker -   | Python | - Algorithmic Trading in Python with Machine Learning (Backtest + live trading / General purpose)
- fucking-awesome-machine-learning - PyBroker - Algorithmic Trading with Machine Learning. (Python)
- awesome-machine-learning - PyBroker - Algorithmic Trading with Machine Learning. (Python / General-Purpose Machine Learning)
- awesome-machine-learning - PyBroker - Algorithmic Trading with Machine Learning. (Python / General-Purpose Machine Learning)
- awesome-quant - PyBroker - `Python` - Algorithmic Trading with Machine Learning. (Trading & Backtesting)
- awesome-machine-learning - PyBroker - Algorithmic Trading with Machine Learning. (Python)
- awesome-machine-learning - PyBroker - Algorithmic Trading with Machine Learning. (Python / General-Purpose Machine Learning)
- github-ultimate-repos - edtechre/pybroker - | ⭐ 3,658★ | 🍴 459 | 💻 `Python`🔗 <https://github.com/edtechre/pybroker>📝 Algorithmic Trading in Python with Machine Learning⚙️ `FIX/ISO 20022 message protocol ingestion -> L2/L3 order book match engine -> continuous double auction execution -> double-entry ledger balance settlement.` (📜 Master Categorized Repository Directory (100% Clickable Links) / [35] FINTECH, CORE BANKING, PAYMENT PROTOCOLS & ALGORITHMIC TRADING)
- awesome-projects - pybroker - Algorithmic Trading in Python with Machine Learning (Uncategorized / Uncategorized)
- awesome-quant-ai - pybroker - Backtesting framework designed for ML-driven strategies (Tools and Platforms)
README

[](https://www.python.org/)
[](https://www.pybroker.com/en/latest/license.html)
[](https://www.pybroker.com/en/latest/?badge=latest)
[](https://github.com/edtechre/pybroker/actions)
[](https://pepy.tech/project/lib-pybroker)
[](https://github.com/edtechre/pybroker/)
[](https://twitter.com/intent/follow?screen_name=libpybroker)
## Algorithmic Trading in Python with Machine Learning
Are you looking to enhance your trading strategies with the power of Python and
machine learning? **PyBroker** is a Python framework
designed for developing algorithmic trading strategies, with a focus on
strategies that use machine learning. With PyBroker, you can easily create and
fine-tune trading rules, build powerful models, and gain valuable insights into
your strategy’s performance.
## Key Features
* A super-fast backtesting engine built in [NumPy](https://numpy.org/) and accelerated with [Numba](https://numba.pydata.org/).
* Easy creation of trading rules and models for executing across multiple instruments.
* Integration of trading signals across [multiple time intervals](https://www.pybroker.com/en/latest/notebooks/15.%20Multiple%20Time%20Intervals.html), including daily, weekly, and monthly.
* Access to historical data from [Alpaca](https://alpaca.markets/), [Yahoo Finance](https://finance.yahoo.com/), [AKShare](https://github.com/akfamily/akshare), or from [your own data provider](https://www.pybroker.com/en/latest/notebooks/7.%20Creating%20a%20Custom%20Data%20Source.html).
* Model training and backtesting using [Walkforward Analysis](https://www.pybroker.com/en/latest/notebooks/6.%20Training%20a%20Model.html#Walkforward-Analysis), which simulates how the strategy would perform during actual trading.
* Reliable trading metrics that use randomized [bootstrapping](https://en.wikipedia.org/wiki/Bootstrapping_(statistics)) to provide more accurate results.
* [Parameter optimization](https://www.pybroker.com/en/latest/notebooks/12.%20Parameter%20Optimization.html) with [Optuna](https://optuna.org/) to select the best strategy parameters.
* [Caching](https://www.pybroker.com/en/latest/notebooks/1.%20Getting%20Started%20with%20Data%20Sources.html#Caching-Data) of downloaded data, indicators, and models to speed up your development process.
* [Parallelized](https://www.pybroker.com/en/latest/notebooks/11.%20Configuring%20Parallelization.html) computation and training for faster performance.
* [Agent Skills](https://www.pybroker.com/en/latest/agent-skills.html) that help AI agents write trading strategies and backtests using PyBroker.
PyBroker provides you with the tools to build, test, and evaluate algorithmic trading strategies backed by machine learning.
## Installation
PyBroker supports Python 3.11+ on Windows, Mac, and Linux. You can install
PyBroker using ``pip``:
```bash
pip install -U lib-pybroker
```
Or you can clone the Git repository with:
```bash
git clone https://github.com/edtechre/pybroker
```
## A Quick Example
Here's a glimpse of what backtesting with PyBroker looks like with these code
snippets:
**Rule-based Strategy**:
```python
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)
```
**Model-based Strategy**:
```python
import pybroker
from pybroker import Alpaca, Strategy
def train_fn(symbol, train_data, test_data):
# Train the model using indicators stored in train_data.
...
return trained_model
# Register the model and its training function with PyBroker.
my_model = pybroker.model('my_model', train_fn, indicators=[...])
def exec_fn(ctx):
preds = ctx.preds('my_model')
if not ctx.long_pos() and preds[-1] > buy_threshold:
ctx.buy_shares = 100
elif ctx.long_pos() and preds[-1] < sell_threshold:
ctx.sell_all_shares()
alpaca = Alpaca(api_key=..., api_secret=...)
strategy = Strategy(alpaca, start_date='1/1/2025', end_date='8/1/2026')
strategy.add_execution(exec_fn, ['AAPL', 'MSFT'], models=my_model)
# Run Walkforward Analysis on 1 minute data using 5 windows with 50/50 train/test data.
result = strategy.walkforward(timeframe='1m', windows=5, train_size=0.5)
```
## User Guide
- [Getting Started with Data Sources](https://www.pybroker.com/en/latest/notebooks/1.%20Getting%20Started%20with%20Data%20Sources.html)
- [Backtesting a Strategy](https://www.pybroker.com/en/latest/notebooks/2.%20Backtesting%20a%20Strategy.html)
- [Evaluating with Bootstrap Metrics](https://www.pybroker.com/en/latest/notebooks/3.%20Evaluating%20with%20Bootstrap%20Metrics.html)
- [Ranking Long and Short Signals](https://www.pybroker.com/en/latest/notebooks/4.%20Ranking%20Long%20and%20Short%20Signals.html)
- [Writing Indicators](https://www.pybroker.com/en/latest/notebooks/5.%20Writing%20Indicators.html)
- [Training a Model](https://www.pybroker.com/en/latest/notebooks/6.%20Training%20a%20Model.html)
- [Creating a Custom Data Source](https://www.pybroker.com/en/latest/notebooks/7.%20Creating%20a%20Custom%20Data%20Source.html)
- [Applying Stops](https://www.pybroker.com/en/latest/notebooks/8.%20Applying%20Stops.html)
- [Rebalancing Positions](https://www.pybroker.com/en/latest/notebooks/9.%20Rebalancing%20Positions.html)
- [Rotational Trading](https://www.pybroker.com/en/latest/notebooks/10.%20Rotational%20Trading.html)
- [Configuring Parallelization](https://www.pybroker.com/en/latest/notebooks/11.%20Configuring%20Parallelization.html)
- [Parameter Optimization](https://www.pybroker.com/en/latest/notebooks/12.%20Parameter%20Optimization.html)
- [Margin Trading](https://www.pybroker.com/en/latest/notebooks/13.%20Margin%20Trading.html)
- [Modeling Slippage](https://www.pybroker.com/en/latest/notebooks/14.%20Modeling%20Slippage.html)
- [Multiple Time Intervals](https://www.pybroker.com/en/latest/notebooks/15.%20Multiple%20Time%20Intervals.html)
- [Time Series Models](https://www.pybroker.com/en/latest/notebooks/16.%20Time%20Series%20Models.html)
- [Multi-Symbol Models](https://www.pybroker.com/en/latest/notebooks/17.%20Multi-Symbol%20Models.html)
- [Dynamic Symbol Selection](https://www.pybroker.com/en/latest/notebooks/18.%20Dynamic%20Symbol%20Selection.html)
- [Agent Skills](https://www.pybroker.com/en/latest/agent-skills.html)
- [FAQs](https://www.pybroker.com/en/latest/notebooks/FAQs.html)
## AI Agent Skills
PyBroker v2 now includes [AI agent skills](https://www.pybroker.com/en/latest/agent-skills.html) for coding agents:
- [Strategy Creator](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-strategy-creator)
- [Indicator Creator](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-indicator-creator)
- [Model Trainer](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-model-trainer)
- [Parameter Optimization](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-optimize)
- [Multi-Interval Strategies](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-multi-interval)
- [Rotational Trading](https://www.pybroker.com/en/latest/agent-skills.html#skill-pybroker-rotational-trading)
## Online Documentation
[The full reference documentation is hosted at **www.pybroker.com**.](https://www.pybroker.com)
(For Chinese users: [中文文档](https://www.pybroker.com/zh_CN/latest/), courtesy of [Albert King](https://github.com/albertandking).)
## Contact
