Machine Learning

Keras

Introduction

This page explains how to build, train, test, and store keras models.

Import Libraries

Import the keras libraries.

from tensorflow.keras import utils, models
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.saving import load_model

Get Historical Data

Get some historical market data to train and test the model. For example, to get data for the SPY ETF during 2020 and 2021, run:

qb = QuantBook()
symbol = qb.AddEquity("SPY", Resolution.Daily).Symbol
history = qb.History(symbol, datetime(2020, 1, 1), datetime(2022, 1, 1)).loc[symbol]

Prepare Data

You need some historical data to prepare the data for the model. If you have historical data, manipulate it to train and test the model. In this example, use the following features and labels:

Data CategoryDescription
FeaturesDaily percent change of the open, high, low, close, and volume 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 prepare the data:

  1. Call the pct_change and dropna methods.
  2. daily_pct_change = history.pct_change().dropna()
  3. Loop through the daily_pct_change DataFrame and collect the features and labels.
  4. n_steps = 5
    features = [] labels = [] for i in range(len(daily_pct_change)-n_steps): features.append(daily_pct_change.iloc[i:i+n_steps].values) labels.append(daily_pct_change['close'].iloc[i+n_steps])
  5. Convert the lists of features and labels into numpy arrays.
  6. features = np.array(features)
    labels = np.array(labels)
  7. Split the data into training and testing periods.
  8. train_length = int(len(features) * 0.7)
    X_train = features[:train_length]
    X_test = features[train_length:]
    y_train = labels[:train_length]
    y_test = labels[train_length:]

Train Models

You need to prepare the historical data for training before you train the model. If you have prepared the data, build and train the model. In this example, build a neural network model that predicts the future return of the SPY. Follow these steps to create the model:

  1. Call the Sequential constructor with a list of layers.
  2. model = Sequential([Dense(10, input_shape=(5,5), activation='relu'),
                        Dense(10, activation='relu'),
                        Flatten(),
                        Dense(1)])

    Set the input_shape of the first layer to (5, 5) because each sample contains the percent change of 5 factors (percent change of the open, high, low, close, and volume) over the previous 5 days. Call the Flatten constructor because the input is 2-dimensional but the output is just a single value.

  3. Call the compile method with a loss function, an optimizer, and a list of metrics to monitor.
  4. model.compile(loss='mse',
                  optimizer=RMSprop(0.001),
                  metrics=['mae', 'mse'])
  5. Call the fit method with the features and labels of the training dataset and a number of epochs.
  6. model.fit(X_train, y_train, epochs=5)

Test Models

You need to build and train the model before you test its performance. If you have trained the model, test it on the out-of-sample data. Follow these steps to test the model:

  1. Call the predict method with the features of the testing period.
  2. y_hat = model.predict(X_test)
  3. Plot the actual and predicted labels of the testing period.
  4. results = pd.DataFrame({'y': y_test.flatten(), 'y_hat': y_hat.flatten()})
    results.plot(title='Model Performance: predicted vs actual %change in closing price')
Keras model performance

Store Models

You can save and load keras models using the Object Store.

Save Models

Follow these steps to save models in the Object Store:

  1. Set the key name of the model to be stored in the Object Store. Add either a .keras extension for the native Keras format (recommended) or a .h5 extension.
  2. model_key = "model.keras"
  3. Call the GetFilePath method with the key.
  4. file_name = qb.ObjectStore.GetFilePath(model_key)

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

  5. Call the save method the file path.
  6. model.save(file_name)

Load Models

You must save a model into the Object Store before you can load it from the Object Store. If you saved a model, follow these steps to load it:

  1. Call the ContainsKey method with the model key.
  2. qb.ObjectStore.ContainsKey(model_key)

    This 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.

  3. Call the GetFilePath method with the key name.
  4. file_name = qb.ObjectStore.GetFilePath(model_key)

    This method returns the path where the model is stored.

  5. Call the load_model method with the file path.
  6. loaded_model = load_model(file_name)

    This method returns the saved model.

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: