<jupyter_start><jupyter_text>train model<jupyter_code>###################################
# train model
###################################
DATA_HANDLER_CONFIG = {
    "dropna_label": True,
    "start_date": "2008-01-01",
    "end_date": "2020-08-01",
    "market": MARKET,
}

TRAINER_CONFIG = {
    "train_start_date": "2008-01-01",
    "train_end_date": "2014-12-31",
    "validate_start_date": "2015-01-01",
    "validate_end_date": "2016-12-31",
    "test_start_date": "2017-01-01",
    "test_end_date": "2020-08-01",
}

# use default DataHandler
# custom DataHandler, refer to: TODO: DataHandler api url
x_train, y_train, x_validate, y_validate, x_test, y_test = QLibDataHandlerClose(**DATA_HANDLER_CONFIG).get_split_data(**TRAINER_CONFIG)


MODEL_CONFIG = {
    "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,
}
# use default model
# custom Model, refer to: TODO: Model api url
model = LGBModel(**MODEL_CONFIG)
model.fit(x_train, y_train, x_validate, y_validate)
_pred = model.predict(x_test)
_pred = pd.DataFrame(_pred, index=x_test.index, columns=y_test.columns)

# backtest requires pred_score
pred_score = pd.DataFrame(index=_pred.index)
pred_score["score"] = _pred.iloc(axis=1)[0]<jupyter_output><empty_output><jupyter_text>backtest<jupyter_code>###################################
# backtest
###################################
STRATEGY_CONFIG = {
    "topk": 50,
    "buffer_margin": 230,
}
BACKTEST_CONFIG = {
    "verbose": False,
    "limit_threshold": 0.095,
    "account": 100000000,
    "benchmark": BENCHMARK,
    "deal_price": "close",
    "open_cost": 0.0005,
    "close_cost": 0.0015,
    "min_cost": 5,
    
}

# use default strategy
# custom Strategy, refer to: TODO: Strategy api url
strategy = TopkAmountStrategy(**STRATEGY_CONFIG)
report_normal, positions_normal = normal_backtest(pred_score, strategy=strategy, **BACKTEST_CONFIG)<jupyter_output><empty_output><jupyter_text>analyze<jupyter_code>###################################
# analyze
# If need a more detailed analysis, refer to: examples/train_and_bakctest.ipynb
###################################
analysis = dict()
analysis["sub_bench"] = risk_analysis(report_normal["return"] - report_normal["bench"])
analysis["sub_cost"] = risk_analysis(
    report_normal["return"] - report_normal["bench"] - report_normal["cost"]
)
analysis_df = pd.concat(analysis)  # type: pd.DataFrame
print(analysis_df)<jupyter_output><empty_output><jupyter_text>analyze graphs<jupyter_code>from qlib.contrib.report import analysis_model, analysis_position
# get label data
from qlib.data import D
pred_df_dates = pred_score.index.get_level_values(level='datetime')
features_df = D.features(D.instruments(MARKET), ['Ref($close, -1)/$close - 1'], pred_df_dates.min(), pred_df_dates.max())
features_df.columns = ['label']<jupyter_output><empty_output><jupyter_text>analysis position  report<jupyter_code>analysis_position.report_graph(report_normal)<jupyter_output><empty_output><jupyter_text>score IC<jupyter_code>pred_label = pd.concat([features_df, pred_score], axis=1, sort=True).reindex(features_df.index)
analysis_position.score_ic_graph(pred_label)<jupyter_output><empty_output><jupyter_text>cumulative return<jupyter_code>analysis_position.cumulative_return_graph(positions_normal, report_normal, features_df)<jupyter_output><empty_output><jupyter_text>risk analysis<jupyter_code>analysis_position.risk_analysis_graph(analysis_df, report_normal)<jupyter_output><empty_output><jupyter_text>rank label<jupyter_code>analysis_position.rank_label_graph(positions_normal, features_df, pred_df_dates.min(), pred_df_dates.max())<jupyter_output><empty_output><jupyter_text>analysis model  model performance<jupyter_code>analysis_model.model_performance_graph(pred_label)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.178743961352657, 'fraction_numerical': 0.034324942791762014, 'mean_word_length': 7.70353982300885, 'pattern_counts': {'":': 29, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '313771', 'license': 'MIT', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1360, 'n_words': 202, 'path': 'examples/train_backtest_analyze.ipynb', 'repo_name': 'kiminh/qlib', 'size': '3933'}