microsoft / qlib

Qlib is an AI-oriented quantitative investment platform that aims to realize the potential, empower research, and create value using AI technologies in quantitative investment, from exploring ideas to implementing productions. Qlib supports diverse machine learning modeling paradigms. including supervised learning, market dynamics modeling, and RL.
https://qlib.readthedocs.io/en/latest/
MIT License
14.55k stars 2.53k forks source link

How to run alpha360? #1749

Closed DanielKui closed 3 months ago

DanielKui commented 4 months ago

` ###################################

train model

################################### data_handler_config = { "start_time": "2008-01-01", "end_time": "2020-08-01", "fit_start_time": "2015-01-01", "fit_end_time": "2018-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": "Alpha360", "module_path": "qlib.contrib.data.handler", "kwargs": data_handler_config, }, "segments": { "train": ("2008-01-01", "2014-12-31"), "valid": ("2015-01-01", "2015-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

###################################

prediction, backtest & analysis

################################### port_analysis_config = { "executor": { "class": "SimulatorExecutor", "module_path": "qlib.backtest.executor", "kwargs": { "time_per_step": "day", "generate_portfolio_metrics": True, }, }, "strategy": { "class": "TopkDropoutStrategy", "module_path": "qlib.contrib.strategy.signal_strategy", "kwargs": { "model": model, "dataset": dataset, "topk": 50, "n_drop": 5, }, }, "backtest": { "start_time": "2015-01-01", "end_time": "2020-08-01", "account": 100000000, "benchmark": benchmark, "exchange_kwargs": { "freq": "day", "limit_threshold": 0.095, "deal_price": "close", "open_cost": 0.0005, "close_cost": 0.0015, "min_cost": 5, }, }, }

backtest and analysis

with R.start(experiment_name="backtest_analysis"): recorder = R.get_recorder(recorder_id=rid, experiment_name="train_model") model = recorder.load_object("trained_model")

import csv

feature_importance = model.get_feature_importance()
fea_expr, fea_name = dataset.handler.get_feature_config()
feature_importance = {fea_name[int(i.split("_")[1])]: v for i,v in feature_importance.items()}

with open('C:\\alpha360_feature.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=feature_importance.keys())
    writer.writeheader()
    writer.writerow(feature_importance)

# prediction
recorder = R.get_recorder()
ba_rid = recorder.id
sr = SignalRecord(model, dataset, recorder)
sr.generate()

# backtest & analysis
par = PortAnaRecord(recorder, port_analysis_config, "day")
par.generate()`

I only change class to Alpha360, but the backtest always throw exception as: image

who can provide a complete example of running alpha360?

DanielKui commented 4 months ago

image

DanielKui commented 4 months ago

着急,谁能帮帮我啊 who can help me

DanielKui commented 4 months ago

@you-n-g @igor17400

DanielKui commented 4 months ago

I dont know why there are two datetime colums in pred.pkl? image