Skip to main content

Algorithmic trading with machine learning

Project description

PyBroker

Algorithmic Trading in Python with Machine Learning

PyBroker is a Python framework for developing algorithmic trading strategies, especially those that use machine learning. With PyBroker, it is easy to write trading rules, build models, and analyze a strategy's performance.

Key Features

  • Fast backtesting engine built in NumPy with Numba acceleration.
  • Easily write trading rules and models that execute on multiple instruments.
  • Download historical data from Alpaca and Yahoo Finance.
  • Train and backtest models using Walkforward Analysis to simulate real trading.
  • Includes robust metrics calculated from randomized bootstrapping.
  • Caching of downloaded data, indicators, and models for faster development.
  • Parallelized computations for faster performance.

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 peek at what backtesting with PyBroker looks like:

   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

lib-pybroker-1.0.11.tar.gz (50.6 kB view details)

Uploaded Source

File details

Details for the file lib-pybroker-1.0.11.tar.gz.

File metadata

  • Download URL: lib-pybroker-1.0.11.tar.gz
  • Upload date:
  • Size: 50.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for lib-pybroker-1.0.11.tar.gz
Algorithm Hash digest
SHA256 d3ca228acd32f55196ecb6c7fa01f274d019425e8090d3ba5308663b171ea43a
MD5 5843463a795f7d409ebf69df52be6688
BLAKE2b-256 6260c0863a73cfafe85e96bbe37d2b714a7bdc3d12b62e1587a8c1ff4524aefb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page