Training a Qlib Model and Updating Its Online Predictions
Summary
This example shows a two-stage workflow for keeping predictions current with Qlib’s online model tools. First, it trains a model using a CSI 300 gradient-boosting task configuration and marks the resulting model as the online model. Second, it calls the online utility to update predictions. The example wraps initialization, training, and prediction updates in a class, with settings for the data provider, region, and experiment name.
The document is an implementation example rather than a trading strategy or evaluation. It explains how to run the training step, the daily prediction-update step, or both in sequence, but gives no evidence about prediction accuracy, trading performance, update reliability, or how often models should be retrained. Its usefulness depends on having Qlib data and the relevant experiment configuration available. The code’s example configuration targets Chinese equities, but the document does not describe how to adapt the modeling approach to other markets or tasks.
Key ideas
- The workflow first trains a model and marks it for online use.
- A separate operation updates predictions using the online model utility.
- The example supports running training and prediction updates separately or in sequence.
- It provides no accuracy or trading results and does not specify a retraining schedule.
Tags
Full text
# update_online_pred.py
```py
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
This example shows how OnlineTool works when we need update prediction.
There are two parts including first_train and update_online_pred.
Firstly, we will finish the training and set the trained models to the `online` models.
Next, we will finish updating online predictions.
"""
import copy
import fire
import qlib
from qlib.constant import REG_CN
from qlib.model.trainer import task_train
from qlib.workflow.online.utils import OnlineToolR
from qlib.tests.config import CSI300_GBDT_TASK
task = copy.deepcopy(CSI300_GBDT_TASK)
task["record"] = {
"class": "SignalRecord",
"module_path": "qlib.workflow.record_temp",
}
class UpdatePredExample:
def __init__(
self,
provider_uri="~/.qlib/qlib_data/cn_data",
region=REG_CN,
experiment_name="online_srv",
task_config=task,
*,
trusted=False,
):
qlib.init(provider_uri=provider_uri, region=region)
self.experiment_name = experiment_name
self.online_tool = OnlineToolR(self.experiment_name, trusted=trusted)
self.task_config = task_config
def first_train(self):
rec = task_train(self.task_config, experiment_name=self.experiment_name)
self.online_tool.reset_online_tag(rec) # set to online model
def update_online_pred(self):
self.online_tool.update_online_pred()
def main(self):
self.first_train()
self.update_online_pred()
if __name__ == "__main__":
## to train a model and set it to online model, use the command below
# python update_online_pred.py first_train
## to update online predictions once a day, use the command below
# Only opt in for artifacts whose writer and store you trust.
# python update_online_pred.py --trusted=True update_online_pred
## to see the whole process with your own parameters, use the command below
# python update_online_pred.py --trusted=True --experiment_name="your_exp_name" main
fire.Fire(UpdatePredExample)
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.