Algorithmic trading with machine learning
Project description
Algorithmic Trading in Python with Machine Learning
PyBroker is a Python framework for backtesting algorithmic trading strategies, including strategies that use machine learning. With PyBroker, it is easy to write trading rules, build models, and analyze a strategy's performance. And it is made fast with the help of NumPy and Numba acceleration.
Some of PyBroker's key features are:
- Easy reuse of trading rules and models across multiple tickers.
- Model training and backtesting using Walkforward Analysis.
- Extensive coverage of trading metrics, calculated out-of-sample.
- Robust performance metrics calculated with randomized bootstrapping.
- Support for downloading historical data from Alpaca.
- Computing indicators in parallel for faster performance.
- Quicker development with caching of downloaded data, indicators, and trained models.
Installation
PyBroker supports Python 3.9+ on Windows, Mac, and Linux. You can install
PyBroker using pip:
pip install lib-pybroker
Or you can clone the Git repository with:
git clone https://github.com/edtechre/pybroker
A Quick Example
Code speaks louder than words! Here is a quick peek at what it looks like to write a strategy with PyBroker:
import pybroker
from pybroker import Alpaca, Strategy
def train_fn(train_data, test_data, ticker):
# 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')
# Open a long position given my_model's latest prediction.
if not ctx.long_pos() and preds[-1] > threshold:
ctx.buy_shares = 100
# Close the long position given my_model's latest prediction.
elif ctx.long_pos() and preds[-1] < threshold:
ctx.sell_all_shares()
alpaca = Alpaca(api_key=..., api_secret=...)
strategy = Strategy(alpaca, start_date='1/1/2022', end_date='7/1/2022')
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)
Online Documentation
To learn how to use PyBroker, head over to the online documentation.
Contact
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file lib-pybroker-1.0.1.tar.gz.
File metadata
- Download URL: lib-pybroker-1.0.1.tar.gz
- Upload date:
- Size: 46.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
219609f322816ab615293494ebbf6e664b447086173e804186e9136a3c0a4d53
|
|
| MD5 |
ff7f10ad9d4408f64e9dad4317bfbc3c
|
|
| BLAKE2b-256 |
2fc7345c7b04456f40855e6e08676b4157c179ea8002df5503962d47a8986b9a
|