Skip to content
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

Add docs for qlib.rl #1322

Merged
merged 45 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ddbbcc6
Add docs for qlib.rl
lwwang1995 Oct 19, 2022
9dd860c
Update docs for qlib.rl
lwwang1995 Oct 19, 2022
c7b68b1
Add homepage introduct to RL framework
you-n-g Oct 20, 2022
d13262f
Update index Link
you-n-g Oct 20, 2022
8cacc96
Fix Icon
you-n-g Oct 20, 2022
c3577e1
typo
you-n-g Oct 20, 2022
b139e7d
Merge remote-tracking branch 'origin/main' into HEAD
you-n-g Oct 20, 2022
a0d3621
Update catelog
you-n-g Oct 20, 2022
1a62af9
Update docs for qlib.rl
lwwang1995 Oct 20, 2022
c9b2198
Update docs for qlib.rl
lwwang1995 Oct 21, 2022
5a75c9d
Update figure
lwwang1995 Oct 21, 2022
77fbb16
Update docs for qlib.rl
lwwang1995 Oct 21, 2022
5d2f21f
Update setup.py
you-n-g Oct 22, 2022
160f951
Merge remote-tracking branch 'origin/main' into HEAD
you-n-g Oct 22, 2022
4b705a9
FIx setup.py
you-n-g Oct 22, 2022
145414b
Update docs and fix some typos
lwwang1995 Oct 24, 2022
f215418
Fix the reference to RL docs
lwwang1995 Oct 24, 2022
b688db7
Update framework.svg
you-n-g Oct 24, 2022
5035af1
Update framework.svg
you-n-g Oct 24, 2022
3b182e1
Update framework.svg
you-n-g Oct 24, 2022
834c4f4
Update docs for qlibrl.
lwwang1995 Oct 27, 2022
0b17397
Update docs for qlibrl.
lwwang1995 Oct 27, 2022
7bfc937
Update docs for Qlibrl.
lwwang1995 Oct 27, 2022
21b765d
Update docs for qlibrl.
lwwang1995 Oct 27, 2022
1703492
Update docs for qlibrl.
lwwang1995 Oct 27, 2022
4d73676
Update docs for qlibrl.
lwwang1995 Oct 27, 2022
f7713e2
Add new framework
you-n-g Oct 27, 2022
a59f844
Update jpg
you-n-g Oct 27, 2022
47667a7
Update framework.svg
you-n-g Oct 28, 2022
129c1a8
Update framework.svg
you-n-g Oct 28, 2022
db543fc
Update Qlib framework and description
you-n-g Oct 28, 2022
34e2bc4
Update grammar
you-n-g Oct 28, 2022
8d7df20
Update README.md
you-n-g Oct 28, 2022
b3eec1c
Update README.md
you-n-g Oct 28, 2022
946177d
Update docs/component/rl.rst
lwwang1995 Oct 28, 2022
04a9b8f
Update docs/component/rl.rst
lwwang1995 Oct 28, 2022
e248066
Update docs for qlib.rl
lwwang1995 Nov 2, 2022
6020b86
Change theme for docs.
lwwang1995 Nov 3, 2022
5db5ea9
Update docs for qlib.rl
lwwang1995 Nov 4, 2022
7b84f49
Update docs for qlib.rl
lwwang1995 Nov 7, 2022
c47a460
Update docs for qlib.rl
lwwang1995 Nov 7, 2022
cf3642d
Update docs for qlib.rl.
lwwang1995 Nov 7, 2022
d723685
Update docs for qlib.rl
lwwang1995 Nov 8, 2022
6484cfa
Update docs for qlib.rl
lwwang1995 Nov 8, 2022
0db199c
Update docs for qlib.rl
lwwang1995 Nov 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/_static/img/qlib_rl_highlevel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions docs/component/highfreq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ In order to support the joint backtest strategies in multiple levels, a correspo

Besides backtesting, the optimization of strategies from different levels is not standalone and can be affected by each other.
For example, the best portfolio management strategy may change with the performance of order executions(e.g. a portfolio with higher turnover may becomes a better choice when we improve the order execution strategies).
To achieve the overall good performance , it is necessary to consider the interaction of strategies in different level.
To achieve the overall good performance , it is necessary to consider the interaction of strategies in different level.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra useless blank.


Therefore, building a new framework for trading in multiple levels becomes necessary to solve the various problems mentioned above, for which we designed a nested decision execution framework that consider the interaction of strategies.

.. image:: ../_static/img/framework.svg

The design of the framework is shown in the yellow part in the middle of the figure above. Each level consists of ``Trading Agent`` and ``Execution Env``. ``Trading Agent`` has its own data processing module (``Information Extractor``), forecasting module (``Forecast Model``) and decision generator (``Decision Generator``). The trading algorithm generates the decisions by the ``Decision Generator`` based on the forecast signals output by the ``Forecast Module``, and the decisions generated by the trading algorithm are passed to the ``Execution Env``, which returns the execution results.

The frequency of trading algorithm, decision content and execution environment can be customized by users (e.g. intraday trading, daily-frequency trading, weekly-frequency trading), and the execution environment can be nested with finer-grained trading algorithm and execution environment inside (i.e. sub-workflow in the figure, e.g. daily-frequency orders can be turned into finer-grained decisions by splitting orders within the day). The flexibility of nested decision execution framework makes it easy for users to explore the effects of combining different levels of trading strategies and break down the optimization barriers between different levels of trading algorithm.
The frequency of trading algorithm, decision content and execution environment can be customized by users (e.g. intraday trading, daily-frequency trading, weekly-frequency trading), and the execution environment can be nested with finer-grained trading algorithm and execution environment inside (i.e. sub-workflow in the figure, e.g. daily-frequency orders can be turned into finer-grained decisions by splitting orders within the day). The flexibility of nested decision execution framework makes it easy for users to explore the effects of combining different levels of trading strategies and break down the optimization barriers between different levels of trading algorithm.

The optimization for the nested decision execution framework can be implemented with an RL-based method, which can be supported by `qlib.rl<https://github.com/microsoft/qlib/tree/main/examples/rl>`_.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reference to the docs will be better than an example.
I think keeping the example will also be helpful


Example
=======
Expand Down
36 changes: 28 additions & 8 deletions docs/component/rl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ Reinforcement Learning in Quantitative Trading

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding a summary upfront to describe what kind of problem we intend to solve.

Introduction
============
The Qlib Reinforcement Learning toolkit (QlibRL) is the RL platform for quantitative investment. It contains a full set of components that cover the entire lifecycle of an RL pipeline, including building the simulator of the market, shaping states & actions, training policies (strategies), and backtesting strategies in the simulated environment.
The Qlib Reinforcement Learning toolkit (QlibRL) is an RL platform for quantitative investment. It contains a full set of components that cover the entire lifecycle of an RL pipeline, including building the simulator of the market, shaping states & actions, training policies (strategies), and backtesting strategies in the simulated environment.

QlibRL is basically implemented within the frameworks of Tianshou and Gym. The high-level structure of QlibRL is demonstrated below:
QlibRL is basically implemented with the support of Tianshou and Gym frameworks. The high-level structure of QlibRL is demonstrated below:

.. image:: ../_static/img/qlib_rl_highlevel.png

Here, we briefly introduce each of the components in the figure.
Here, we briefly introduce each component in the figure.

Base Modules
============
Expand All @@ -24,7 +24,7 @@ EnvWrapper is the complete capsulation of the simulated environment. It receives
In QlibRL, EnvWrapper is a subclass of gym.Env, so it implements all necessary interfaces of gym.Env. Any classes or pipelines that accept gym.Env should also accept EnvWrapper. Developers do not need to implement their own EnvWrapper to build their own environment. Instead, they only need to implement 4 components of the EnvWrapper:

- `Simulator`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to class reference with :class:`~qlib.rl.Simulator` .

The simulator is the core component responsible for the environment simulation. Developers could implement all the logic that is directly related to the environment simulation in the Simulator in any way they like. In QlibRL, there are already two implementations of Simulator: 1) ``SingleAssetOrderExecution``, which is built based on Qlib's backtest toolkits. 2) ``SimpleSingleAssetOrderExecution``, which is built based on naive simulation logic.
The simulator is the core component responsible for the environment simulation. Developers could implement all the logic that is directly related to the environment simulation in the Simulator in any way they like. In QlibRL, there are already two implementations of Simulator for single asset trading: 1) ``SingleAssetOrderExecution``, which is built based on Qlib's backtest toolkits and hence considers a lot of practical trading details but is slow. 2) ``SimpleSingleAssetOrderExecution``, which is built based on a simplified trading simulator, which ignores a lot of details (e.g. trading limitations, rounding) but is quite fast.
- `State interpreter`
The state interpreter is responsible for "interpret" states in the original format (format provided by the simulator) into states in a format that the policy could understand. For example, transform unstructured raw features into numerical tensors.
- `Action interpreter`
Expand Down Expand Up @@ -60,15 +60,35 @@ Order Execution
------------
As a fundamental problem in algorithmic trading, order execution aims at fulfilling a specific trading order, either liquidation or acquirement, for a given instrument. Essentially, the goal of order execution is twofold: it not only requires to fulfill the whole order but also targets a more economical execution with maximizing profit gain (or minimizing capital loss). The order execution with only one order of liquidation or acquirement is called single-asset order execution.

Considering stock investment always aim to pursue long-term maximized profits, is usually behaved in the form of a sequential process of continuously adjusting the asset portfolio, execution for multiple orders, including order of liquidation and acquirement, brings more constraints and making the sequence of execution for different orders should be considered, e.g. before executing an order to buy some stocks, we have to sell at least one stock. The order execution with multiple assets is called multi-asset order execution.
Considering stock investment always aim to pursue long-term maximized profits, is usually manifests as a sequential process of continuously adjusting the asset portfolios, execution for multiple orders, including order of liquidation and acquirement, brings more constraints and making the sequence of execution for different orders should be considered, e.g. before executing an order to buy some stocks, we have to sell at least one stock. The order execution with multiple assets is called multi-asset order execution.

According to the order execution’s trait of sequential decision making, an RL-based solution could be applied to solve the order execution. With an RL-based solution, an agent optimizes execution strategy through interacting with the market environment.

With QlibRL, the RL algorithm in the above scenarios can be easily implemented.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add an extra section for nested Portfolio Construction & Order Execution
and emphasize the difference from traditional methods.

Nested Portfolio Construction and Order Executor
------------
QlibRL make it possible to jointly optimize different levels of strategies/models/agents. Take `Nested Decision Execution Framework <https://github.com/microsoft/qlib/blob/main/examples/nested_decision_execution>`_ an example of, optimization of order execution strategy and portfolio management strategy can interact with each other to maximize returns.

Base Class & Interface
============
``Qlib`` provides a set of APIs for developers to further simplify their development, including base classes for Interpreter, Simulator and Reward.

.. autoclass:: qlib.rl.interpreter.Interpreter
:members:

.. autoclass:: qlib.rl.simulator.Simulator
:members:

.. autoclass:: qlib.rl.reward.Reward
:members:


Example
============
QlibRL provides a set of APIs for developers to further simplify their development. For example, if developers have already defined their simulator / interpreters / reward function / policy, they could launch the training pipeline by simply running:
``Qlib`` provides an example based on order execution, specifically, `qlib.rl.order_execution.simulator_qlib.SingleAssetOrderExecution<https://github.com/microsoft/qlib/blob/main/qlib/rl/order_execution/simulator_qlib.py>`_ and `qlib.rl.order_execution.simulator_simple.SingleAssetOrderExecutionSimple<https://github.com/microsoft/qlib/blob/main/qlib/rl/order_execution/simulator_simple.py>`_ as examples for simulator, two `StateInterpreter<https://github.com/microsoft/qlib/blob/main/qlib/rl/order_execution/interpreter.py>`_ and `ActionInterpreter<https://github.com/microsoft/qlib/blob/main/qlib/rl/order_execution/interpreter.py>`_ as examples for interpreter, and `qlib.rl.order_execution.reward.PAPenaltyReward<https://github.com/microsoft/qlib/blob/main/qlib/rl/order_execution/reward.py>`_ as an example for reward.

If developers have already defined their simulator / interpreters / reward function / policy, they could launch the training pipeline by simply running:

.. code-block:: python
train(
Expand All @@ -79,7 +99,7 @@ QlibRL provides a set of APIs for developers to further simplify their developme
policy=policy,
reward=PAPenaltyReward(),
vessel_kwargs={
"episode_per_iter": 100,
"episode_per_iter": 100, 6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the 6 mean here?

"update_kwargs": {
"batch_size": 64,
"repeat": 5,
Expand All @@ -91,4 +111,4 @@ QlibRL provides a set of APIs for developers to further simplify their developme
},
)

We demonstrate an example of an implementation of a single asset order execution task based on QlibRL, the details about the example can be found `here <../../examples/rl/README.md>`_.
We demonstrate an example of an implementation of a single asset order execution task based on QlibRL, the details about the example can be found `here <../../examples/rl/README.md>`_. RL-based portfolio construction learning will be released in the future.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Document Structure

.. toctree::
:maxdepth: 3
:caption: COMPONENTS:
:caption: MAIN COMPONENTS:

Workflow: Workflow Management <component/workflow.rst>
Data Layer: Data Framework & Usage <component/data.rst>
Expand All @@ -48,7 +48,7 @@ Document Structure

.. toctree::
:maxdepth: 3
:caption: ADVANCED TOPICS:
:caption: OTHER COMPONENTS/FEATURES/TOPICS:

Building Formulaic Alphas <advanced/alpha.rst>
Online & Offline mode <advanced/server.rst>
Expand Down
13 changes: 13 additions & 0 deletions docs/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,16 @@ Serializable

.. automodule:: qlib.utils.serial.Serializable
:members:

RL
==============
``Qlib`` provides a series of base classes for Interpreter, Simulator and Reward.

.. autoclass:: qlib.rl.interpreter.Interpreter
:members:

.. autoclass:: qlib.rl.simulator.Simulator
:members:

.. autoclass:: qlib.rl.reward.Reward
:members:
11 changes: 4 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import io
import os
import numpy

Expand All @@ -26,9 +25,6 @@ def get_version(rel_path: str) -> str:
DESCRIPTION = "A Quantitative-research Platform"
REQUIRES_PYTHON = ">=3.5.0"

from pathlib import Path
from shutil import copyfile

VERSION = get_version("qlib/__init__.py")

# Detect Cython
Expand Down Expand Up @@ -148,15 +144,16 @@ def get_version(rel_path: str) -> str:
# References: https://github.com/python/typeshed/issues/8799
"mypy<0.981",
"flake8",
# The 5.0.0 version of importlib-metadata removed the deprecated endpoint,
# which prevented flake8 from working properly, so we restricted the version of importlib-metadata.
# To help ensure the dependencies of flake8 https://github.com/python/importlib_metadata/issues/406
"importlib-metadata<5.0.0",
"readthedocs_sphinx_ext",
"cmake",
"lxml",
"baostock",
"yahooquery",
"beautifulsoup4",
# The 5.0.0 version of importlib-metadata removed the deprecated endpoint,
# which prevented flake8 from working properly, so we restricted the version of importlib-metadata.
"importlib-metadata<5.0.0",
"tianshou",
"gym>=0.24", # If you do not put gym at the end, gym will degrade causing pytest results to fail.
],
Expand Down