book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Popular Libraries

GPlearn

Introduction

This page explains how to build, train, deploy and store GPlearn models.

Import Libraries

Import the gplearn and joblib libraries.

from AlgorithmImports import *
from gplearn.genetic import SymbolicRegressor, SymbolicTransformer
import joblib

You need the joblib library to store models.

Create Subscriptions

In the Initializeinitialize method, subscribe to some data so you can train the GPLearn model and make predictions.

# Add a security and save a reference to its Symbol.
self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol

Build Models

In this example, build a genetic programming feature transformation model and a genetic programming regression prediction model using the following features and labels:

Data CategoryDescription
FeaturesDaily percent change of the close price of the SPY over the last 5 days
LabelsDaily percent return of the SPY over the next day

The following image shows the time difference between the features and labels:

Features and labels for training

Follow these steps to create a method to build the model:

  1. Declare a set of functions to use for feature engineering.
  2. # Pick some functions for feature engineering to explore transformations of the data.
    function_set = ['add', 'sub', 'mul', 'div',
                    'sqrt', 'log', 'abs', 'neg', 'inv',
                    'max', 'min']
  3. Call the SymbolicTransformer constructor with the preceding set of functions and then save it as a class variable.
  4. # Initialize a SymbolicTransformer with a function set for feature engineering.
    self.gp_transformer = SymbolicTransformer(function_set=function_set)
  5. Call the SymbolicRegressor constructor to instantiate the regression model.
  6. # Instantiate the regression model to perform symbolic regression, which involves 
    # mathematical expressions to fit the data and identify patterns.
    self.model = SymbolicRegressor()

Train Models

You can train the model at the beginning of your algorithm and you can periodically re-train it as the algorithm executes.

Warm Up Training Data

You need historical data to initially train the model at the start of your algorithm. To get the initial training data, in the Initializeinitialize method, make a history request.

# Fill a RollingWindow with 2 years of training data.
training_length = 252*2
self.training_data = RollingWindow[float](training_length)
history = self.history[TradeBar](self._symbol, training_length, Resolution.DAILY)
for trade_bar in history:
    self.training_data.add(trade_bar.close)

Define a Training Method

To train the model, define a method that fits the model with the training data.

# Prepare the feature and label data for training.
def get_features_and_labels(self, n_steps=5):
    training_df = list(self.training_data)[::-1]
    daily_pct_change = ((np.roll(training_df, -1) - training_df) / training_df)[:-1]

    features = []
    labels = []
    for i in range(len(daily_pct_change)-n_steps):
        features.append(daily_pct_change[i:i+n_steps])
        labels.append(daily_pct_change[i+n_steps])
    features = np.array(features)
    labels = np.array(labels)

    return features, labels

def my_training_method(self):
    features, labels = self.get_features_and_labels()

    # Perform feature engineering.
    self.gp_transformer.fit(features, labels)
    gp_features = self.gp_transformer.transform(features)
    new_features = np.hstack((features, gp_features))

    # Fit the regression model with transformed and raw features.
    self.model.fit(new_features, labels)

Set Training Schedule

To train the model at the beginning of your algorithm, in the Initializeinitialize method, call the Traintrain method.

# Train the model right now.
self.train(self.my_training_method)

To periodically re-train the model as your algorithm executes, in the Initializeinitialize method, call the Traintrain method as a Scheduled Event.

# Train the model every Sunday at 8:00 AM.
self.train(self.date_rules.every(DayOfWeek.SUNDAY), self.time_rules.at(8, 0), self.my_training_method)

Update Training Data

To update the training data as the algorithm executes, in the OnDataon_data method, add the current close price to the RollingWindow that holds the training data.

# Add the current close to the training data to ensure the model trains with the most recent market data.
def on_data(self, slice: Slice) -> None:
    if self._symbol in slice.bars:
        self.training_data.add(slice.bars[self._symbol].close)

Predict Labels

To predict the labels of new data, in the OnDataon_data method, get the most recent set of features and then call the predict method.

# Get the feature set.
features, _ = self.get_features_and_labels()

# Transform the features.
gp_features = self.gp_transformer.transform(features)
new_features = np.hstack((features, gp_features))

# Make a prediction.
prediction = self.model.predict(new_features)
prediction = float(prediction.flatten()[-1])

You can use the label prediction to place orders.

# Place orders based on the prediction.
if prediction > 0:
    self.set_holdings(self._symbol, 1)
elif prediction < 0:            
    self.set_holdings(self._symbol, -1)

Save Models

Follow these steps to save GPLearn models into the Object Store:

  1. Set the key names you want to store the models under in the Object Store.
  2. # Set the storage keys to something representative.
    transformer_model_key = "transformer"
    regressor_model_key = "regressor"
  3. Call the GetFilePathget_file_path method with the keys.
  4. # Get the file paths to save and access the models in the Object Store.
    transformer_file_name = self.object_store.get_file_path(transformer_model_key)
    regressor_file_name = self.object_store.get_file_path(regressor_model_key)

    This method returns the file paths where the models will be stored.

  5. Call the dump method the file paths.
  6. # Serialize and save the models.
    joblib.dump(self.gp_transformer, transformer_file_name)
    joblib.dump(self.model, regressor_file_name)

    If you dump the models using the joblib module before you save the models, you don't need to retrain the models.

Load Models

You can load and trade with pre-trained GPLearn models that you saved in the Object Store. To load a GPLearn model from the Object Store, in the Initializeinitialize method, get the file path to the saved model and then call the load method.

# Load the GPLearn model from Object Store to use its saved state and update with new data if needed.
def initialize(self) -> None:
    if self.object_store.contains_key(transformer_model_key) and self.object_store.contains_key(regressor_model_key):
        transformer_file_name = self.object_store.get_file_path(transformer_model_key)
        regressor_file_name = self.object_store.get_file_path(regressor_model_key)
        self.gp_transformer = joblib.load(transformer_file_name)
        self.model = joblib.load(regressor_file_name)

The ContainsKeycontains_key method returns a boolean that represents if transformer_model_key and regressor_model_key are in the Object Store. If the Object Store does not contain the keys, save the model using them before you proceed.

Examples

The following examples demonstrate some common practices for using GPLearn library.

Example 1: Genetic Learning

The below algorithm makes use of GPLearn library to predict the future price movement using the previous 5 OHLCV data. The model is trained using rolling 2-year data. To ensure the model applicable to the current market environment, we recalibrate the model on every Sunday.

from gplearn.genetic import SymbolicRegressor, SymbolicTransformer
import joblib

class GPlearnExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2022, 7, 4)
        self.set_end_date(2022, 7, 8)
        self.set_cash(100000)
        # Request SPY data for model training, prediction and trading.
        self.symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        
        # 2-year data to train the model.
        training_length = 252*2
        self.training_data = RollingWindow[float](training_length)
        # Warm up the training dataset to train the model immediately.
        history = self.history[TradeBar](self.symbol, training_length, Resolution.DAILY)
        for trade_bar in history:
            self.training_data.add(trade_bar.close)

        # Retrieve already trained model from object store to use immediately.
        transformer_model_key = "Transformer"
        regressor_model_key = "Regressor"
        if self.object_store.contains_key(transformer_model_key) and self.object_store.contains_key(regressor_model_key):
            transformer_file_name = self.object_store.get_file_path(transformer_model_key)
            regressor_file_name = self.object_store.get_file_path(regressor_model_key)

            self.gp_transformer = joblib.load(transformer_file_name)
            self.model = joblib.load(regressor_file_name)

        else:
            # Exhasting list of the non-linear transformation to apply on the variables.
            function_set = ['add', 'sub', 'mul', 'div',
                'sqrt', 'log', 'abs', 'neg', 'inv',
                'max', 'min']
            # Create and train the model to use the prediction right away.
            self.gp_transformer = SymbolicTransformer(function_set=function_set)
            self.model = SymbolicRegressor()
            self.train(self.my_training_method)

        # Recalibrate the model weekly to ensure its accuracy on the updated domain.
        self.train(self.date_rules.every(DayOfWeek.SUNDAY), self.time_rules.at(8,0), self.my_training_method)
        
    def get_features_and_labels(self, n_steps=5) -> None:
        # Train and predict the return data, which is more normalized and stationary.
        training_df = list(self.training_data)[::-1]
        daily_pct_change = ((np.roll(training_df, -1) - training_df) / training_df)[:-1]

        # Stack the data for 5-day OHLCV data per each sample to train with.
        features = []
        labels = []
        for i in range(len(daily_pct_change)-n_steps):
            features.append(daily_pct_change[i:i+n_steps])
            labels.append(daily_pct_change[i+n_steps])
        features = np.array(features)
        labels = np.array(labels)

        return features, labels

    def my_training_method(self) -> None:
        # Prepare the processed training data.
        features, labels = self.get_features_and_labels()

        # Feature engineering
        self.gp_transformer.fit(features, labels)
        gp_features = self.gp_transformer.transform(features)
        new_features = np.hstack((features, gp_features))

        # Fit the regression model with transformed and raw features.
        self.model.fit(new_features, labels)

    def on_data(self, slice: Slice) -> None:
        features, _ = self.get_features_and_labels()

        # Get transformed features
        gp_features = self.gp_transformer.transform(features)
        new_features = np.hstack((features, gp_features))

        # Get prediction using the latest 5 OHLCV.
        prediction = self.model.predict(new_features)
        prediction = float(prediction.flatten()[-1])

        # If the predicted direction is going upward, buy SPY.
        if prediction > 0:
            self.set_holdings(self.symbol, 1)
        # If the predicted direction is going downward, sell SPY.
        elif prediction < 0:            
            self.set_holdings(self.symbol, -1)

    def on_end_of_algorithm(self) -> None:
        # Store the model to object store to retrieve it in other instances in case the algorithm stops.
        transformer_model_key = "Transformer"
        regressor_model_key = "Regressor"
        transformer_file_name = self.object_store.get_file_path(transformer_model_key)
        regressor_file_name = self.object_store.get_file_path(regressor_model_key)
        joblib.dump(self.gp_transformer, transformer_file_name)
        joblib.dump(self.model, regressor_file_name)
        self.object_store.save(transformer_model_key)
        self.object_store.save(regressor_model_key)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: