1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| from qlib.data.dataset.handler import DataHandlerLP from qlib.data.dataset.loader import QlibDataLoader
from qlib.data.dataset.processor import Processor from qlib.utils import get_callable_kwargs from qlib.data.dataset import processor as processor_module from inspect import getfullargspec
CSI300_BENCH = "SH000300" CSI5_MARKET = "all" DATASET_A4_CLASS = "A4"
_DEFAULT_LEARN_PROCESSORS = [ {"class": "DropnaLabel"}, {"class": "CSZScoreNorm", "kwargs": {"fields_group": "label"}}, {"class": "Fillna"}, ]
def check_transform_proc(proc_l, fit_start_time, fit_end_time): """ 检查并处理数据处理器配置,为需要的处理器添加拟合时间范围 参数: proc_l: 处理器配置列表 fit_start_time: 拟合开始时间 fit_end_time: 拟合结束时间 返回: 处理后的处理器配置列表 """ new_l = [] for p in proc_l: if not isinstance(p, Processor): klass, pkwargs = get_callable_kwargs(p, processor_module) args = getfullargspec(klass).args if "fit_start_time" in args and "fit_end_time" in args: assert ( fit_start_time is not None and fit_end_time is not None ), "Make sure `fit_start_time` and `fit_end_time` are not None." pkwargs.update( { "fit_start_time": fit_start_time, "fit_end_time": fit_end_time, } ) proc_config = {"class": klass.__name__, "kwargs": pkwargs} if isinstance(p, dict) and "module_path" in p: proc_config["module_path"] = p["module_path"] new_l.append(proc_config) else: new_l.append(p) return new_l
class A4DL(QlibDataLoader): """ 自定义数据加载器:用于获取指定因子(MACD、成交量、价格波动范围、3日均值) """
def __init__(self, config=None, **kwargs): """初始化数据加载器""" _config = { "feature": self.get_feature_config(), } if config is not None: _config.update(config) super().__init__(config=_config,** kwargs)
@staticmethod def get_feature_config(config={}): """ 创建指定的因子配置:MACD、成交量、价格波动范围、3日均值 参数: config: 额外配置(预留) 返回: 特征表达式和名称的元组 """ macd_exp = '(EMA($close, 12) - EMA($close, 26))/$close - EMA((EMA($close, 12) - EMA($close, 26))/$close, 9)/$close' volume = '$volume' price_range = '$high-$low' ma3 = 'Mean($close, 3)' fields = [macd_exp, volume, price_range, ma3] names = ['MACD', 'VOLUME', 'RANGE', 'MA3'] return fields, names
class A4(DataHandlerLP): """ 自定义数据处理器类,继承自DataHandlerLP 用于处理数据加载、特征工程和标签生成 """ def __init__( self, instruments="all", start_time=None, end_time=None, freq="day", infer_processors=[], learn_processors=_DEFAULT_LEARN_PROCESSORS, fit_start_time=None, fit_end_time=None, process_type=DataHandlerLP.PTYPE_A, filter_pipe=None, inst_processors=None, **kwargs, ): infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time) learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
data_loader = { "class": "QlibDataLoader", "kwargs": { "config": { "feature": self.get_feature_config(), "label": kwargs.pop("label", self.get_label_config()), }, "filter_pipe": filter_pipe, "freq": freq, "inst_processors": inst_processors, }, } super().__init__( instruments=instruments, start_time=start_time, end_time=end_time, data_loader=data_loader, infer_processors=infer_processors, learn_processors=learn_processors, process_type=process_type,** kwargs, )
def get_feature_config(self): """获取特征配置(复用A4DL的数据加载器)""" return A4DL.get_feature_config()
def get_label_config(self): """ 获取标签配置 这里定义的标签是:下一期的收盘价(用于预测) """ return ["Ref($close, -1)"], ["LABEL0"]
def get_data_handler_config( start_time="2019-11-28", end_time="2020-09-23", fit_start_time="<dataset.kwargs.segments.train.0>", fit_end_time="<dataset.kwargs.segments.train.1>", instruments=CSI5_MARKET, ): """ 获取数据处理器配置 返回: 数据处理器配置字典 """ return { "start_time": start_time, "end_time": end_time, "fit_start_time": fit_start_time, "fit_end_time": fit_end_time, "instruments": instruments, }
def get_dataset_config( dataset_class=DATASET_A4_CLASS, train=("2019-11-28", "2020-07-23"), valid=("2020-07-24", "2020-08-23"), test=("2020-08-24", "2020-09-23"), handler_kwargs={"instruments": CSI5_MARKET}, ): """ 获取数据集配置 返回: 数据集配置字典 """ return { "class": "DatasetH", "module_path": "qlib.data.dataset", "kwargs": { "handler": { "class": dataset_class, "module_path": "mycode.myconfig", "kwargs": get_data_handler_config(**handler_kwargs), }, "segments": { "train": train, "valid": valid, "test": test, }, }, }
GBDT_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, }, }
def get_gbdt_task(dataset_kwargs={}, handler_kwargs={"instruments": CSI5_MARKET}): """ 获取GBDT任务配置(模型+数据集) 参数: dataset_kwargs: 数据集额外参数 handler_kwargs: 数据处理器额外参数 返回: 任务配置字典 """ return { "model": GBDT_MODEL, "dataset": get_dataset_config(** dataset_kwargs, handler_kwargs=handler_kwargs), }
CSI5_GBDT_TASK = get_gbdt_task(handler_kwargs={"instruments": CSI5_MARKET})
|