1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-06-06 05:51:17 +08:00

Add initial workflow_by_config

This commit is contained in:
Jactus
2020-11-16 17:29:26 +08:00
parent 42867264f3
commit 3e04ded750
4 changed files with 110 additions and 19 deletions

View File

@@ -46,15 +46,6 @@ if __name__ == "__main__":
"instruments": MARKET,
}
TRAINER_CONFIG = {
"train_start_time": "2008-01-01",
"train_end_time": "2014-12-31",
"validate_start_time": "2015-01-01",
"validate_end_time": "2016-12-31",
"test_start_time": "2017-01-01",
"test_end_time": "2020-08-01",
}
task = {
"model": {
"class": "LGBModel",
@@ -82,14 +73,8 @@ if __name__ == "__main__":
},
"segments": {
"train": ("2008-01-01", "2014-12-31"),
"valid": (
"2015-01-01",
"2016-12-31",
),
"test": (
"2017-01-01",
"2020-08-01",
),
"valid": ("2015-01-01", "2016-12-31"),
"test": ("2017-01-01", "2020-08-01"),
},
},
},
@@ -99,8 +84,12 @@ if __name__ == "__main__":
port_analysis_config = {
"strategy": {
"topk": 50,
"n_drop": 5,
"class": "TopkDropoutStrategy",
"module_path": "qlib.contrib.strategy.strategy",
"kwargs": {
"topk": 50,
"n_drop": 5,
}
},
"backtest": {
"verbose": False,

View File

@@ -0,0 +1,49 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import sys
from pathlib import Path
import qlib
import fire
import yaml
import pandas as pd
from qlib.config import REG_CN
from qlib.utils import exists_qlib_data, init_instance_by_config
from qlib.workflow import R
from qlib.workflow.record_temp import SignalRecord, PortAnaRecord
# worflow handler function
def workflow(config_path):
with open(config_path) as fp:
config = yaml.load(fp, Loader=yaml.FullLoader)
provider_uri = config.get("PROVIDER_URI")
if not exists_qlib_data(provider_uri):
print(f"Qlib data is not found in {provider_uri}")
sys.path.append(str(Path(__file__).resolve().parent.parent.joinpath("scripts")))
from get_data import GetData
GetData().qlib_data_cn(target_dir=provider_uri)
qlib.init(provider_uri=provider_uri, region=REG_CN)
# model initiaiton
model = init_instance_by_config(config.get("TASK")["model"])
dataset = init_instance_by_config(config.get("TASK")["dataset"])
# start exp
with R.start("workflow"):
model.fit(dataset)
# prediction
recorder = R.get_recorder()
sr = SignalRecord(model, dataset, recorder)
sr.generate()
# backtest
par = PortAnaRecord(recorder, config.get("PORT_ANALYSIS_CONFIG"))
par.generate()
if __name__ == "__main__":
fire.Fire(workflow)

View File

@@ -0,0 +1,52 @@
PROVIDER_URI: "~/.qlib/qlib_data/cn_data"
MARKET: &market csi300
BENCHMARK: &benchmark SH000300
DATA_HANDLER_CONFIG: &data_handerler_config
start_time: 2008-01-01
end_time: 2020-08-01
fit_start_time: 2008-01-01
fit_end_time: 2014-12-31
instruments: *market
TASK:
model:
class: LGBModel
module_path: qlib.contrib.model.gbdt
kwargs:
loss: mse
colsample_bytree: 0.8879
learning_rate: 0.0421
subsample: 0.8789
lambda_l1: 205.6999
lambda_l2: 580.9768
max_depth: 8
num_leaves: 210
num_threads: 20
dataset:
class: DatasetH
module_path: qlib.data.dataset
kwargs:
handler:
class: Alpha158
module_path: qlib.contrib.data.handler
kwargs: *data_handerler_config
segments:
train: [2008-01-01, 2014-12-31]
valid: [2015-01-01, 2016-12-31]
test: [2017-01-01, 2020-08-01]
record: [SignalRecord, PortAnaRecord]
PORT_ANALYSIS_CONFIG:
strategy:
class: TopkDropoutStrategy
module_path: qlib.contrib.strategy.strategy
kwargs:
topk: 50
n_drop: 5
backtest:
verbose: False
limit_threshold: 0.095
account: 100000000
benchmark: *benchmark
deal_price: close
open_cost: 0.0005
close_cost: 0.0015
min_cost: 5

View File

@@ -56,6 +56,7 @@ REQUIRED = [
"lightgbm",
"tornado",
"joblib>=0.17.0",
"fire>=0.3.1",
]
# Numpy include