Skip to content

Commit

Permalink
update docs and code
Browse files Browse the repository at this point in the history
  • Loading branch information
you-n-g committed Sep 22, 2020
1 parent c41efa9 commit d3e22a6
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 517 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
__pycache__/

*.pyc
*.so
*.ipynb
.ipynb_checkpoints
_build
build/
dist/


*.pkl
Expand All @@ -16,6 +19,9 @@ _build
.nvimrc
.vscode

qlib/data/_libs/expanding.cpp
qlib/data/_libs/rolling.cpp
examples/estimator/estimator_example/

*.egg-info/

Expand Down
Binary file removed docs/_static/img/topk_margin.png
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/component/backtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ Example
===========================

Users need to generate a prediction score(a pandas DataFrame) with MultiIndex<instrument, datetime> and a `score` column. And users need to assign a strategy used in backtest, if strategy is not assigned,
a `TopkAmountStrategy` strategy with `(topk=20, buffer_margin=150, risk_degree=0.95, limit_threshold=0.0095)` will be used.
If ``Strategy`` module is not user's interested part, `TopkAmountStrategy` is enough.
a `TopkDropoutStrategy` strategy with `(topk=50, n_drop=5, risk_degree=0.95, limit_threshold=0.0095)` will be used.
If ``Strategy`` module is not user's interested part, `TopkDropoutStrategy` is enough.

The simple example with default strategy is as follows.

.. code-block:: python
from qlib.contrib.evaluate import backtest
# pred_score is the prediction score
report, positions = backtest(pred_score, topk=50, margin=0.5, verbose=False, limit_threshold=0.0095)
report, positions = backtest(pred_score, topk=50, n_drop=0.5, verbose=False, limit_threshold=0.0095)
To know more about backtesting with specific strategy, please refer to `Strategy <strategy.html>`_.

Expand Down
16 changes: 8 additions & 8 deletions docs/component/estimator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ First, Write a simple configuration file as following,
test_start_date: 2017-01-01
test_end_date: 2020-08-01
strategy:
class: TopkAmountStrategy
class: TopkDropoutStrategy
args:
topk: 50
buffer_margin: 230
n_drop: 5
backtest:
normal_backtest_args:
verbose: False
Expand Down Expand Up @@ -473,13 +473,13 @@ Users can specify strategy through a config file, for example:
.. code-block:: YAML
strategy :
class: TopkAmountStrategy
class: TopkDropoutStrategy
args:
topk: 50
buffer_margin: 300
n_drop: 5
- `class`
The strategy class, str type, should be a subclass of `qlib.contrib.strategy.strategy.BaseStrategy`. The default value is `TopkAmountStrategy`.
The strategy class, str type, should be a subclass of `qlib.contrib.strategy.strategy.BaseStrategy`. The default value is `TopkDropoutStrategy`.

- `module_path`
The module location, str type, absolute url is also supported, and absolute path is also supported, indicates the location of the policy class implementation.
Expand All @@ -488,10 +488,10 @@ Users can specify strategy through a config file, for example:
Parameters used for ``Trainer`` initialization.

- `topk`
A threshold for buying rank, integer type, determines the threshold for the topk-margin strategy buy rank. The default value is 30.
The number of stocks in the portfolio

- `margin`
The sell buffer threshold, integer type, determines the buffer threshold, those who are outside the margin will be sold. The default value is 350.
- `n_drop`
Number of stocks to be replaced in each trading date

Custom Strategy
^^^^^^^^^^^^^^^^^^^
Expand Down
38 changes: 4 additions & 34 deletions docs/component/strategy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,7 @@ Users can inherit `WeightStrategyBase` and implement the inteface `generate_targ
Implemented Strategy
====================

Qlib provides several implemented strategy classes, such as `TopkWeightStrategy`, `TopkAmountStrategy` and `TopkDropoutStrategy`.

TopkWeightStrategy
------------------
`TopkWeightStrategy` is a subclass of `WeightStrategyBase` and implements the interface `generate_target_weight_position`.

The implemented interface `generate_target_weight_position` adopts the ``Topk-Margin`` algorithm to calculate the target position, it ensures that the weight of each stock is as even as possible.

.. note::
``Topk-Margin`` algorithm:

- `Topk`: The number of stocks held
- `margin`: Rank threshold of prediction score


Currently, the number of held stocks is `Topk`.
On each trading day, the held stocks with the rank of prediction score outside the threshold `margin` will be sold, and the same number of unheld stocks with best prediction score will be bought.

.. image:: ../_static/img/topk_margin.png
:alt: Topk-Margin




TopkAmountStrategy
------------------
`TopkAmountStrategy` is a subclass of `BaseStrategy` and implement the interface `generate_order_list` whose process is as follows.

- Adopt the the ``Topk-Margin`` algorithm to calculate the target amount of each stock
- Generate the order list from the target amount

Qlib provides several implemented strategy classes `TopkDropoutStrategy`.


TopkDropoutStrategy
Expand Down Expand Up @@ -115,11 +85,11 @@ Usage & Example

.. code-block:: python
from qlib.contrib.strategy.strategy import TopkAmountStrategy
from qlib.contrib.strategy.strategy import TopkDropoutStrategy
from qlib.contrib.evaluate import backtest
STRATEGY_CONFIG = {
"topk": 50,
"buffer_margin": 230,
"n_drop": 5,
}
BACKTEST_CONFIG = {
"verbose": False,
Expand All @@ -131,7 +101,7 @@ Usage & Example
# use default strategy
# custom Strategy, refer to: TODO: Strategy API url
strategy = TopkAmountStrategy(**STRATEGY_CONFIG)
strategy = TopkDropoutStrategy(**STRATEGY_CONFIG)
# pred_score is the prediction score output by Model
report_normal, positions_normal = backtest(
Expand Down
4 changes: 2 additions & 2 deletions docs/hidden/online.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ Followings are two examples for a TopkAmountStrategy
.. code-block:: YAML
strategy:
class: TopkAmountStrategy
class: TopkDropoutStrategy
module_path: qlib.contrib.strategy.strategy
args:
topk: 100
buffer_margin: 300
n_drop: 10
Generated files
------------------
Expand Down
5 changes: 3 additions & 2 deletions examples/estimator/estimator_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ trainer:
test_start_date: 2017-01-01
test_end_date: 2020-08-01
strategy:
class: TopkAmountStrategy
class: TopkDropoutStrategy
args:
topk: 50
buffer_margin: 230
n_drop: 5
backtest:
normal_backtest_args:
verbose: False
Expand All @@ -52,3 +52,4 @@ qlib_data:
# when testing, please modify the following parameters according to the specific environment
provider_uri: "~/.qlib/qlib_data/cn_data"
region: "cn"
redis_port: 4312
4 changes: 2 additions & 2 deletions examples/estimator/estimator_config_dnn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ trainer:
test_start_date: 2017-01-01
test_end_date: 2020-08-01
strategy:
class: TopkAmountStrategy
class: TopkDropoutStrategy
args:
topk: 50
buffer_margin: 230
n_drop: 5
backtest:
normal_backtest_args:
verbose: False
Expand Down
6 changes: 3 additions & 3 deletions examples/train_and_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from qlib.config import REG_CN
from qlib.contrib.model.gbdt import LGBModel
from qlib.contrib.estimator.handler import QLibDataHandlerClose
from qlib.contrib.strategy.strategy import TopkAmountStrategy
from qlib.contrib.strategy.strategy import TopkDropoutStrategy
from qlib.contrib.evaluate import (
backtest as normal_backtest,
risk_analysis,
Expand Down Expand Up @@ -90,7 +90,7 @@
###################################
STRATEGY_CONFIG = {
"topk": 50,
"buffer_margin": 230,
"n_drop": 5,
}
BACKTEST_CONFIG = {
"verbose": False,
Expand All @@ -105,7 +105,7 @@

# use default strategy
# custom Strategy, refer to: TODO: Strategy API url
strategy = TopkAmountStrategy(**STRATEGY_CONFIG)
strategy = TopkDropoutStrategy(**STRATEGY_CONFIG)
report_normal, positions_normal = normal_backtest(pred_score, strategy=strategy, **BACKTEST_CONFIG)

###################################
Expand Down
6 changes: 3 additions & 3 deletions examples/train_backtest_analyze.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"from qlib.config import REG_CN\n",
"from qlib.contrib.model.gbdt import LGBModel\n",
"from qlib.contrib.estimator.handler import QLibDataHandlerClose\n",
"from qlib.contrib.strategy.strategy import TopkAmountStrategy\n",
"from qlib.contrib.strategy.strategy import TopkDropoutStrategy\n",
"from qlib.contrib.evaluate import (\n",
" backtest as normal_backtest,\n",
" risk_analysis,\n",
Expand Down Expand Up @@ -135,7 +135,7 @@
"###################################\n",
"STRATEGY_CONFIG = {\n",
" \"topk\": 50,\n",
" \"buffer_margin\": 230,\n",
" \"n_drop\": 5",
"}\n",
"BACKTEST_CONFIG = {\n",
" \"verbose\": False,\n",
Expand All @@ -151,7 +151,7 @@
"\n",
"# use default strategy\n",
"# custom Strategy, refer to: TODO: Strategy api url\n",
"strategy = TopkAmountStrategy(**STRATEGY_CONFIG)\n",
"strategy = TopkDropoutStrategy(**STRATEGY_CONFIG)\n",
"report_normal, positions_normal = normal_backtest(pred_score, strategy=strategy, **BACKTEST_CONFIG)\n"
]
},
Expand Down
Loading

0 comments on commit d3e22a6

Please sign in to comment.