Start prototype
This commit is contained in:
		| @@ -1,3 +0,0 @@ | ||||
| import os | ||||
|  | ||||
| print('xxx123') | ||||
							
								
								
									
										94
									
								
								exps/trading/workflow_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								exps/trading/workflow_test.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | ||||
| ##################################################### | ||||
| # Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2019.01 # | ||||
| ##################################################### | ||||
| # Refer to: | ||||
| # - https://github.com/microsoft/qlib/blob/main/examples/workflow_by_code.ipynb | ||||
| # - https://github.com/microsoft/qlib/blob/main/examples/workflow_by_code.py | ||||
| # python exps/trading/workflow_test.py | ||||
| ##################################################### | ||||
| import sys, site | ||||
| from pathlib import Path | ||||
|  | ||||
| lib_dir = (Path(__file__).parent / '..' / '..' / 'lib').resolve() | ||||
| if str(lib_dir) not in sys.path: sys.path.insert(0, str(lib_dir)) | ||||
|  | ||||
| import qlib | ||||
| import pandas as pd | ||||
| from qlib.config import REG_CN | ||||
| from qlib.contrib.model.gbdt import LGBModel | ||||
| from qlib.contrib.data.handler import Alpha158 | ||||
| from qlib.contrib.strategy.strategy import TopkDropoutStrategy | ||||
| from qlib.contrib.evaluate import ( | ||||
|     backtest as normal_backtest, | ||||
|     risk_analysis, | ||||
| ) | ||||
| from qlib.utils import exists_qlib_data, init_instance_by_config | ||||
| from qlib.workflow import R | ||||
| from qlib.workflow.record_temp import SignalRecord, PortAnaRecord | ||||
| from qlib.utils import flatten_dict | ||||
|  | ||||
|  | ||||
| # use default data | ||||
| # NOTE: need to download data from remote: python scripts/get_data.py qlib_data_cn --target_dir ~/.qlib/qlib_data/cn_data | ||||
| provider_uri = "~/.qlib/qlib_data/cn_data"  # target_dir | ||||
| if not exists_qlib_data(provider_uri): | ||||
|     print(f"Qlib data is not found in {provider_uri}") | ||||
|     sys.path.append(str(scripts_dir)) | ||||
|     from get_data import GetData | ||||
|     GetData().qlib_data(target_dir=provider_uri, region=REG_CN) | ||||
| qlib.init(provider_uri=provider_uri, region=REG_CN) | ||||
|  | ||||
|  | ||||
| market = "csi300" | ||||
| benchmark = "SH000300" | ||||
|  | ||||
|  | ||||
| ################################### | ||||
| # train model | ||||
| ################################### | ||||
| data_handler_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": "QuantTransformer", | ||||
|         "module_path": "trade_models", | ||||
|         "kwargs": { | ||||
|             "loss": "mse", | ||||
|             "GPU": "0", | ||||
|             "metric": "loss", | ||||
|         }, | ||||
|     }, | ||||
|     "dataset": { | ||||
|         "class": "DatasetH", | ||||
|         "module_path": "qlib.data.dataset", | ||||
|         "kwargs": { | ||||
|             "handler": { | ||||
|                 "class": "Alpha158", | ||||
|                 "module_path": "qlib.contrib.data.handler", | ||||
|                 "kwargs": data_handler_config, | ||||
|             }, | ||||
|             "segments": { | ||||
|                 "train": ("2008-01-01", "2014-12-31"), | ||||
|                 "valid": ("2015-01-01", "2016-12-31"), | ||||
|                 "test": ("2017-01-01", "2020-08-01"), | ||||
|             }, | ||||
|         }, | ||||
|     }, | ||||
| } | ||||
|  | ||||
| # model initiaiton | ||||
| model = init_instance_by_config(task["model"]) | ||||
| dataset = init_instance_by_config(task["dataset"]) | ||||
|  | ||||
| # start exp to train model | ||||
| with R.start(experiment_name="train_model"): | ||||
|     R.log_params(**flatten_dict(task)) | ||||
|     model.fit(dataset) | ||||
|     R.save_objects(trained_model=model) | ||||
|     rid = R.get_recorder().id | ||||
		Reference in New Issue
	
	Block a user