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

Popular Libraries

Hmmlearn

Introduction

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

Import Libraries

Import the hmmlearn and joblib libraries.

from AlgorithmImports import *
from hmmlearn import hmm
import joblib

You need the joblib library to store models.

Create Subscriptions

In the initialize method, subscribe to some data so you can train the hmmlearn 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, assume the market has only 2 regimes and the market returns follow a Gaussian distribution. Therefore, create a 2-component Hidden Markov Model with Gaussian emissions, which is equivalent to a Gaussian mixture model with 2 means.

To build the model, call the GaussianHMM constructor with the number of components, a covariance type, and the number of iterations:

# Initialize GaussianHMM with 2 states for capturing patterns, full covariance to model complex relationships, and 
# 100 iterations for sufficient training.
self.model = hmm.GaussianHMM(n_components=2, covariance_type="full", n_iter=100)

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 initialize method, make a history request.

# Fill a RollingWindow with 2 years of historical closing prices.
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 feature and label data for training by processing the RollingWindow data into a time series.
def get_features(self):
    training_df = np.array(list(self.training_data)[::-1])
    daily_pct_change = (np.roll(training_df, 1) - training_df) / training_df

    return daily_pct_change[1:].reshape(-1, 1)

def my_training_method(self):
    features = self.get_features()
    self.model.fit(features)

Set Training Schedule

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

# Train the model initially to provide a baseline for prediction and decision-making.
self.train(self.my_training_method)

To periodically re-train the model as your algorithm executes, in the initialize method, call the train 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 on_data method, add the current close price to the RollingWindow that holds the training data.

# Add the latest bar to the training data to ensure the model is trained 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 on_data method, get the most recent set of features and then call the predict method.

# Get the current feature set and make a prediction.
new_feature = self.get_features()
prediction = self.model.predict(new_feature)
prediction = float(prediction[-1])

You can use the label prediction to place orders.

# Place orders based on the forecasted market direction.
if prediction == 1:
    self.set_holdings(self._symbol, 1)
else:            
    self.liquidate(self._symbol)

Save Models

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

  1. Set the key name you want to store the model under in the Object Store.
  2. # Set the key to store the model in the Object Store so you can use it later.
    model_key = "model.hmm"
  3. Call the get_file_path method with the key.
  4. # Get the file path to correctly save and access the model in Object Store.
    file_name = self.object_store.get_file_path(model_key)

    This method returns the file path where the model will be stored.

  5. Call the dump method the file path.
  6. # Serialize the model and save it to the file.
    joblib.dump(self.model, file_name)

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

Load Models

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

# Load the hmmlearn model from Object Store to use its saved state and update it with new data if needed.
def initialize(self) -> None:
    if self.object_store.contains_key(model_key):
        file_name = self.object_store.get_file_path(model_key)
        self.model = joblib.load(file_name)

The contains_key method returns a boolean that represents if the model_key is in the Object Store. If the Object Store does not contain the model_key, save the model using the model_key before you proceed.

Examples

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

Example 1: Hidden Markov Model Regime Detection

The below algorithm makes use of Hmmlearn library to predict the future market regime through a hidden markov model using return data. If the regime is upward market, we hold SPY. Otherwise, we liquidate the portfolio. 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 hmmlearn import hmm
import joblib

class HmmlearnExampleAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2022, 7, 4)
        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.
        if self.object_store.contains_key("model.hmm"):
            file_name = self.object_store.get_file_path("model.hmm")
            self.model = joblib.load(file_name)
        # Create a 2-regime model otherwise to predict different variance regime markets.
        else:
            self.model = hmm.GaussianHMM(n_components=2, covariance_type="full", n_iter=100)

        # Train the model to use the prediction right away.
        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(self) -> None:
        # Train and predict the return data, which is more normalized and stationary.
        training_df = np.array(list(self.training_data)[::-1])
        daily_pct_change = (np.roll(training_df, 1) - training_df) / training_df

        return daily_pct_change[1:].reshape(-1, 1)

    def my_training_method(self) -> None:
        # Prepare the processed training data.
        features = self.get_features()
        # Recalibrate the model based on updated data.
        self.model.fit(features)

    def on_data(self, slice: Slice) -> None:
        if self.symbol in slice.bars:
            self.training_data.add(slice.bars[self.symbol].close)

        # Get prediction by the updated features.
        new_feature = self.get_features()
        prediction = self.model.predict(new_feature)
        prediction = float(prediction[-1])

        # If the predicted direction is going upward, buy SPY.
        if prediction == 1:
            self.set_holdings(self.symbol, 1)
        # If the predicted direction is going downward, liquidate.
        else:            
            self.liquidate(self.symbol)

    def on_end_of_algorithm(self) -> None:
        # Store the model to object store to retrieve it in other instances in case the algorithm stops.
        model_key = "model.hmm"
        file_name = self.object_store.get_file_path(model_key)
        joblib.dump(self.model, file_name)

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: