Algorithmic Trading in Python with Machine Learning
Are you looking to enhance your trading strategies with the power of Python and
machine learning? Then you need to check out PyBroker! This Python framework
is 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.
- The ability to create and execute trading rules and models across multiple instruments with ease.
- Access to historical data from Alpaca, Yahoo Finance, AKShare, or from your own data provider.
- The option to train and backtest models using Walkforward Analysis, which simulates how the strategy would perform during actual trading.
- More reliable trading metrics that use randomized bootstrapping to provide more accurate results.
- Caching of downloaded data, indicators, and models to speed up your development process.
- Parallelized computations that enable faster performance.
With PyBroker, you'll have all the tools you need to create winning trading
strategies backed by data and machine learning. Start using PyBroker today and
take your trading to the next level!
Installation
PyBroker supports Python 3.9+ 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
Get 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):
high_10d = ctx.indicator('high_10d')
if not ctx.long_pos() and high_10d[-1] > high_10d[-2]:
ctx.buy_shares = 100
ctx.hold_bars = 5
ctx.stop_loss_pct = 2
strategy = Strategy(YFinance(), start_date='1/1/2022', end_date='7/1/2022')
strategy.add_execution(
exec_fn, ['AAPL', 'MSFT'], indicators=highest('high_10d', 'close', period=10))
result = strategy.backtest(warmup=20)
Model-based Strategy:
import pybroker
from pybroker import Alpaca, Strategy
def train_fn(train_data, test_data, ticker):
...
return trained_model
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/2022', end_date='7/1/2022')
strategy.add_execution(exec_fn, ['AAPL', 'MSFT'], models=my_model)
result = strategy.walkforward(timeframe='1m', windows=5, train_size=0.5)
User Guide
Online Documentation
The full reference documentation is hosted at www.pybroker.com.
(For Chinese users: 中文文档, courtesy of Albert King.)
Contact
AI-Powered Stock News
Stay informed with AI-powered news on top trending stocks. See www.trendninja.ai for the latest updates!