Popular Libraries
XGBoost
Create Subscriptions
In the Initializeinitialize method, subscribe to some data so you can train the xgboost 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 gradient boost tree regression prediction model that uses the following features and labels:
| Data Category | Description |
|---|---|
| Features | The last 5 closing prices |
| Labels | The following day's closing price |
The following image shows the time difference between the features and labels:

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 historical closing prices.
training_length = 252*2
self.training_data = RollingWindow(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_and_labels(self, n_steps=5):
# Create a list of close prices from the rolling window.
close_prices = np.array(list(self.training_data)[::-1])
df = (np.roll(close_prices, -1) - close_prices) * 0.5 + close_prices * 0.5
df = df[:-1]
features = []
labels = []
for i in range(len(df)-n_steps):
features.append(df[i:i+n_steps])
labels.append(df[i+n_steps])
features = np.array(features)
labels = np.array(labels)
# Standardize the features and labels arrays.
features = (features - features.mean()) / features.std()
labels = (labels - labels.mean()) / labels.std()
# Load the NumPy array into a DMatrix object.
d_matrix = xgb.DMatrix(features, label=labels)
return d_matrix
def my_training_method(self):
# Instantiate the DMatrix object with the features and labels array.
d_matrix = self.get_features_and_labels()
# Define the parameters for the XGBoost model.
params = {
'booster': 'gbtree',
'colsample_bynode': 0.8,
'learning_rate': 0.1,
'lambda': 0.1,
'max_depth': 5,
'num_parallel_tree': 100,
'objective': 'reg:squarederror',
'subsample': 0.8,
}
# Create the model and fit it with the training data.
self.model = xgb.train(params, d_matrix, num_boost_round=10)
Set Training Schedule
To train the model at the beginning of your algorithm, in the Initializeinitialize method, call the Traintrain 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 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 TradeBar to the RollingWindow that holds the training data.
# Add the latest price 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:
# Update the training data with new most recent close price.
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 current feature set and make a prediction. new_d_matrix = self.get_features_and_labels(df) prediction = self.model.predict(new_d_matrix) prediction = prediction.flatten()
You can use the label prediction to place orders.
# Place orders based on the forecasted market direction.
if float(prediction[-1]) > float(prediction[-2]):
self.set_holdings(self._symbol, 1)
else:
self.set_holdings(self._symbol, -1)
Save Models
Follow these steps to save xgboost models into the Object Store:
- Set the key name of the model to be stored in the Object Store.
- Call the
GetFilePathget_file_pathmethod with the key. - Call the
dumpmethod the file path.
# Set the key to store the model in the Object Store so you can use it later. model_key = "model"
# 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.
# 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 xgboost models that saved in Object Store. To load a xgboost 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 xgboost model from the 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 ContainsKeycontains_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
XGBoost
library.
Example 1: Gradient Boosting Model
The below algorithm makes use of
XGBoost
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.
import xgboost as xgb
import joblib
class XGBoostExampleAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
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(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.
self._model_key = "xgboost-example-model"
if self.live_mode and self.object_store.contains_key(self._model_key):
file_name = self.object_store.get_file_path(self._model_key)
self.model = joblib.load(file_name)
else:
# 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_and_labels(self, n_steps=5) -> None:
# Train and predict the partial-differencing data, which is more stationary while more variance remaining.
close_prices = np.array(list(self.training_data)[::-1])
df = (np.roll(close_prices, -1) - close_prices) * 0.5 + close_prices * 0.5
df = df[:-1]
# Stack the data for 5-day OHLCV data per each sample to train with.
features = []
labels = []
for i in range(len(df)-n_steps):
features.append(df[i:i+n_steps])
labels.append(df[i+n_steps])
features = np.array(features)
labels = np.array(labels)
features = (features - features.mean()) / features.std()
labels = (labels - labels.mean()) / labels.std()
d_matrix = xgb.DMatrix(features, label=labels)
return d_matrix
def my_training_method(self) -> None:
# Prepare the processed training data.
d_matrix = self.get_features_and_labels()
# Recalibrate the model based on updated data.
params = {
'booster': 'gbtree',
'colsample_bynode': 0.8,
'learning_rate': 0.1,
'lambda': 0.1,
'max_depth': 5,
'num_parallel_tree': 100,
'objective': 'reg:squarederror',
'subsample': 0.8,
}
self.model = xgb.train(params, d_matrix, num_boost_round=2)
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_d_matrix = self.get_features_and_labels()
prediction = self.model.predict(new_d_matrix)
prediction = prediction.flatten()
# If the predicted direction is going upward, buy SPY.
if float(prediction[-1]) > float(prediction[-2]):
self.set_holdings(self.symbol, 1)
# If the predicted direction is going downward, sell SPY.
else:
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.
if not self.live_mode:
return
file_name = self.object_store.get_file_path(self._model_key)
joblib.dump(self.model, file_name)
self.object_store.save(self._model_key)