mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-01 18:11:18 +08:00
init commit
This commit is contained in:
106
docs/component/backtest.rst
Normal file
106
docs/component/backtest.rst
Normal file
@@ -0,0 +1,106 @@
|
||||
.. _backtest:
|
||||
============================================
|
||||
Intraday Trading: Model&Strategy Testing
|
||||
============================================
|
||||
.. currentmodule:: qlib
|
||||
|
||||
Introduction
|
||||
===================
|
||||
|
||||
``Intraday Trading`` is designed to test models and strategies, which help users to check the performance of custom model/strategy.
|
||||
|
||||
|
||||
.. note::
|
||||
|
||||
``Intraday Trading`` uses ``Order Executor`` to trade and execute orders output by ``Interday Strategy``. ``Order Executor`` is a component in `Qlib Framework <../introduction/introduction.html#framework>`_, which can execute orders. ``Vwap Executor`` and ``Close Executor`` is supported by ``Qlib`` now. In the future, ``Qlib`` will support ``HighFreq Executor`` also.
|
||||
|
||||
|
||||
|
||||
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 `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, n_drop=0.5, verbose=False, limit_threshold=0.0095)
|
||||
|
||||
To know more about backtesting with specific strategy, please refer to `Strategy <strategy.html>`_.
|
||||
|
||||
To know more about the prediction score `pred_score` output by ``Model``, please refer to `Interday Model: Model Training & Prediction <model.html>`_.
|
||||
|
||||
Prediction Score
|
||||
-----------------
|
||||
|
||||
The prediction score is a pandas DataFrame. Its index is <instrument(str), datetime(pd.Timestamp)> and it must
|
||||
contains a `score` column.
|
||||
|
||||
A prediction sample is shown as follows.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
instrument datetime score
|
||||
SH600000 2019-01-04 -0.505488
|
||||
SZ002531 2019-01-04 -0.320391
|
||||
SZ000999 2019-01-04 0.583808
|
||||
SZ300569 2019-01-04 0.819628
|
||||
SZ001696 2019-01-04 -0.137140
|
||||
... ...
|
||||
SZ000996 2019-04-30 -1.027618
|
||||
SH603127 2019-04-30 0.225677
|
||||
SH603126 2019-04-30 0.462443
|
||||
SH603133 2019-04-30 -0.302460
|
||||
SZ300760 2019-04-30 -0.126383
|
||||
|
||||
``Model`` module can make predictions, please refer to `Model <model.html>`_.
|
||||
|
||||
Backtest Result
|
||||
------------------
|
||||
|
||||
The backtest results are in the following form:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
sub_bench mean 0.000662
|
||||
std 0.004487
|
||||
annual 0.166720
|
||||
sharpe 2.340526
|
||||
mdd -0.080516
|
||||
sub_cost mean 0.000577
|
||||
std 0.004482
|
||||
annual 0.145392
|
||||
sharpe 2.043494
|
||||
mdd -0.083584
|
||||
|
||||
- `sub_bench`
|
||||
Returns of the portfolio without deduction of fees
|
||||
|
||||
- `sub_cost`
|
||||
Returns of the portfolio with deduction of fees
|
||||
|
||||
- `mean`
|
||||
Mean value of the returns sequence(difference sequence of assets).
|
||||
|
||||
- `std`
|
||||
Standard deviation of the returns sequence(difference sequence of assets).
|
||||
|
||||
- `annual`
|
||||
Average annualized returns of the portfolio.
|
||||
|
||||
- `ir`
|
||||
Information Ratio, please refer to `Information Ratio – IR <https://www.investopedia.com/terms/i/informationratio.asp>`_.
|
||||
|
||||
- `mdd`
|
||||
Maximum Drawdown, please refer to `Maximum Drawdown (MDD) <https://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp>`_.
|
||||
|
||||
|
||||
Reference
|
||||
==============
|
||||
|
||||
To know more about ``Intraday Trading``, please refer to `Backtest API <../reference/api.html>`_.
|
||||
333
docs/component/data.rst
Normal file
333
docs/component/data.rst
Normal file
@@ -0,0 +1,333 @@
|
||||
.. _data:
|
||||
================================
|
||||
Data Layer: Data Framework&Usage
|
||||
================================
|
||||
|
||||
Introduction
|
||||
============================
|
||||
|
||||
``Data Layer`` is designed to download raw data, retrieve data, construct datasets and get frequently-used data.
|
||||
|
||||
Also, users can building formulaic alphas with ``Data Layer`` easliy. If users are interesting formulaic alphas, please refer to `Building Formulaic Alphas <../advanced/alpha.html>`_.
|
||||
|
||||
The ``Data Layer`` framework includes four components as follows.
|
||||
|
||||
- Raw Data
|
||||
- Data API
|
||||
- Data Handler
|
||||
- Cache
|
||||
|
||||
|
||||
|
||||
Raw Data
|
||||
============================
|
||||
|
||||
``Qlib`` provides the script ``scripts/get_data.py`` to download the raw data that will be used to initialize the qlib package, please refer to `Initialization <../start/initialization.rst>`_.
|
||||
|
||||
When ``Qlib`` is initialized, users can choose china-stock mode or US-stock mode, please refer to `Initialization <../start/initialization.rst>`_.
|
||||
|
||||
China-Stock Market Mode
|
||||
--------------------------------
|
||||
|
||||
If users use ``Qlib`` in china-stock mode, china-stock data is required. The script ``scripts/get_data.py`` can be used to download china-stock data. If users want to use ``Qlib`` in china-stock mode, they need to do as follows.
|
||||
|
||||
- Download data in qlib format
|
||||
Run the following command to download china-stock data in csv format.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python scripts/get_data.py qlib_data_cn --target_dir ~/.qlib/qlib_data/cn_data
|
||||
|
||||
Users can find china-stock data in qlib format in the'~/.qlib/csv_data/cn_data' directory.
|
||||
|
||||
- Initialize ``Qlib`` in china-stock mode
|
||||
Users only need to initialize ``Qlib`` as follows.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from qlib.config import REG_CN
|
||||
qlib.init(provider_uri='~/.qlib/qlib_data/cn_data', region=REG_CN)
|
||||
|
||||
|
||||
US-Stock Market Mode
|
||||
-------------------------
|
||||
If users use ``Qlib`` in US-stock mode, US-stock data is required. ``Qlib`` does not provide script to download US-stock data. If users want to use ``Qlib`` in US-stock market mode, they need to do as follows.
|
||||
|
||||
- Prepare data in csv format
|
||||
Users need to prepare US-stock data in csv format by themselves, which is in the same format as the china-stock data in csv format. Please download the china-stock data in csv format as follows for reference of format.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python scripts/get_data.py csv_data_cn --target_dir ~/.qlib/csv_data/cn_data
|
||||
|
||||
|
||||
- Convert data from csv format to ``Qlib`` format
|
||||
``Qlib`` provides the script ``scripts/dump_bin.py`` to convert data from csv format to qlib format.
|
||||
Assuming that the users store the US-stock data in csv format in path '~/.qlib/csv_data/us_data', they need to execute the following command to convert the data from csv format to ``Qlib`` format:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
python scripts/dump_bin.py dump --csv_path ~/.qlib/csv_data/us_data --qlib_dir ~/.qlib/qlib_data/us_data --include_fields open,close,high,low,volume,factor
|
||||
|
||||
- Initialize ``Qlib`` in US-stock mode
|
||||
Users only need to initialize ``Qlib`` as follows.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from qlib.config import REG_US
|
||||
qlib.init(provider_uri='~/.qlib/qlib_data/us_data', region=REG_US)
|
||||
|
||||
|
||||
Please refer to `Script API <../reference/api.html>`_ for more details.
|
||||
|
||||
Data API
|
||||
========================
|
||||
|
||||
Data Retrieval
|
||||
---------------
|
||||
Users can use APIs in ``qlib.data`` to retrieve data, please refer to `Data Retrieval <../start/getdata.html>`_.
|
||||
|
||||
Feature
|
||||
------------------
|
||||
|
||||
``Qlib`` provides `Feature` and `ExpressionOps` to fetch the features according to users' need.
|
||||
|
||||
- `Feature`
|
||||
Load data from data provider.
|
||||
|
||||
- `ExpressionOps`
|
||||
`ExpressionOps` will use operator for feature construction.
|
||||
To know more about ``Operator``, please refer to `Operator API <../reference/api.html>`_.
|
||||
|
||||
To know more about ``Feature``, please refer to `Feature API <../reference/api.html>`_.
|
||||
|
||||
Filter
|
||||
-------------------
|
||||
``Qlib`` provides `NameDFilter` and `ExpressionDFilter` to filter the instruments according to users' need.
|
||||
|
||||
- `NameDFilter`
|
||||
Name dynamic instrument filter. Filter the instruments based on a regulated name format. A name rule regular expression is required.
|
||||
|
||||
- `ExpressionDFilter`
|
||||
Expression dynamic instrument filter. Filter the instruments based on a certain expression. An expression rule indicating a certain feature field is required.
|
||||
|
||||
- `basic features filter`: rule_expression = '$close/$open>5'
|
||||
- `cross-sectional features filter` : rule_expression = '$rank($close)<10'
|
||||
- `time-sequence features filter`: rule_expression = '$Ref($close, 3)>100'
|
||||
|
||||
To know more about ``Filter``, please refer to `Filter API <../reference/api.html>`_.
|
||||
|
||||
|
||||
API
|
||||
-------------
|
||||
|
||||
To know more about ``Data Api``, please refer to `Data Api <../reference/api.html>`_.
|
||||
|
||||
Data Handler
|
||||
=================
|
||||
|
||||
``Data Handler`` is a part of ``estimator`` and can also be used as a single module.
|
||||
|
||||
``Data Handler`` can be used to load raw data, prepare features and label columns, preprocess data(standardization, remove NaN, etc.), split training, validation, and test sets. It is a subclass of ``qlib.contrib.estimator.handler.BaseDataHandler``, which provides some interfaces, for example:
|
||||
|
||||
Base Class & Interface
|
||||
----------------------
|
||||
|
||||
Qlib provides a base class `qlib.contrib.estimator.BaseDataHandler <../reference/api.html#class-qlib.contrib.estimator.BaseDataHandler>`_, which provides the following interfaces:
|
||||
|
||||
- `setup_feature`
|
||||
Implement the interface to load the data features.
|
||||
|
||||
- `setup_label`
|
||||
Implement the interface to load the data labels and calculate user's labels.
|
||||
|
||||
- `setup_processed_data`
|
||||
Implement the interface for data preprocessing, such as preparing feature columns, discarding blank lines, and so on.
|
||||
|
||||
Qlib also provides two functions to help user init the data handler, user can override them for user's need.
|
||||
|
||||
- `_init_kwargs`
|
||||
User can init the kwargs of the data handler in this function, some kwargs may be used when init the raw df.
|
||||
Kwargs are the other attributes in data.args, like dropna_label, dropna_feature
|
||||
|
||||
- `_init_raw_df`
|
||||
User can init the raw df, feature names and label names of data handler in this function.
|
||||
If the index of feature df and label df are not same, user need to override this method to merge them (e.g. inner, left, right merge).
|
||||
|
||||
If users want to load features and labels by config, users can inherit ``qlib.contrib.estimator.handler.ConfigDataHandler``, ``Qlib`` also have provided some preprocess method in this subclass.
|
||||
If users want to use qlib data, `QLibDataHandler` is recommended. Users can inherit their custom class from `QLibDataHandler`, which is also a subclass of `ConfigDataHandler`.
|
||||
|
||||
|
||||
Usage
|
||||
--------------
|
||||
'Data Handler' can be used as a single module, which provides the following mehtod:
|
||||
|
||||
- `get_split_data`
|
||||
- According to the start and end dates, return features and labels of the pandas DataFrame type used for the 'Model'
|
||||
|
||||
- `get_rolling_data`
|
||||
- According to the start and end dates, and `rolling_period`, an iterator is returned, which can be used to traverse the features and labels used for rolling.
|
||||
|
||||
|
||||
|
||||
|
||||
Example
|
||||
--------------
|
||||
|
||||
``Data Handler`` can be run with ``estimator`` by modifying the configuration file, and can also be used as a single module.
|
||||
|
||||
Know more about how to run ``Data Handler`` with ``estimator``, please refer to `Estimator <estimator.html#about-data>`_.
|
||||
|
||||
Qlib provides implemented data handler `QLibDataHandlerV1`. The following example shows how to run 'QLibDataHandlerV1' as a single module.
|
||||
|
||||
.. note:: User needs to initialize ``Qlib`` with `qlib.init` first, please refer to `initialization <initialization.rst>`_.
|
||||
|
||||
|
||||
.. code-block:: Python
|
||||
|
||||
from qlib.contrib.estimator.handler import QLibDataHandlerV1
|
||||
from qlib.contrib.model.gbdt import LGBModel
|
||||
|
||||
DATA_HANDLER_CONFIG = {
|
||||
"dropna_label": True,
|
||||
"start_date": "2007-01-01",
|
||||
"end_date": "2020-08-01",
|
||||
"market": "csi500",
|
||||
}
|
||||
|
||||
TRAINER_CONFIG = {
|
||||
"train_start_date": "2007-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",
|
||||
}
|
||||
|
||||
exampleDataHandler = QLibDataHandlerV1(**DATA_HANDLER_CONFIG)
|
||||
|
||||
# example of 'get_split_data'
|
||||
x_train, y_train, x_validate, y_validate, x_test, y_test = exampleDataHandler.get_split_data(**TRAINER_CONFIG)
|
||||
|
||||
# example of 'get_rolling_data'
|
||||
|
||||
for (x_train, y_train, x_validate, y_validate, x_test, y_test) in exampleDataHandler.get_rolling_data(**TRAINER_CONFIG):
|
||||
print(x_train, y_train, x_validate, y_validate, x_test, y_test)
|
||||
|
||||
|
||||
.. note:: (x_train, y_train, x_validate, y_validate, x_test, y_test) can be used as arguments for the ``fit``, ``predict``, and ``score`` methods of the 'Model' , please refer to `Model <model.html#Interface>`_.
|
||||
|
||||
Also, the above example has been given in ``examples.estimator.train_backtest_analyze.ipynb``.
|
||||
|
||||
API
|
||||
---------
|
||||
|
||||
To know more abot ``Data Handler``, please refer to `Data Handler API <../reference/api.html#handler>`_.
|
||||
|
||||
Cache
|
||||
==========
|
||||
|
||||
``Cache`` is an optional module that helps accelerate providing data by saving some frequently-used data as cache file.
|
||||
|
||||
Memory Cache
|
||||
--------------
|
||||
|
||||
Base Class & Interface
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
``Qlib`` provides a `Memcache` class to cache the most-frequently-used data in memory, an inheritable `ExpressionCache` class, and an inheritable `DatasetCache` class.
|
||||
|
||||
`Memcache` is a memory cache mechanism that composes of three `MemCacheUnit` instances to cache **Calendar**, **Instruments**, and **Features**. The MemCache is defined globally in `cache.py` as `H`. User can use `H['c'], H['i'], H['f']` to get/set memcache.
|
||||
|
||||
.. autoclass:: qlib.data.cache.MemCacheUnit
|
||||
:members:
|
||||
|
||||
.. autoclass:: qlib.data.cache.MemCache
|
||||
:members:
|
||||
|
||||
|
||||
Disk Cache
|
||||
--------------
|
||||
|
||||
Base Class & Interface
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
`ExpressionCache` is a disk cache mechanism that saves expressions such as **Mean($close, 5)**. Users can inherit this base class to define their own cache mechanism. Users need to override `self._uri` method to define how their cache file path is generated, `self._expression` method to define what data they want to cache and how to cache it.
|
||||
|
||||
`DatasetCache` is a disk cache mechanism that saves datasets. A certain dataset is regulated by a stockpool configuration (or a series of instruments, though not recommended), a list of expressions or static feature fields, the start time and end time for the collected features and the frequency. Users need to override `self._uri` method to define how their cache file path is generated, `self._expression` method to define what data they want to cache and how to cache it.
|
||||
|
||||
`ExpressionCache` and `DatasetCache` actually provides the same interfaces with `ExpressionProvider` and `DatasetProvider` so that the disk cache layer is transparent to users and will only be used if they want to define their own cache mechanism. The users can plug the cache mechanism into the server system by assigning the cache class they want to use in `config.py`:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
'ExpressionCache': 'ServerExpressionCache',
|
||||
'DatasetCache': 'ServerDatasetCache',
|
||||
|
||||
Users can find the cache interface here.
|
||||
|
||||
ExpressionCache
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
.. autoclass:: qlib.data.cache.ExpressionCache
|
||||
:members:
|
||||
|
||||
DatasetCache
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
.. autoclass:: qlib.data.cache.DatasetCache
|
||||
:members:
|
||||
|
||||
|
||||
Implemented Disk Cache
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note::
|
||||
|
||||
If the user does not use QlibServer, please ignore the content of this section
|
||||
|
||||
Qlib has currently provided `ServerExpressionCache` class and `ServerDatasetCache` class as the cache mechanisms used for QlibServer. The class interface and file structure designed for server cache mechanism is listed below.
|
||||
|
||||
DiskExpressionCache
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
.. autoclass:: qlib.data.cache.ServerExpressionCache
|
||||
|
||||
DiskDatasetCache
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
.. autoclass:: qlib.data.cache.ServerDatasetCache
|
||||
|
||||
|
||||
Data and Cache File Structure
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. code-block:: json
|
||||
|
||||
- data/
|
||||
[raw data] updated by data providers
|
||||
- calendars/
|
||||
- day.txt
|
||||
- instruments/
|
||||
- all.txt
|
||||
- csi500.txt
|
||||
- ...
|
||||
- features/
|
||||
- sh600000/
|
||||
- open.day.bin
|
||||
- close.day.bin
|
||||
- ...
|
||||
- ...
|
||||
[cached data] updated by server when raw data is updated
|
||||
- calculated features/
|
||||
- sh600000/
|
||||
- [hash(instrtument, field_expression, freq)]
|
||||
- all-time expression -cache data file
|
||||
- .meta : an assorted meta file recording the instrument name, field name, freq, and visit times
|
||||
- ...
|
||||
- cache/
|
||||
- [hash(stockpool_config, field_expression_list, freq)]
|
||||
- all-time Dataset-cache data file
|
||||
- .meta : an assorted meta file recording the stockpool config, field names and visit times
|
||||
- .index : an assorted index file recording the line index of all calendars
|
||||
- ...
|
||||
|
||||
674
docs/component/estimator.rst
Normal file
674
docs/component/estimator.rst
Normal file
@@ -0,0 +1,674 @@
|
||||
.. _estimator:
|
||||
=================================
|
||||
Estimator: Workflow Management
|
||||
=================================
|
||||
.. currentmodule:: qlib
|
||||
|
||||
Introduction
|
||||
===================
|
||||
|
||||
The components in `Qlib Framework <../introduction/introduction.html#framework>`_ is designed in a loosely-coupled way. Users could build their own quant research workflow with these components like `Example <http://TODO_URL>`_
|
||||
|
||||
|
||||
Besides, ``Qlib`` provides more user-friendly interfaces named ``Estimator`` to automatically run the whole workflow defined by a config. A concrete execution of the whole workflow is called an `experiment`.
|
||||
With ``Estimator``, user can easily run an `experiment`, which includes the following steps:
|
||||
|
||||
- Data
|
||||
- Loading
|
||||
- Processing
|
||||
- Slicing
|
||||
- Model
|
||||
- Training and inference(static or rolling)
|
||||
- Saving & loading
|
||||
- Evaluation(Back-testing)
|
||||
|
||||
For each `experiment`, ``Qlib`` will capture the details of model training, performance evalution results and basic infomation(e.g. names, ids). The captured data will be stored in backend-storge(disk or database).
|
||||
|
||||
Example
|
||||
===================
|
||||
|
||||
The following is an example:
|
||||
|
||||
.. note:: Make sure install the latest version of `qlib`, please refer to `Qlib installation <../start/installation.html>`_.
|
||||
|
||||
If users want to use the models and data provided by `Qlib`, they only need to do as follows.
|
||||
|
||||
First, Write a simple configuration file as following,
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
experiment:
|
||||
name: estimator_example
|
||||
observer_type: file_storage
|
||||
mode: train
|
||||
model:
|
||||
class: LGBModel
|
||||
module_path: qlib.contrib.model.gbdt
|
||||
args:
|
||||
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
|
||||
data:
|
||||
class: QLibDataHandlerClose
|
||||
args:
|
||||
dropna_label: True
|
||||
filter:
|
||||
market: csi500
|
||||
trainer:
|
||||
class: StaticTrainer
|
||||
args:
|
||||
rolling_period: 360
|
||||
train_start_date: 2007-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
|
||||
strategy:
|
||||
class: TopkDropoutStrategy
|
||||
args:
|
||||
topk: 50
|
||||
n_drop: 5
|
||||
backtest:
|
||||
normal_backtest_args:
|
||||
verbose: False
|
||||
limit_threshold: 0.095
|
||||
account: 100000000
|
||||
benchmark: SH000905
|
||||
deal_price: close
|
||||
open_cost: 0.0005
|
||||
close_cost: 0.0015
|
||||
min_cost: 5
|
||||
qlib_data:
|
||||
# when testing, please modify the following parameters according to the specific environment
|
||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||
region: "cn"
|
||||
|
||||
|
||||
Then run the following command:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
estimator -c configuration.yaml
|
||||
|
||||
.. note:: 'estimator' is a built-in command of our program.
|
||||
|
||||
|
||||
|
||||
Configuration File
|
||||
===================
|
||||
|
||||
Before using ``estimator``, users need to prepare a configuration file. The following shows how to prepare each part of the configuration file.
|
||||
|
||||
Experiment Field
|
||||
--------------------
|
||||
|
||||
First, the configuration file needs to have a field about the experiment, whose key is `experiment`. This field and its contents determine how `estimator` tracks and persists this `experiment`. ``Qlib`` used `sacred`, a lightweight open-source tool designed to configure, organize, generate logs, and manage experiment results. The field `experiment` will determine the partial behavior of `sacred`.
|
||||
|
||||
Usually, in the running process of `estimator`, those following will be managed by `sacred`:
|
||||
|
||||
- `model.bin`, model binary file
|
||||
- `pred.pkl`, model prediction result file
|
||||
- `analysis.pkl`, backtest performance analysis file
|
||||
- `positions.pkl`, backtest position record file
|
||||
- `run`, the experiment information object, usually contains some meta information such as the experiment name, experiment date, etc.
|
||||
|
||||
Usually, it should contain the following:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
experiment:
|
||||
name: test_experiment
|
||||
observer_type: mongo
|
||||
mongo_url: mongodb://MONGO_URL
|
||||
db_name: public
|
||||
finetune: false
|
||||
exp_info_path: /home/test_user/exp_info.json
|
||||
mode: test
|
||||
loader:
|
||||
id: 677
|
||||
|
||||
|
||||
The meaning of each field is as follows:
|
||||
|
||||
- `name`
|
||||
The experiment name, str type, `sacred` will use this experiment name as an identifier for some important internal processes. Usually, users can see this field in `sacred` by `run` object. The default value is `test_experiment`.
|
||||
|
||||
- `observer_type`
|
||||
Observer type, str type, there are two values which are `file_storage` and `mongo` respectively. If it is `file_storage`, all the above-mentioned managed contents will be stored in the `dir` directory, separated by the number of times of experiments as a subfolder. If it is `mongo`, the content will be stored in the database. The default is `file_storage`.
|
||||
|
||||
- For `file_storage` observer.
|
||||
- `dir`
|
||||
Directory url, str type, directory for `file_storage` observer type, files captures and managed by sacred with observer type of `file_storage` will be save to this directory, default is the directory of `config.json`.
|
||||
|
||||
- For `mongo` observer.
|
||||
- `mongo_url`
|
||||
Database URL, str type, required if the observer type is `mongo`.
|
||||
|
||||
- `db_name`
|
||||
Database name, str type, required if the observer type is `mongo`.
|
||||
|
||||
- `finetune`
|
||||
Estimator will produce a model based on this flag
|
||||
|
||||
The following table is the processing logic for different situations.
|
||||
|
||||
========== =========================================== ==================================== =========================================== ==========================================
|
||||
. Static Rolling
|
||||
. Finetune=True Finetune=False Finetune=True Finetune=False
|
||||
========== =========================================== ==================================== =========================================== ==========================================
|
||||
Train - Need to provide model(Static or Rolling) - No need to provide model - Need to provide model(Static or Rolling) - Need to provide model(Static or Rolling)
|
||||
- The args in model section will be - The args in model section will be - The args in model section will be - The args in model section will be
|
||||
used for finetuning used for training used for finetuning used for finetuning
|
||||
- Update based on the provided model - Train model from scratch - Update based on the provided model - Based on the provided model update
|
||||
and parameters and parameters - Train model from scratch
|
||||
- **Each rolling time slice is based on** - **Train each rolling time slice**
|
||||
**a model updated from the previous** **separately**
|
||||
**time**
|
||||
Test - Model must exist, otherwise an exception will be raised.
|
||||
- For `StaticTrainer`, users need to train a model and record 'exp_info' for 'Test'.
|
||||
- For `RollingTrainer`, users need to train a set of models until the latest time, and record 'exp_info' for 'Test'.
|
||||
========== =============================================================================================================================================================================
|
||||
|
||||
.. note::
|
||||
|
||||
1. finetune parameters: share model.args parameters.
|
||||
|
||||
2. provide model: from `loader.model_index`, load the index of the model(starting from 0).
|
||||
|
||||
3. If `loader.model_index` is None:
|
||||
- In 'Static Finetune=True', if provide 'Rolling', use the last model to update.
|
||||
|
||||
- For RollingTrainer with Finetune=Ture.
|
||||
|
||||
- If StaticTrainer is used in loader, the model will be used for initialization for finetuning.
|
||||
|
||||
- If RollingTrainer is used in loader, the existing models will be used without any modification and the new models will be initialized with the model in the last period and finetune one by one.
|
||||
|
||||
|
||||
- `exp_info_path`
|
||||
experiment info save path, str type, save the experiment info and model prediction score after the experiment is finished. Optional parameter, the default value is `config_file_dir/ex_name/exp_info.json`
|
||||
|
||||
- `mode`
|
||||
`train` or `test`, str type, if `mode` is test, it will load the model according to the parameters of `loader`. The default value is `train`.
|
||||
Also note that when the load model failed, it will `fit` model.
|
||||
.. note::
|
||||
|
||||
if users choose `mode` test, they need to make sure:
|
||||
- The loader of `test_start_date` must be less than or equal to the current `test_start_date`.
|
||||
- If other parameters of the `loader` model args are different, a warning will appear.
|
||||
|
||||
|
||||
- `loader`
|
||||
If the `mode` is `test` or `finetune` is `true`, it will be used.
|
||||
|
||||
- `model_index`
|
||||
Model index, int type. The index of the loaded model in loader_models (starting at 0) for the first `finetune`. The default value is None.
|
||||
|
||||
- `exp_info_path`
|
||||
Loader model experiment info path, str type. If the field exists, the following parameters will be parsed from `exp_info_path`, and the following parameters will not work. This field and `id` must exist one.
|
||||
|
||||
- `id`
|
||||
The experiment id of the model that needs to be loaded, int type. If the `mode` is `test`, this value is required. This field and `exp_info_path` must exist one.
|
||||
|
||||
- `name`
|
||||
The experiment name of the model that needs to be loaded, str type. The default value is the current experiment `name`.
|
||||
|
||||
- `observer_type`
|
||||
The experiment observer type of the model that needs to be loaded, str type. The default value is the current experiment `observer_type`.
|
||||
.. note:: The observer type is a concept of the `sacred` module, which determines how files, standard input and output which are managed by sacred are stored.
|
||||
|
||||
|
||||
- `file_storage`
|
||||
If `observer_type` is `file_storage`, the config may be as follows.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
experiment:
|
||||
name: test_experiment
|
||||
dir: <path to a directory> # default is dir of `config.yml`
|
||||
observer_type: file_storage
|
||||
- `mongo`
|
||||
If `observer_type` is `mongo`, the config may be as follows.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
experiment:
|
||||
name: test_experiment
|
||||
observer_type: mongo
|
||||
mongo_url: mongodb://MONGO_URL
|
||||
db_name: public
|
||||
|
||||
Users need to indicate `mongo_url` and `db_name` for a mongo observer.
|
||||
|
||||
.. note::
|
||||
|
||||
If users choose mongo observer, they need to make sure:
|
||||
- have an environment with the mongodb installed and a mongo database dedicated for storing the experiments results.
|
||||
- The python environment(the version of python and package) to run the experiments and the one to fetch the results are consistent.
|
||||
|
||||
Model Field
|
||||
-----------------
|
||||
|
||||
Users can use a specified model by configuration with hyper-parameters.
|
||||
|
||||
Custom Models
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
Qlib support custom models, but it must be a subclass of the `qlib.contrib.model.Model`, the config for custom model may be as following.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
model:
|
||||
class: SomeModel
|
||||
module_path: /tmp/my_experment/custom_model.py
|
||||
args:
|
||||
loss: binary
|
||||
|
||||
|
||||
The class `SomeModel` should be in the module `custom_model`, and ``Qlib`` could parse the `module_path` to load the class.
|
||||
|
||||
To Know more about ``Model``, please refer to `Model <model.html>`_.
|
||||
|
||||
Data Field
|
||||
-----------------
|
||||
|
||||
``Data Handler`` can be used to load raw data, prepare features and label columns, preprocess data(standardization, remove NaN, etc.), split training, validation, and test sets. It is a subclass of `qlib.contrib.estimator.handler.BaseDataHandler`.
|
||||
|
||||
Users can use the specified data handler by config as follows.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
data:
|
||||
class: QLibDataHandlerClose
|
||||
args:
|
||||
start_date: 2005-01-01
|
||||
end_date: 2018-04-30
|
||||
dropna_label: True
|
||||
filter:
|
||||
market: csi500
|
||||
filter_pipeline:
|
||||
-
|
||||
class: NameDFilter
|
||||
module_path: qlib.filter
|
||||
args:
|
||||
name_rule_re: S(?!Z3)
|
||||
fstart_time: 2018-01-01
|
||||
fend_time: 2018-12-11
|
||||
-
|
||||
class: ExpressionDFilter
|
||||
module_path: qlib.filter
|
||||
args:
|
||||
rule_expression: $open/$factor<=45
|
||||
fstart_time: 2018-01-01
|
||||
fend_time: 2018-12-11
|
||||
|
||||
- `class`
|
||||
Data handler class, str type, which should be a subclass of `qlib.contrib.estimator.handler.BaseDataHandler`, and implements 5 important interfaces for loading features, loading raw data, preprocessing raw data, slicing train, validation, and test data. The default value is `ALPHA360`. If users want to write a data handler to retrieve the data in qlib, `QlibDataHandler` is suggested.
|
||||
|
||||
- `module_path`
|
||||
The module path, str type, absolute url is also supported, indicates the path of the `class` implementation of data processor class. The default value is `qlib.contrib.estimator.handler`.
|
||||
|
||||
- `args`
|
||||
Parameters used for ``Data Handler`` initialization.
|
||||
|
||||
- `train_start_date`
|
||||
Training start time, str type, default value is `2005-01-01`.
|
||||
|
||||
- `start_date`
|
||||
Data start date, str type.
|
||||
|
||||
- `end_date`
|
||||
Data end date, str type. the data from start_date to end_date decides which part of data will be loaded in datahandler, users can only use these data in the following parts.
|
||||
|
||||
- `dropna_feature` (Optional in args)
|
||||
Drop Nan feature, bool type, default value is False.
|
||||
|
||||
- `dropna_label` (Optional in args)
|
||||
Drop Nan label, bool type, default value is True. Some multi-label tasks will use this.
|
||||
|
||||
- `normalize_method` (Optional in args)
|
||||
Normalzie data by given method. str type. ``Qlib`` give two normalize method, `MinMax` and `Std`.
|
||||
If users wants to build their own method, please override `_process_normalize_feature`.
|
||||
|
||||
- `filter`
|
||||
Dynamically filtering the stocks based on the filter pipeline.
|
||||
|
||||
- `market`
|
||||
index name, str type, the default value is `csi500`.
|
||||
|
||||
- `filter_pipeline`
|
||||
Filter rule list, list type, the default value is []. Can be customized according to users' needs.
|
||||
|
||||
- `class`
|
||||
Filter class name, str type.
|
||||
|
||||
- `module_path`
|
||||
The module path, str type.
|
||||
|
||||
- `args`
|
||||
The filter class parameters, this parameters are set according to the `class`, and all the parameters as kwargs to `class`.
|
||||
|
||||
Custom Data Handler
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Qlib support custom data handler, but it must be a subclass of the ``qlib.contrib.estimator.handler.BaseDataHandler``, the config for custom data handler may be as follows.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
data:
|
||||
class: SomeDataHandler
|
||||
module_path: /tmp/my_experment/custom_data_handler.py
|
||||
args:
|
||||
start_date: 2005-01-01
|
||||
end_date: 2018-04-30
|
||||
|
||||
The class `SomeDataHandler` should be in the module `custom_data_handler`, and ``Qlib`` could parse the `module_path` to load the class.
|
||||
|
||||
If users want to load features and labels by config, they can inherit ``qlib.contrib.estimator.handler.ConfigDataHandler``, ``Qlib`` also has provided some preprocess method in this subclass.
|
||||
If users want to use qlib data, `QLibDataHandler` is recommended, from which users can inherit custom class. `QLibDataHandler` is also a subclass of `ConfigDataHandler`.
|
||||
|
||||
To Know more about ``Data Handler``, please refer to `Data Framework&Usage <data.html>`_.
|
||||
|
||||
Trainer Field
|
||||
-----------------
|
||||
|
||||
Users can specify the trainer ``Trainer`` by the config file, which is subclass of ``qlib.contrib.estimator.trainer.BaseTrainer`` and implement three important interfaces for training the model, restoring the model, and getting model predictions as follows.
|
||||
|
||||
- `train`
|
||||
Implement this interface to train the model.
|
||||
|
||||
- `load`
|
||||
Implement this interface to recover the model from disk.
|
||||
|
||||
- `get_pred`
|
||||
Implement this interface to get model prediction results.
|
||||
|
||||
Qlib have provided two implemented trainer,
|
||||
|
||||
- `StaticTrainer`
|
||||
The static trainer will be trained using the training, validation, and test data of the data processor static slicing.
|
||||
|
||||
- `RollingTrainer`
|
||||
The rolling trainer will use the rolling iterator of the data processor to split data for rolling training.
|
||||
|
||||
|
||||
Users can specify `trainer` with the configuration file:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
trainer:
|
||||
class: StaticTrainer # or RollingTrainer
|
||||
args:
|
||||
rolling_period: 360
|
||||
train_start_date: 2005-01-01
|
||||
train_end_date: 2014-12-31
|
||||
validate_start_date: 2015-01-01
|
||||
validate_end_date: 2016-06-30
|
||||
test_start_date: 2016-07-01
|
||||
test_end_date: 2017-07-31
|
||||
|
||||
- `class`
|
||||
Trainer class, which should be a subclass of `qlib.contrib.estimator.trainer.BaseTrainer`, and needs to implement three important interfaces, the default value is `StaticTrainer`.
|
||||
|
||||
- `module_path`
|
||||
The module path, str type, absolute url is also supported, indicates the path of the trainer class implementation.
|
||||
|
||||
- `args`
|
||||
Parameters used for ``Trainer`` initialization.
|
||||
|
||||
- `rolling_period`
|
||||
The rolling period, integer type, indicates how many time steps need rolling when rolling the data. The default value is `60`. Only used in `RollingTrainer`.
|
||||
|
||||
- `train_start_date`
|
||||
Training start time, str type.
|
||||
|
||||
- `train_end_date`
|
||||
Training end time, str type.
|
||||
|
||||
- `validate_start_date`
|
||||
Validation start time, str type.
|
||||
|
||||
- `validate_end_date`
|
||||
Validation end time, str type.
|
||||
|
||||
- `test_start_date`
|
||||
Test start time, str type.
|
||||
|
||||
- `test_end_date`
|
||||
Test end time, str type. If `test_end_date` is `-1` or greater than the last date of the data, the last date of the data will be used as `test_end_date`.
|
||||
|
||||
Custom Trainer
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Qlib support custom trainer, but it must be a subclass of the `qlib.contrib.estimator.trainer.BaseTrainer`, the config for custom trainer may be as following,
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
trainer:
|
||||
class: SomeTrainer
|
||||
module_path: /tmp/my_experment/custom_trainer.py
|
||||
args:
|
||||
train_start_date: 2005-01-01
|
||||
train_end_date: 2014-12-31
|
||||
validate_start_date: 2015-01-01
|
||||
validate_end_date: 2016-06-30
|
||||
test_start_date: 2016-07-01
|
||||
test_end_date: 2017-07-31
|
||||
|
||||
|
||||
The class `SomeTrainer` should be in the module `custom_trainer`, and ``Qlib`` could parse the `module_path` to load the class.
|
||||
|
||||
Strategy Field
|
||||
-----------------
|
||||
|
||||
Users can specify strategy through a config file, for example:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
strategy :
|
||||
class: TopkDropoutStrategy
|
||||
args:
|
||||
topk: 50
|
||||
n_drop: 5
|
||||
|
||||
- `class`
|
||||
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.
|
||||
|
||||
- `args`
|
||||
Parameters used for ``Trainer`` initialization.
|
||||
|
||||
- `topk`
|
||||
The number of stocks in the portfolio
|
||||
|
||||
- `n_drop`
|
||||
Number of stocks to be replaced in each trading date
|
||||
|
||||
Custom Strategy
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Qlib support custom strategy, but it must be a subclass of the ``qlib.contrib.strategy.strategy.BaseStrategy``, the config for custom strategy may be as following,
|
||||
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
strategy :
|
||||
class: SomeStrategy
|
||||
module_path: /tmp/my_experment/custom_strategy.py
|
||||
|
||||
The class `SomeStrategy` should be in the module `custom_strategy`, and ``Qlib`` could parse the `module_path` to load the class.
|
||||
|
||||
To Know more about ``Strategy``, please refer to `Strategy <strategy.html>`_.
|
||||
|
||||
Backtest Field
|
||||
-----------------
|
||||
|
||||
Users can specify `backtest` through a config file, for example:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
backtest :
|
||||
normal_backtest_args:
|
||||
topk: 50
|
||||
benchmark: SH000905
|
||||
account: 500000
|
||||
deal_price: close
|
||||
min_cost: 5
|
||||
subscribe_fields:
|
||||
- $close
|
||||
- $change
|
||||
- $factor
|
||||
|
||||
- `normal_backtest_args`
|
||||
Normal backtest parameters. All the parameters in this section will be passed to the ``qlib.contrib.evaluate.backtest`` function in the form of `**kwargs`.
|
||||
|
||||
- `benchmark`
|
||||
Stock index symbol, str or list type, the default value is `None`.
|
||||
|
||||
.. note::
|
||||
|
||||
* If `benchmark` is None, it will use the average change of the day of all stocks in 'pred' as the 'bench'.
|
||||
|
||||
* If `benchmark` is list, it will use the daily average change of the stock pool in the list as the 'bench'.
|
||||
|
||||
* If `benchmark` is str, it will use the daily change as the 'bench'.
|
||||
|
||||
|
||||
- `account`
|
||||
Backtest initial cash, integer type. The `account` in `strategy` section is deprecated. It only works when `account` is not set in `backtest` section. It will be overridden by `account` in the `backtest` section. The default value is 1e9.
|
||||
|
||||
- `deal_price`
|
||||
Order transaction price field, str type, the default value is vwap.
|
||||
|
||||
- `min_cost`
|
||||
Min transaction cost, float type, the default value is 5.
|
||||
|
||||
- `subscribe_fields`
|
||||
Subscribe quote fields, array type, the default value is [`deal_price`, $close, $change, $factor].
|
||||
|
||||
|
||||
Qlib Data Field
|
||||
--------------------
|
||||
|
||||
The `qlib_data` field describes the parameters of qlib initialization.
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
qlib_data:
|
||||
# when testing, please modify the following parameters according to the specific environment
|
||||
provider_uri: "~/.qlib/qlib_data/cn_data"
|
||||
region: "cn"
|
||||
|
||||
- `provider_uri`
|
||||
The local directory where the data loaded by 'get_data.py' is stored.
|
||||
- `region`
|
||||
- If region == ``qlib.config.REG_CN``, 'qlib' will be initialized in US-stock mode.
|
||||
- If region == ``qlib.config.REG_US``, 'qlib' will be initialized in china-stock mode.
|
||||
|
||||
Please refer to `Initialization <../start/initialization.rst>`_.
|
||||
|
||||
Experiment Result
|
||||
===================
|
||||
|
||||
Form of Experimental Result
|
||||
----------------------------
|
||||
The result of the experiment is the result of the backtest, please refer to `Backtest <backtest.html>`_.
|
||||
|
||||
|
||||
Get Experiment Result
|
||||
----------------------------
|
||||
|
||||
Users can check the experiment results from file storage directly, or check the experiment results from database, or get the experiment results through two API of a module `fetcher` provided by ``Qlib``.
|
||||
|
||||
- `get_experiments()`
|
||||
The API takes two parameters. The first parameter is the experiment name. The default is all experiments. The second parameter is the observer type. Users can get the experiment name dictionary with a list of ids and test end date by the API as follows.
|
||||
|
||||
.. code-block:: JSON
|
||||
|
||||
{
|
||||
"ex_a": [
|
||||
{
|
||||
"id": 1,
|
||||
"test_end_date": "2017-01-01"
|
||||
}
|
||||
],
|
||||
"ex_b": [
|
||||
...
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
- `get_experiment(exp_name, exp_id, fields=None)`
|
||||
The API takes three parameters, the first parameter is the experiment name, the second parameter is the experiment id, and the third parameter is field list.
|
||||
If fields is None, will get all fields.
|
||||
|
||||
.. note::
|
||||
Currently supported fields:
|
||||
['model', 'analysis', 'positions', 'report_normal', 'pred', 'task_config', 'label']
|
||||
|
||||
.. code-block:: JSON
|
||||
|
||||
{
|
||||
'analysis': analysis_df,
|
||||
'pred': pred_df,
|
||||
'positions': positions_dic,
|
||||
'report_normal': report_normal_df,
|
||||
}
|
||||
|
||||
|
||||
Here is a simple example of `FileFetcher`, which could fetch files from `file_storage` observer.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> from qlib.contrib.estimator.fetcher import FileFetcher
|
||||
>>> f = FileFetcher(experiments_dir=r'./')
|
||||
>>> print(f.get_experiments())
|
||||
|
||||
{
|
||||
'test_experiment': [
|
||||
{
|
||||
'id': '1',
|
||||
'config': ...
|
||||
},
|
||||
{
|
||||
'id': '2',
|
||||
'config': ...
|
||||
},
|
||||
{
|
||||
'id': '3',
|
||||
'config': ...
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
>>> print(f.get_experiment('test_experiment', '1'))
|
||||
|
||||
risk
|
||||
sub_bench mean 0.000662
|
||||
std 0.004487
|
||||
annual 0.166720
|
||||
sharpe 2.340526
|
||||
mdd -0.080516
|
||||
sub_cost mean 0.000577
|
||||
std 0.004482
|
||||
annual 0.145392
|
||||
sharpe 2.043494
|
||||
mdd -0.083584
|
||||
|
||||
If users use mongo observer when training, they should initialize their fether with mongo_url
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> from qlib.contrib.estimator.fetcher import MongoFetcher
|
||||
>>> f = MongoFetcher(mongo_url=..., db_name=...)
|
||||
|
||||
179
docs/component/model.rst
Normal file
179
docs/component/model.rst
Normal file
@@ -0,0 +1,179 @@
|
||||
.. _model:
|
||||
============================================
|
||||
Interday Model: Model Training & Prediction
|
||||
============================================
|
||||
|
||||
Introduction
|
||||
===================
|
||||
|
||||
``Interday Model`` is designed to make the prediction score about stocks. Users can use the ``Interday Model`` 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 Model`` can be used as a independent module also.
|
||||
|
||||
Base Class & Interface
|
||||
======================
|
||||
|
||||
``Qlib`` provides a base class `qlib.contrib.model.base.Model <../reference/api.html#module-qlib.contrib.model.base>`_, which all models should inherit from.
|
||||
|
||||
The base class provides the following interfaces:
|
||||
|
||||
- `__init__(**kwargs)`
|
||||
- Initialization.
|
||||
- If users use ``Estimator`` to start an `experiment`, the parameter of `__init__` method shoule be consistent with the hyperparameters in the configuration file.
|
||||
|
||||
- `fit(self, x_train, y_train, x_valid, y_valid, w_train=None, w_valid=None, **kwargs)`
|
||||
- Train model.
|
||||
- Parameter:
|
||||
- `x_train`, pd.DataFrame type, train feature
|
||||
The following example explains the value of `x_train`:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
KMID KLEN KMID2 KUP KUP2
|
||||
instrument datetime
|
||||
SH600004 2012-01-04 0.000000 0.017685 0.000000 0.012862 0.727275
|
||||
2012-01-05 -0.006473 0.025890 -0.250001 0.012945 0.499998
|
||||
2012-01-06 0.008117 0.019481 0.416666 0.008117 0.416666
|
||||
2012-01-09 0.016051 0.025682 0.624998 0.006421 0.250001
|
||||
2012-01-10 0.017323 0.026772 0.647057 0.003150 0.117648
|
||||
... ... ... ... ... ...
|
||||
SZ300273 2014-12-25 -0.005295 0.038697 -0.136843 0.016293 0.421052
|
||||
2014-12-26 -0.022486 0.041701 -0.539215 0.002453 0.058824
|
||||
2014-12-29 -0.031526 0.039092 -0.806451 0.000000 0.000000
|
||||
2014-12-30 -0.010000 0.032174 -0.310811 0.013913 0.432433
|
||||
2014-12-31 0.010917 0.020087 0.543479 0.001310 0.065216
|
||||
|
||||
|
||||
`x_train` is a pandas DataFrame, whose index is MultiIndex <instrument(str), datetime(pd.Timestamp)>. Each column of `x_train` corresponds to a feature, and the column name is the feature name.
|
||||
|
||||
.. note::
|
||||
|
||||
The number and names of the columns is determined by the data handler, please refer to `Data Handler <data.html#data-handler>`_ and `Estimator Data <estimator.html#about-data>`_.
|
||||
|
||||
- `y_train`, pd.DataFrame type, train label
|
||||
The following example explains the value of `y_train`:
|
||||
|
||||
.. code-block:: YAML
|
||||
|
||||
LABEL
|
||||
instrument datetime
|
||||
SH600004 2012-01-04 -0.798456
|
||||
2012-01-05 -1.366716
|
||||
2012-01-06 -0.491026
|
||||
2012-01-09 0.296900
|
||||
2012-01-10 0.501426
|
||||
... ...
|
||||
SZ300273 2014-12-25 -0.465540
|
||||
2014-12-26 0.233864
|
||||
2014-12-29 0.471368
|
||||
2014-12-30 0.411914
|
||||
2014-12-31 1.342723
|
||||
|
||||
`y_train` is a pandas DataFrame, whose index is MultiIndex <instrument(str), datetime(pd.Timestamp)>. The `LABEL` column represents the value of train label.
|
||||
|
||||
.. note::
|
||||
|
||||
The number and names of the columns is determined by the ``Data Handler``, please refer to `Data Handler <data.html#data-handler>`_.
|
||||
|
||||
- `x_valid`, pd.DataFrame type, validation feature
|
||||
The format of `x_valid` is same as `x_train`
|
||||
|
||||
|
||||
- `y_valid`, pd.DataFrame type, validation label
|
||||
The format of `y_valid` is same as `y_train`
|
||||
|
||||
- `w_train`(Optional args, default is None), pd.DataFrame type, train weight
|
||||
`w_train` is a pandas DataFrame, whose shape and index is same as `x_train`. The float value in `w_train` represents the weight of the feature at the same position in `x_train`.
|
||||
|
||||
- `w_train`(Optional args, default is None), pd.DataFrame type, validation weight
|
||||
`w_train` is a pandas DataFrame, whose shape and index is same as `x_valid`. The float value in `w_train` represents the weight of the feature at the same position in `x_train`.
|
||||
|
||||
- `predict(self, x_test, **kwargs)`
|
||||
- Predict test data 'x_test'
|
||||
- Parameter:
|
||||
- `x_test`, pd.DataFrame type, test features
|
||||
The form of `x_test` is same as `x_train` in 'fit' method.
|
||||
- Return:
|
||||
- `label`, np.ndarray type, test label
|
||||
The label of `x_test` that predicted by model.
|
||||
|
||||
- `score(self, x_test, y_test, w_test=None, **kwargs)`
|
||||
- Evaluate model with test feature/label
|
||||
- Parameter:
|
||||
- `x_test`, pd.DataFrame type, test feature
|
||||
The format of `x_test` is same as `x_train` in `fit` method.
|
||||
|
||||
- `x_test`, pd.DataFrame type, test label
|
||||
The format of `y_test` is same as `y_train` in `fit` method.
|
||||
|
||||
- `w_test`, pd.DataFrame type, test weight
|
||||
The format of `w_test` is same as `w_train` in `fit` method.
|
||||
- Return: float type, evaluation score
|
||||
|
||||
For other interfaces such as `save`, `load`, `finetune`, please refer to `Model API <../reference/api.html#module-qlib.contrib.model.base>`_.
|
||||
|
||||
Example
|
||||
==================
|
||||
|
||||
``Qlib`` provides ``LightGBM`` and ``DNN`` models as the baseline, the following steps shows how to run`` LightGBM`` as an independent module.
|
||||
|
||||
- Initialize ``Qlib`` with `qlib.init` first, please refer to `initialization <initialization.rst>`_.
|
||||
- Run the following code to get the prediction score `pred_score`
|
||||
.. code-block:: Python
|
||||
|
||||
from qlib.contrib.estimator.handler import QLibDataHandlerClose
|
||||
from qlib.contrib.model.gbdt import LGBModel
|
||||
|
||||
DATA_HANDLER_CONFIG = {
|
||||
"dropna_label": True,
|
||||
"start_date": "2007-01-01",
|
||||
"end_date": "2020-08-01",
|
||||
"market": MARKET,
|
||||
}
|
||||
|
||||
TRAINER_CONFIG = {
|
||||
"train_start_date": "2007-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",
|
||||
}
|
||||
|
||||
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_score = pd.DataFrame(index=_pred.index)
|
||||
pred_score["score"] = _pred.iloc(axis=1)[0]
|
||||
|
||||
.. note:: `QLibDataHandlerClose` is the data handler provided by ``Qlib``, please refer to `Data Handler <data.html#data-handler>`_.
|
||||
|
||||
Also, the above example has been given in ``examples/train_backtest_analyze.ipynb``.
|
||||
|
||||
Custom Model
|
||||
===================
|
||||
|
||||
Qlib supports custom models. If users are interested in customizing their own models and integrating the models into ``Qlib``, please refer to `Custom Model Integration <../start/integration.html>`_.
|
||||
|
||||
|
||||
API
|
||||
===================
|
||||
Please refer to `Model API <../reference/api.html#module-qlib.contrib.model.base>`_.
|
||||
197
docs/component/report.rst
Normal file
197
docs/component/report.rst
Normal file
@@ -0,0 +1,197 @@
|
||||
.. _report:
|
||||
==========================================
|
||||
Aanalysis: Evaluation & Results Analysis
|
||||
==========================================
|
||||
|
||||
Introduction
|
||||
===================
|
||||
|
||||
``Aanalysis`` is designed to show the graphical reports of ``Intraday Trading`` , which helps users to evaluate and analyse investment portfolios visually. There are the following graphics to view:
|
||||
|
||||
- analysis_position
|
||||
- report_graph
|
||||
- score_ic_graph
|
||||
- cumulative_return_graph
|
||||
- risk_analysis_graph
|
||||
- rank_label_graph
|
||||
|
||||
- analysis_model
|
||||
- model_performance_graph
|
||||
|
||||
|
||||
Graphical Reports
|
||||
===================
|
||||
|
||||
Users can run the following code to get all supported reports.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> import qlib.contrib.report as qcr
|
||||
>>> print(qcr.GRAPH_NAME_LISt)
|
||||
['analysis_position.report_graph', 'analysis_position.score_ic_graph', 'analysis_position.cumulative_return_graph', 'analysis_position.risk_analysis_graph', 'analysis_position.rank_label_graph', 'analysis_model.model_performance_graph']
|
||||
|
||||
.. note::
|
||||
|
||||
For more details, please refer to the function document: similar to ``help(qcr.analysis_position.report_graph)``
|
||||
|
||||
|
||||
|
||||
Usage&Example
|
||||
===================
|
||||
|
||||
Usage of `analysis_position.report`
|
||||
-----------------------------------
|
||||
|
||||
API
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_position.report
|
||||
:members:
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note::
|
||||
|
||||
- Axis X: Trading day
|
||||
- Axis Y: Accumulated value
|
||||
- The shaded part above: Maximum drawdown corresponding to `cum return`
|
||||
- The shaded part below: Maximum drawdown corresponding to `cum ex return wo cost` %
|
||||
|
||||
.. image:: ../_static/img/analysis/report.png
|
||||
|
||||
|
||||
Usage of `analysis_position.score_ic`
|
||||
-------------------------------------
|
||||
|
||||
API
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_position.score_ic
|
||||
:members:
|
||||
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note::
|
||||
|
||||
- Axis X: Trading day
|
||||
- Axis Y: `Ref($close, -1)/$close - 1` and `score` IC%
|
||||
|
||||
.. image:: ../_static/img/analysis/score_ic.png
|
||||
|
||||
|
||||
Usage of `analysis_position.cumulative_return`
|
||||
----------------------------------------------
|
||||
|
||||
API
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_position.cumulative_return
|
||||
:members:
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note::
|
||||
|
||||
- Cumulative return graphics.
|
||||
- Axis X: Trading day
|
||||
- Axis Y:
|
||||
- Above axis Y: `(((Ref($close, -1)/$close - 1) * weight).sum() / weight.sum()).cumsum()`
|
||||
- Below axis Y: Daily weight sum
|
||||
- In the **sell** graph, `y < 0` stands for profit; in other cases, `y > 0` stands for profit.
|
||||
- In the **buy_minus_sell** graph, the **y** value of the **weight** graph at the bottom is `buy_weight + sell_weight`.
|
||||
- In each graph, the **red line** in the histogram on the right represents the average.%
|
||||
|
||||
.. image:: ../_static/img/analysis/cumulative_return_buy.png
|
||||
|
||||
.. image:: ../_static/img/analysis/cumulative_return_sell.png
|
||||
|
||||
.. image:: ../_static/img/analysis/cumulative_return_buy_minus_sell.png
|
||||
|
||||
.. image:: ../_static/img/analysis/cumulative_return_hold.png
|
||||
|
||||
|
||||
Usage of `analysis_position.risk_analysis`
|
||||
----------------------------------------------
|
||||
|
||||
API
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_position.risk_analysis
|
||||
:members:
|
||||
|
||||
|
||||
.. note::
|
||||
|
||||
- annual/mdd/sharpe/std graphics
|
||||
- Axis X: Trading days are grouped by month
|
||||
- Axis Y: monthly(trading date) value
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. image:: ../_static/img/analysis/risk_analysis_bar.png
|
||||
|
||||
.. image:: ../_static/img/analysis/risk_analysis_annual.png
|
||||
|
||||
.. image:: ../_static/img/analysis/risk_analysis_mdd.png
|
||||
|
||||
.. image:: ../_static/img/analysis/risk_analysis_sharpe.png
|
||||
|
||||
.. image:: ../_static/img/analysis/risk_analysis_std.png
|
||||
|
||||
|
||||
Usage of `analysis_position.rank_label`
|
||||
----------------------------------------------
|
||||
|
||||
API
|
||||
~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_position.rank_label
|
||||
:members:
|
||||
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. note::
|
||||
|
||||
- hold/sell/buy graphics:
|
||||
- Axis X: Trading day
|
||||
- Axis Y: Percentage of `'Ref($close, -1)/$close - 1'.rank(ascending=False) / (number of lines on the day) * 100` every trading day. (`ascending=False`: The higher the value, the higher the ranking)%
|
||||
|
||||
.. image:: ../_static/img/analysis/rank_label_hold.png
|
||||
|
||||
.. image:: ../_static/img/analysis/rank_label_buy.png
|
||||
|
||||
.. image:: ../_static/img/analysis/rank_label_sell.png
|
||||
|
||||
|
||||
|
||||
Usage of `analysis_model.analysis_model_performance`
|
||||
-----------------------------------------------------
|
||||
|
||||
API
|
||||
~~~~~
|
||||
|
||||
.. automodule:: qlib.contrib.report.analysis_model.analysis_model_performance
|
||||
:members:
|
||||
|
||||
|
||||
Graphical Result
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_cumulative_return.png
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_long_short.png
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_IC.png
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_monthly_IC.png
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_NDQ.png
|
||||
|
||||
.. image:: ../_static/img/analysis/analysis_model_auto_correlation.png
|
||||
119
docs/component/strategy.rst
Normal file
119
docs/component/strategy.rst
Normal file
@@ -0,0 +1,119 @@
|
||||
.. _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 componets in ``Qlib`` are designed in a loosely-coupled way, ``Interday Strategy`` can be used as a independent module also.
|
||||
|
||||
``Qlib`` provides several implemented trading strategy. Also, ``Qlib`` supports costom 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.
|
||||
|
||||
User can inherit `BaseStrategy` to costomize 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 inteface `generate_target_weight_position` to costomize their strategy class, which only focuses on the target positions.
|
||||
|
||||
Implemented Strategy
|
||||
====================
|
||||
|
||||
Qlib provides several implemented strategy classes `TopkDropoutStrategy`.
|
||||
|
||||
|
||||
TopkDropoutStrategy
|
||||
------------------
|
||||
`TopkDropoutStrategy` is a subclass of `BaseStrategy` and implement the interface `generate_order_list` whose process is as follows.
|
||||
|
||||
- Adopt the 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 worst prediction score will be sold, and the same number of unheld stocks with 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 konw more about ``Interday Strategy``, please refer to `Strategy API <../reference/api.html>`_.
|
||||
Reference in New Issue
Block a user