跳轉到

機器學習範例策略

本範例展示如何使用機器學習進行選股,透過 XGBoost 模型(經由 qlib 整合)預測股票未來報酬,並結合技術面趨勢過濾建構投資組合。

特徵工程

使用股價淨值比(PB)和本益比(PE)作為特徵,透過 feature.combine() 整合為特徵矩陣,以週為重新取樣頻率。

from finlab import data
from finlab.ml import feature as mlf

features = mlf.combine({
    'pb': data.get('price_earning_ratio:股價淨值比'),
    'pe': data.get('price_earning_ratio:本益比')
}, resample='W')

features.head()

標籤定義

標籤為未來 12 週的報酬率,使用 label.return_percentage() 計算。

from finlab.ml import label as mll

label = mll.return_percentage(features.index, resample='W', period=12)

模型訓練與預測

以 2020-01-01 為分界,之前的資料作為訓練集,之後的資料作為測試集。使用 qlib 提供的 XGBModel 進行訓練與預測。

import finlab.ml.qlib as q

is_train = features.index.get_level_values('datetime') < '2020-01-01'

X_train, y_train = features[is_train], label[is_train]
X_test = features[~is_train]

model = q.XGBModel()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

建構投資組合與回測

將模型預測結果結合 60 日均線趨勢過濾(收盤價高於 60 日均線才納入),選取預測值最高的前 10 檔股票,以季度為頻率進行回測。

from finlab.backtest import sim

close = data.get('price:收盤價')

position = y_pred[close > close.average(60)].is_largest(10)

sim(position, resample='Q')