.. meta:: :title: PyBroker :description: Algorithmic Trading in Python with Machine Learning :google-site-verification: XJpTXQaAdlEb2eAbndHa2ZmaUiOixSMaRusk-kKVKOQ .. title:: Algorithmic Trading in Python with Machine Learning .. raw:: html ================ PyBroker ================ .. raw:: html
python PyPI 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? 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! .. include:: install.rst 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): # 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/2022', end_date='7/1/2022') 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(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] > buy_threshold: ctx.buy_shares = 100 # Close the long position given my_model's latest prediction. 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) # 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) To learn how to use PyBroker, see the notebooks under the *User Guide*: .. toctree:: :maxdepth: 1 :caption: User Guide Installation notebooks/1. Getting Started with Data Sources notebooks/2. Backtesting a Strategy notebooks/3. Evaluating with Bootstrap Metrics notebooks/4. Ranking and Position Sizing notebooks/5. Writing Indicators notebooks/6. Training a Model notebooks/7. Creating a Custom Data Source notebooks/8. Applying Stops notebooks/9. Rebalancing Positions notebooks/10. Rotational Trading notebooks/FAQs `The notebooks above are also available on Github `_. .. toctree:: :maxdepth: 4 :caption: Reference Configuration Options .. toctree:: :maxdepth: 2 Indicators .. toctree:: :maxdepth: 4 Modules .. toctree:: :maxdepth: 1 Index Recommended Reading =================== The following is a list of essential books that provide background information on quantitative finance and algorithmic trading: * Lingjie Ma, `Quantitative Investing: From Theory to Industry `_ * Timothy Masters, `Testing and Tuning Market Trading Systems: Algorithms in C++ `_ * Stefan Jansen, `Machine Learning for Algorithmic Trading, 2nd Edition `_ * Ernest P. Chan, `Machine Trading: Deploying Computer Algorithms to Conquer the Markets `_ * Perry J. Kaufman, `Trading Systems and Methods, 6th Edition `_ .. toctree:: :maxdepth: 1 :caption: Other Information Changelog License Contact ======= .. image:: _static/email-image.png .. toctree:: :caption: Stock News & Alerts MoveAlerts.ai **AI-driven market analysis for the stocks you follow.** Receive real-time AI alerts and sentiment analysis for 10,000+ tickers at `www.movealerts.ai `_.