-
Notifications
You must be signed in to change notification settings - Fork 3.6k
support optimization based strategy #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b9ff0f5
support optimization based strategy
b6d82d8
fix riskdata not found & update doc
af09b7a
refactor signal_strategy
3049b04
add portfolio example
5a45c1a
Update examples/portfolio/prepare_riskdata.py
evanzd 1003ca4
fix typo
evanzd 7227420
fix typo
evanzd e376af6
update doc
5d53182
fix riskmodel doc
evanzd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Portfolio Optimization Strategy | ||
|
||
## Introduction | ||
|
||
In `qlib/examples/benchmarks` we have various **alpha** models that predict | ||
the stock returns. We also use a simple rule based `TopkDropoutStrategy` to | ||
evaluate the investing performance of these models. However, such a strategy | ||
is too simple to control the portfolio risk like correlation and volatility. | ||
|
||
To this end, an optimization based strategy should be used to for the | ||
trade-off between return and risk. In this doc, we will show how to use | ||
`EnhancedIndexingStrategy` to maximize portfolio return while minimizing | ||
tracking error relative to a benchmark. | ||
|
||
|
||
## Preparation | ||
|
||
We use China stock market data for our example. | ||
|
||
1. Prepare CSI300 weight: | ||
|
||
```bash | ||
wget http://fintech.msra.cn/stock_data/downloads/csi300_weight.zip | ||
unzip -d ~/.qlib/qlib_data/cn_data csi300_weight.zip | ||
rm -f csi300_weight.zip | ||
``` | ||
|
||
2. Prepare risk model data: | ||
|
||
```bash | ||
python prepare_riskdata.py | ||
``` | ||
|
||
Here we use a **Statistical Risk Model** implemented in `qlib.model.riskmodel`. | ||
However users are strongly recommended to use other risk models for better quality: | ||
* **Fundamental Risk Model** like MSCI BARRA | ||
* [Deep Risk Model](https://arxiv.org/abs/2107.05201) | ||
|
||
|
||
## End-to-End Workflow | ||
|
||
You can finish workflow with `EnhancedIndexingStrategy` by running | ||
`qrun config_enhanced_indexing.yaml`. | ||
|
||
In this config, we mainly changed the strategy section compared to | ||
`qlib/examples/benchmarks/workflow_config_lightgbm_Alpha158.yaml`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
qlib_init: | ||
provider_uri: "~/.qlib/qlib_data/cn_data" | ||
region: cn | ||
market: &market csi300 | ||
benchmark: &benchmark SH000300 | ||
data_handler_config: &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 | ||
port_analysis_config: &port_analysis_config | ||
strategy: | ||
class: EnhancedIndexingStrategy | ||
module_path: qlib.contrib.strategy | ||
kwargs: | ||
model: <MODEL> | ||
dataset: <DATASET> | ||
riskmodel_root: ./riskdata | ||
backtest: | ||
start_time: 2017-01-01 | ||
end_time: 2020-08-01 | ||
account: 100000000 | ||
benchmark: *benchmark | ||
exchange_kwargs: | ||
limit_threshold: 0.095 | ||
deal_price: close | ||
open_cost: 0.0005 | ||
close_cost: 0.0015 | ||
min_cost: 5 | ||
task: | ||
model: | ||
class: LGBModel | ||
module_path: qlib.contrib.model.gbdt | ||
kwargs: | ||
loss: mse | ||
colsample_bytree: 0.8879 | ||
learning_rate: 0.2 | ||
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_handler_config | ||
segments: | ||
train: [2008-01-01, 2014-12-31] | ||
valid: [2015-01-01, 2016-12-31] | ||
test: [2017-01-01, 2020-08-01] | ||
record: | ||
- class: SignalRecord | ||
module_path: qlib.workflow.record_temp | ||
kwargs: | ||
model: <MODEL> | ||
dataset: <DATASET> | ||
- class: SigAnaRecord | ||
module_path: qlib.workflow.record_temp | ||
kwargs: | ||
ana_long_short: False | ||
ann_scaler: 252 | ||
- class: PortAnaRecord | ||
module_path: qlib.workflow.record_temp | ||
kwargs: | ||
config: *port_analysis_config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
import os | ||
evanzd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from qlib.data import D | ||
from qlib.model.riskmodel import StructuredCovEstimator | ||
|
||
|
||
def prepare_data(riskdata_root="./riskdata", T=240, start_time="2016-01-01"): | ||
|
||
universe = D.features(D.instruments("csi300"), ["$close"], start_time=start_time).swaplevel().sort_index() | ||
|
||
price_all = ( | ||
D.features(D.instruments("all"), ["$close"], start_time=start_time).squeeze().unstack(level="instrument") | ||
) | ||
|
||
# StructuredCovEstimator is a statistical risk model | ||
riskmodel = StructuredCovEstimator() | ||
|
||
for i in range(T - 1, len(price_all)): | ||
|
||
date = price_all.index[i] | ||
ref_date = price_all.index[i - T + 1] | ||
|
||
print(date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussions about the data preparation |
||
|
||
codes = universe.loc[date].index | ||
price = price_all.loc[ref_date:date, codes] | ||
|
||
# calculate return and remove extreme return | ||
ret = price.pct_change() | ||
you-n-g marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret.clip(ret.quantile(0.025), ret.quantile(0.975), axis=1, inplace=True) | ||
|
||
# run risk model | ||
F, cov_b, var_u = riskmodel.predict(ret, is_price=False, return_decomposed_components=True) | ||
|
||
# save risk data | ||
root = riskdata_root + "/" + date.strftime("%Y%m%d") | ||
os.makedirs(root, exist_ok=True) | ||
|
||
pd.DataFrame(F, index=codes).to_pickle(root + "/factor_exp.pkl") | ||
pd.DataFrame(cov_b).to_pickle(root + "/factor_cov.pkl") | ||
# for specific_risk we follow the convention to save volatility | ||
pd.Series(np.sqrt(var_u), index=codes).to_pickle(root + "/specific_risk.pkl") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
import qlib | ||
|
||
qlib.init(provider_uri="~/.qlib/qlib_data/cn_data") | ||
|
||
prepare_data() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.