Python Algotrading with Machine Learning
Tags: python, machinelearning, algotrading
Published: 2023-05-30 19:49:52

[Github Link](https://github.com/edtechre/pybroker/)

Hello, I would like to share with you [**PyBroker**](https://github.com/edtechre/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](https://numpy.org/) and accelerated with [Numba](https://numba.pydata.org/).
- The ability to create and execute trading rules and models across multiple instruments with ease.
- Access to historical data from [Alpaca](https://alpaca.markets/) and [Yahoo Finance](https://finance.yahoo.com/), or from [your own data provider](https://www.pybroker.com/en/latest/notebooks/7.%20Creating%20a%20Custom%20Data%20Source.html).
- The option to train and backtest models using [Walkforward Analysis](https://www.pybroker.com/en/latest/notebooks/6.%20Training%20a%20Model.html#Walkforward-Analysis), which simulates how the strategy would perform during actual trading.
- More reliable trading metrics that use randomized [bootstrapping](https://en.wikipedia.org/wiki/Bootstrapping_(statistics)) 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](https://www.investopedia.com/terms/b/backtesting.asp) trading strategies with PyBroker.

## Installing

To begin using [PyBroker](https://github.com/edtechre/pybroker/), you can install the library with pip:

```bash
pip install lib-pybroker
```

Or you can clone the Github repository:
```bash
git clone https://github.com/edtechre/pybroker
```

## 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:

```python
    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)
```

## 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:

```python
    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)
```

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](https://github.com/edtechre/pybroker/). Thank you for reading!