DEV Community

Cover image for Python Algotrading with Machine Learning
edtechre
edtechre

Posted on • Updated on

Python Algotrading with Machine Learning

Github Link

Hello, I would like to share with you PyBroker, a free and open Python framework that I developed for creating algorithmic trading strategies, including those that utilize machine learning. With PyBroker, you can easily develop and fine-tune trading rules, build powerful ML models, and gain valuable insights into your strategy's performance.

Some of the key features of PyBroker include:

  • 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 and Yahoo Finance, 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.

PyBroker was designed with machine learning in mind and supports training machine learning models using your favorite ML framework. Additionally, you can use PyBroker to write rule-based strategies.

This article will show some basic examples of how to implement and backtest trading strategies with PyBroker.

Installing

To begin using PyBroker, you can install the library with pip:

pip install lib-pybroker
Enter fullscreen mode Exit fullscreen mode

Or you can clone the Github repository:

git clone https://github.com/edtechre/pybroker
Enter fullscreen mode Exit fullscreen mode

Rule-based Example

Below is an example of a strategy that buys on a new 10-day high and holds the position for 5 days:

    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='1/1/2023'
    )
    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)
    # Get metrics:
    result.metrics_df.round(2)
Enter fullscreen mode Exit fullscreen mode

Model Example

This next example shows how to train a Linear Regression model that predicts the next day's return using the 20-day RSI, and then uses the model in a trading strategy:

    import pybroker
    import talib
    from pybroker import Strategy, YFinance
    from sklearn.linear_model import LinearRegression

    def train_slr(symbol, train_data, test_data):
        # Previous day close prices.
        train_prev_close = train_data['close'].shift(1)
        # Calculate daily returns.
        train_daily_returns = (
            (train_data['close'] - train_prev_close)
            / train_prev_close
        )
        # Predict next day's return.
        train_data['pred'] = train_daily_returns.shift(-1)
        train_data = train_data.dropna()
        # Train the LinearRegession model to predict
        # the next day's return given the 20-day RSI.
        X_train = train_data[['rsi_20']]
        y_train = train_data[['pred']]
        model = LinearRegression()
        model.fit(X_train, y_train)
        return model

    def exec_fn(ctx):
        preds = ctx.preds('slr')
        # Open a long position given the latest prediction.
        if not ctx.long_pos() and preds[-1] > 0:
            ctx.buy_shares = 100
        # Close the long position given the latest prediction.
        elif ctx.long_pos() and preds[-1] < 0:
            ctx.sell_all_shares()

    # Register a 20-day RSI indicator with PyBroker.
    rsi_20 = pybroker.indicator(
        'rsi_20', 
        lambda data: talib.RSI(data.close, timeperiod=20)
    )
    # Register the model and its training function
    # with PyBroker.
    model_slr = pybroker.model(
        'slr', train_slr, indicators=[rsi_20])
    strategy = Strategy(
        YFinance(), 
        start_date='1/1/2022', 
        end_date='1/1/2023'
    )
    strategy.add_execution(
        exec_fn, ['NVDA', 'AMD'], models=model_slr)

    # Run the backtest using a 50/50 train/test split.
    result = strategy.backtest(warmup=20, train_size=0.5)
    # Get metrics:
    result.metrics_df.round(2)
Enter fullscreen mode Exit fullscreen mode

We are also not limited to just building linear regression models in PyBroker. We can train other model types such as gradient boosted machines, neural networks, or any other architecture that we choose with our preferred ML framework.

If you're interested in learning more, you can find additional examples and tutorials on the Github page. Thank you for reading!

Top comments (1)

Collapse
 
stankukucka profile image
Stan Kukučka

@edtechre this is dope, i'll try it out