mirror of
https://github.com/microsoft/qlib.git
synced 2026-06-06 05:51:17 +08:00
* init commit * change the version number * rich the docs&fix cache docs * update index readme * Modify cache class name * Modify sharpe to information_ratio * Modify Group- to Group * add the description of graphical results & fix the backtest docs * fix docs in details * update docs * Update introduction.rst * Update README.md * Update introduction.rst * Update introduction.rst * Update introduction.rst * Update installation.rst * Update installation.rst * Update initialization.rst * Update getdata.rst * Update integration.rst * Update initialization.rst * Update getdata.rst * Update estimator.rst Modify some typos. * Update README.md Modify the typos. * Update initialization.rst * Update data.rst * Update report.rst * Update estimator.rst * Update cumulative_return.py * Update model.rst * Update rank_label.py * Update cumulative_return.py * Update strategy.rst * Update getdata.rst * Update backtest.rst * Update integration.rst * Update getdata.rst * Update introduction.rst * Update introduction.rst * Update README.md * Update report.rst * Update integration.rst Fix typos * Update installation.rst Fix typos * Update getdata.rst * Update initialization.rst Fix typos. * add quick start docs&fix detials * fix estimator docs & fix strategy docs * fix the cahce in data.rst * update documents * Fix Corr && Rsquare * fix data retrival example to csi300 & fix a data bug * fix filter bug * Fix data collector * Modift model args * add the log & fix README.md\quick.rst * add enviroment depend & add intoduction of qlib-server online mode * fix image center fomat & set log_only of docs is True * fix README.md format * update data preparation & readme logo image * get_data support version * Modify analysis names * Modify analysis graph * update report.rst & data.rst * commmit estimator for merge * minimal requirements * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update READEME.md * Update READEME.md * update estimator * Fix doc urls * fix get_data.py docstring * update test_get_data.py * Upate docs * Upate docs * Upate docs Co-authored-by: bxdd <bxddream@gmail.com> Co-authored-by: zhupr <zhu.pengrong@foxmail.com> Co-authored-by: Wendi Li <wendili.academic@qq.com> Co-authored-by: Dingsu Wang <dingsu.wang@gmail.com> Co-authored-by: bxdd <45119470+bxdd@users.noreply.github.com> Co-authored-by: cslwqxx <cslwqxx@users.noreply.github.com>
119 lines
4.7 KiB
ReStructuredText
119 lines
4.7 KiB
ReStructuredText
.. _strategy:
|
||
========================================
|
||
Interday Strategy: Portfolio Management
|
||
========================================
|
||
.. currentmodule:: qlib
|
||
|
||
Introduction
|
||
===================
|
||
|
||
``Interday Strategy`` is designed to adopt different trading strategies, which means that users can adopt different algorithms to generate investment portfolios based on the prediction scores of the ``Interday Model``. Users can use the ``Interday Strategy`` in an automatic workflow by ``Estimator``, please refer to `Estimator <estimator.html>`_.
|
||
|
||
Because the components in ``Qlib`` are designed in a loosely-coupled way, ``Interday Strategy`` can be used as an independent module also.
|
||
|
||
``Qlib`` provides several implemented trading strategies. Also, ``Qlib`` supports custom strategy, users can customize strategies according to their own needs.
|
||
|
||
Base Class & Interface
|
||
======================
|
||
|
||
BaseStrategy
|
||
------------------
|
||
|
||
Qlib provides a base class ``qlib.contrib.strategy.BaseStrategy``. All strategy classes need to inherit the base class and implement its interface.
|
||
|
||
- `get_risk_degree`
|
||
Return the proportion of your total value you will use in investment. Dynamically risk_degree will result in Market timing.
|
||
|
||
- `generate_order_list`
|
||
Rerturn the order list.
|
||
|
||
Users can inherit `BaseStrategy` to customize their strategy class.
|
||
|
||
WeightStrategyBase
|
||
--------------------
|
||
|
||
Qlib alse provides a class ``qlib.contrib.strategy.WeightStrategyBase`` that is a subclass of `BaseStrategy`.
|
||
|
||
`WeightStrategyBase` only focuses on the target positions, and automatically generates an order list based on positions. It provides the `generate_target_weight_position` interface.
|
||
|
||
- `generate_target_weight_position`
|
||
- According to the current position and trading date to generate the target position. The cash is not considered.
|
||
- Return the target position.
|
||
|
||
.. note::
|
||
Here the `target position` means the target percentage of total assets.
|
||
|
||
`WeightStrategyBase` implements the interface `generate_order_list`, whose processions is as follows.
|
||
|
||
- Call `generate_target_weight_position` method to generate the target position.
|
||
- Generate the target amount of stocks from the target position.
|
||
- Generate the order list from the target amount
|
||
|
||
Users can inherit `WeightStrategyBase` and implement the interface `generate_target_weight_position` to customize their strategy class, which only focuses on the target positions.
|
||
|
||
Implemented Strategy
|
||
====================
|
||
|
||
Qlib provides a implemented strategy classes named `TopkDropoutStrategy`.
|
||
|
||
TopkDropoutStrategy
|
||
------------------
|
||
`TopkDropoutStrategy` is a subclass of `BaseStrategy` and implement the interface `generate_order_list` whose process is as follows.
|
||
|
||
- Adopt the ``Topk-Drop`` algorithm to calculate the target amount of each stock
|
||
|
||
.. note::
|
||
``Topk-Drop`` algorithm:
|
||
|
||
- `Topk`: The number of stocks held
|
||
- `Drop`: The number of stocks sold on each trading day
|
||
|
||
Currently, the number of held stocks is `Topk`.
|
||
On each trading day, the `Drop` number of held stocks with the worst `prediction score` will be sold, and the same number of unheld stocks with the best `prediction score` will be bought.
|
||
|
||
.. image:: ../_static/img/topk_drop.png
|
||
:alt: Topk-Drop
|
||
|
||
``TopkDrop`` algorithm sells `Drop` stocks every trading day, which guarantees a fixed turnover rate.
|
||
|
||
- Generate the order list from the target amount
|
||
|
||
Usage & Example
|
||
====================
|
||
``Interday Strategy`` can be specified in the ``Intraday Trading(Backtest)``, the example is as follows.
|
||
|
||
.. code-block:: python
|
||
|
||
from qlib.contrib.strategy.strategy import TopkDropoutStrategy
|
||
from qlib.contrib.evaluate import backtest
|
||
STRATEGY_CONFIG = {
|
||
"topk": 50,
|
||
"n_drop": 5,
|
||
}
|
||
BACKTEST_CONFIG = {
|
||
"verbose": False,
|
||
"limit_threshold": 0.095,
|
||
"account": 100000000,
|
||
"benchmark": BENCHMARK,
|
||
"deal_price": "vwap",
|
||
}
|
||
|
||
# use default strategy
|
||
# custom Strategy, refer to: TODO: Strategy API url
|
||
strategy = TopkDropoutStrategy(**STRATEGY_CONFIG)
|
||
|
||
# pred_score is the `prediction score` output by Model
|
||
report_normal, positions_normal = backtest(
|
||
pred_score, strategy=strategy, **BACKTEST_CONFIG
|
||
)
|
||
|
||
Also, the above example has been given in ``examples\train_backtest_analyze.ipynb``.
|
||
|
||
To know more about the `prediction score` `pred_score` output by ``Interday Model``, please refer to `Interday Model: Model Training & Prediction <model.html>`_.
|
||
|
||
To know more about ``Intraday Trading``, please refer to `Intraday Trading: Model&Strategy Testing <backtest.html>`_.
|
||
|
||
Reference
|
||
===================
|
||
To know more about ``Interday Strategy``, please refer to `Strategy API <../reference/api.html>`_.
|