Skip to main content
PyBroker

python Apache 2.0 with Commons Clause Documentation Status Package status Downloads Github stars Twitter

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 and accelerated with Numba.
  • Easy creation of trading rules and models for executing across multiple instruments.
  • Integration of trading signals across multiple time intervals, including daily, weekly, and monthly.
  • Access to historical data from Alpaca, Yahoo Finance, AKShare, or from your own data provider.
  • Model training and backtesting using Walkforward Analysis, which simulates how the strategy would perform during actual trading.
  • Reliable trading metrics that use randomized bootstrapping to provide more accurate results.
  • Parameter optimization with Optuna to select the best strategy parameters.
  • Caching of downloaded data, indicators, and models to speed up your development process.
  • Parallelized computation and training for faster performance.
  • Agent Skills 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:

   pip install -U lib-pybroker

Or you can clone the Git repository with:

   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:

   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:

   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

AI Agent Skills

PyBroker v2 now includes AI agent skills for coding agents:

Online Documentation

The full reference documentation is hosted at www.pybroker.com.

(For Chinese users: 中文文档, courtesy of Albert King.)

Contact

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lib_pybroker-2.0.0.tar.gz (369.9 kB view details)

Uploaded Source

File details

Details for the file lib_pybroker-2.0.0.tar.gz.

File metadata

  • Download URL: lib_pybroker-2.0.0.tar.gz
  • Upload date:
  • Size: 369.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for lib_pybroker-2.0.0.tar.gz
Algorithm Hash digest
SHA256 87e59824da727d51f3d47b0d84f529bf6df2f5069f2a03d2590801c29512c527
MD5 1024ce3d968e0e6a10c2c430c9a31970
BLAKE2b-256 42335d6d890d7434a5750b2a9ab09aae9c2c8d0d20e6d174c2d746ff758de8ed

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.1

1 file

This release

2.0.0 This release

1 file

1.2.14

1 file

1.2.13

1 file

1.2.12

1 file

1.2.11

1 file

1.2.10

1 file

1.2.9

1 file

1.2.8

1 file

1.2.7

1 file

1.2.6

1 file

1.2.5

1 file

1.2.4

1 file

1.2.3

1 file

1.2.2

1 file

1.2.1

1 file

1.2.0

1 file

1.1.40

1 file

1.1.39

1 file

1.1.38

1 file

1.1.37

1 file

1.1.36

1 file

1.1.35

1 file

1.1.34

1 file

1.1.33

1 file

1.1.32

1 file

1.1.31

1 file

1.1.30

1 file

1.1.29

1 file

1.1.28

1 file

1.1.27

1 file

1.1.26

1 file

1.1.25

1 file

1.1.24

1 file

1.1.23

1 file

1.1.22

1 file

1.1.21

1 file

1.1.20

1 file

1.1.19

1 file

1.1.18

1 file

1.1.17

1 file

1.1.16

1 file

1.1.15

1 file

1.1.14

1 file

1.1.13

1 file

1.1.12

1 file

1.1.11

1 file

1.1.10

1 file

1.1.9

1 file

1.1.8

1 file

1.1.7

1 file

1.1.6

1 file

1.1.5

1 file

1.1.4

1 file

1.1.3

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

1.0.21

1 file

1.0.20

1 file

1.0.19

1 file

1.0.18

1 file

1.0.17

1 file

1.0.16

1 file

1.0.15

1 file

1.0.14

1 file

1.0.13

1 file

1.0.12

1 file

1.0.11

1 file

1.0.10

1 file

1.0.9

1 file

1.0.8

1 file

1.0.7

1 file

1.0.6

1 file

1.0.5

1 file

1.0.4

1 file

1.0.3

1 file

1.0.2

1 file

1.0.1

1 file

1.0.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page