Skip to content

Commit d08146c

Browse files
authored
Merge pull request #290 from you-n-g/online_srv
init version of online serving and rolling
2 parents 142a9dc + 8c3a08b commit d08146c

39 files changed

+3903
-125
lines changed

docs/_static/img/online_serving.png

440 KB
Loading

docs/advanced/serial.rst

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Serializable Class
1414

1515
``Qlib`` provides a base class ``qlib.utils.serial.Serializable``, whose state can be dumped into or loaded from disk in `pickle` format.
1616
When users dump the state of a ``Serializable`` instance, the attributes of the instance whose name **does not** start with `_` will be saved on the disk.
17+
However, users can use ``config`` method or override ``default_dump_all`` attribute to prevent this feature.
18+
19+
Users can also override ``pickle_backend`` attribute to choose a pickle backend. The supported value is "pickle" (default and common) and "dill" (dump more things such as function, more information in `here <https://pypi.org/project/dill/>`_).
1720

1821
Example
1922
==========================

docs/advanced/task_management.rst

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
.. _task_management:
2+
3+
=================================
4+
Task Management
5+
=================================
6+
.. currentmodule:: qlib
7+
8+
9+
Introduction
10+
=============
11+
12+
The `Workflow <../component/introduction.html>`_ part introduces how to run research workflow in a loosely-coupled way. But it can only execute one ``task`` when you use ``qrun``.
13+
To automatically generate and execute different tasks, ``Task Management`` provides a whole process including `Task Generating`_, `Task Storing`_, `Task Training`_ and `Task Collecting`_.
14+
With this module, users can run their ``task`` automatically at different periods, in different losses, or even by different models.
15+
16+
This whole process can be used in `Online Serving <../component/online.html>`_.
17+
18+
An example of the entire process is shown `here <https://github.com/microsoft/qlib/tree/main/examples/model_rolling/task_manager_rolling.py>`_.
19+
20+
Task Generating
21+
===============
22+
A ``task`` consists of `Model`, `Dataset`, `Record`, or anything added by users.
23+
The specific task template can be viewed in
24+
`Task Section <../component/workflow.html#task-section>`_.
25+
Even though the task template is fixed, users can customize their ``TaskGen`` to generate different ``task`` by task template.
26+
27+
Here is the base class of ``TaskGen``:
28+
29+
.. autoclass:: qlib.workflow.task.gen.TaskGen
30+
:members:
31+
32+
``Qlib`` provides a class `RollingGen <https://github.com/microsoft/qlib/tree/main/qlib/workflow/task/gen.py>`_ to generate a list of ``task`` of the dataset in different date segments.
33+
This class allows users to verify the effect of data from different periods on the model in one experiment. More information is `here <../reference/api.html#TaskGen>`_.
34+
35+
Task Storing
36+
===============
37+
To achieve higher efficiency and the possibility of cluster operation, ``Task Manager`` will store all tasks in `MongoDB <https://www.mongodb.com/>`_.
38+
``TaskManager`` can fetch undone tasks automatically and manage the lifecycle of a set of tasks with error handling.
39+
Users **MUST** finish the configuration of `MongoDB <https://www.mongodb.com/>`_ when using this module.
40+
41+
Users need to provide the MongoDB URL and database name for using ``TaskManager`` in `initialization <../start/initialization.html#Parameters>`_ or make a statement like this.
42+
43+
.. code-block:: python
44+
45+
from qlib.config import C
46+
C["mongo"] = {
47+
"task_url" : "mongodb://localhost:27017/", # your MongoDB url
48+
"task_db_name" : "rolling_db" # database name
49+
}
50+
51+
.. autoclass:: qlib.workflow.task.manage.TaskManager
52+
:members:
53+
54+
More information of ``Task Manager`` can be found in `here <../reference/api.html#TaskManager>`_.
55+
56+
Task Training
57+
===============
58+
After generating and storing those ``task``, it's time to run the ``task`` which is in the *WAITING* status.
59+
``Qlib`` provides a method called ``run_task`` to run those ``task`` in task pool, however, users can also customize how tasks are executed.
60+
An easy way to get the ``task_func`` is using ``qlib.model.trainer.task_train`` directly.
61+
It will run the whole workflow defined by ``task``, which includes *Model*, *Dataset*, *Record*.
62+
63+
.. autofunction:: qlib.workflow.task.manage.run_task
64+
65+
Meanwhile, ``Qlib`` provides a module called ``Trainer``.
66+
67+
.. autoclass:: qlib.model.trainer.Trainer
68+
:members:
69+
70+
``Trainer`` will train a list of tasks and return a list of model recorders.
71+
``Qlib`` offer two kinds of Trainer, TrainerR is the simplest way and TrainerRM is based on TaskManager to help manager tasks lifecycle automatically.
72+
If you do not want to use ``Task Manager`` to manage tasks, then use TrainerR to train a list of tasks generated by ``TaskGen`` is enough.
73+
`Here <../reference/api.html#Trainer>`_ are the details about different ``Trainer``.
74+
75+
Task Collecting
76+
===============
77+
To collect the results of ``task`` after training, ``Qlib`` provides `Collector <../reference/api.html#Collector>`_, `Group <../reference/api.html#Group>`_ and `Ensemble <../reference/api.html#Ensemble>`_ to collect the results in a readable, expandable and loosely-coupled way.
78+
79+
`Collector <../reference/api.html#Collector>`_ can collect objects from everywhere and process them such as merging, grouping, averaging and so on. It has 2 step action including ``collect`` (collect anything in a dict) and ``process_collect`` (process collected dict).
80+
81+
`Group <../reference/api.html#Group>`_ also has 2 steps including ``group`` (can group a set of object based on `group_func` and change them to a dict) and ``reduce`` (can make a dict become an ensemble based on some rule).
82+
For example: {(A,B,C1): object, (A,B,C2): object} ---``group``---> {(A,B): {C1: object, C2: object}} ---``reduce``---> {(A,B): object}
83+
84+
`Ensemble <../reference/api.html#Ensemble>`_ can merge the objects in an ensemble.
85+
For example: {C1: object, C2: object} ---``Ensemble``---> object
86+
87+
So the hierarchy is ``Collector``'s second step corresponds to ``Group``. And ``Group``'s second step correspond to ``Ensemble``.
88+
89+
For more information, please see `Collector <../reference/api.html#Collector>`_, `Group <../reference/api.html#Group>`_ and `Ensemble <../reference/api.html#Ensemble>`_, or the `example <https://github.com/microsoft/qlib/tree/main/examples/model_rolling/task_manager_rolling.py>`_.

docs/component/online.rst

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. _online:
2+
3+
=================================
4+
Online Serving
5+
=================================
6+
.. currentmodule:: qlib
7+
8+
9+
Introduction
10+
=============
11+
12+
.. image:: ../_static/img/online_serving.png
13+
:align: center
14+
15+
16+
In addition to backtesting, one way to test a model is effective is to make predictions in real market conditions or even do real trading based on those predictions.
17+
``Online Serving`` is a set of modules for online models using the latest data,
18+
which including `Online Manager <#Online Manager>`_, `Online Strategy <#Online Strategy>`_, `Online Tool <#Online Tool>`_, `Updater <#Updater>`_.
19+
20+
`Here <https://github.com/microsoft/qlib/tree/main/examples/online_srv>`_ are several examples for reference, which demonstrate different features of ``Online Serving``.
21+
If you have many models or `task` needs to be managed, please consider `Task Management <../advanced/task_management.html>`_.
22+
The `examples <https://github.com/microsoft/qlib/tree/main/examples/online_srv>`_ are based on some components in `Task Management <../advanced/task_management.html>`_ such as ``TrainerRM`` or ``Collector``.
23+
24+
Online Manager
25+
=============
26+
27+
.. automodule:: qlib.workflow.online.manager
28+
:members:
29+
30+
Online Strategy
31+
=============
32+
33+
.. automodule:: qlib.workflow.online.strategy
34+
:members:
35+
36+
Online Tool
37+
=============
38+
39+
.. automodule:: qlib.workflow.online.utils
40+
:members:
41+
42+
Updater
43+
=============
44+
45+
.. automodule:: qlib.workflow.online.update
46+
:members:

docs/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Document Structure
4242
Intraday Trading: Model&Strategy Testing <component/backtest.rst>
4343
Qlib Recorder: Experiment Management <component/recorder.rst>
4444
Analysis: Evaluation & Results Analysis <component/report.rst>
45+
Online Serving: Online Management & Strategy & Tool <component/online.rst>
4546

4647
.. toctree::
4748
:maxdepth: 3
@@ -50,6 +51,7 @@ Document Structure
5051
Building Formulaic Alphas <advanced/alpha.rst>
5152
Online & Offline mode <advanced/server.rst>
5253
Serialization <advanced/serial.rst>
54+
Task Management <advanced/task_management.rst>
5355

5456
.. toctree::
5557
:maxdepth: 3

docs/reference/api.rst

+68-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,70 @@ Record Template
154154
.. automodule:: qlib.workflow.record_temp
155155
:members:
156156

157+
Task Management
158+
====================
159+
160+
161+
TaskGen
162+
--------------------
163+
.. automodule:: qlib.workflow.task.gen
164+
:members:
165+
166+
TaskManager
167+
--------------------
168+
.. automodule:: qlib.workflow.task.manage
169+
:members:
170+
171+
Trainer
172+
--------------------
173+
.. automodule:: qlib.model.trainer
174+
:members:
175+
176+
Collector
177+
--------------------
178+
.. automodule:: qlib.workflow.task.collect
179+
:members:
180+
181+
Group
182+
--------------------
183+
.. automodule:: qlib.model.ens.group
184+
:members:
185+
186+
Ensemble
187+
--------------------
188+
.. automodule:: qlib.model.ens.ensemble
189+
:members:
190+
191+
Utils
192+
--------------------
193+
.. automodule:: qlib.workflow.task.utils
194+
:members:
195+
196+
197+
Online Serving
198+
====================
199+
200+
201+
Online Manager
202+
--------------------
203+
.. automodule:: qlib.workflow.online.manager
204+
:members:
205+
206+
Online Strategy
207+
--------------------
208+
.. automodule:: qlib.workflow.online.strategy
209+
:members:
210+
211+
Online Tool
212+
--------------------
213+
.. automodule:: qlib.workflow.online.utils
214+
:members:
215+
216+
RecordUpdater
217+
--------------------
218+
.. automodule:: qlib.workflow.online.update
219+
:members:
220+
157221

158222
Utils
159223
====================
@@ -162,4 +226,7 @@ Serializable
162226
--------------------
163227

164228
.. automodule:: qlib.utils.serial.Serializable
165-
:members:
229+
:members:
230+
231+
232+

docs/start/initialization.rst

+11
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ Besides `provider_uri` and `region`, `qlib.init` has other parameters. The follo
7575
"default_exp_name": "Experiment",
7676
}
7777
})
78+
- `mongo`
79+
Type: dict, optional parameter, the setting of `MongoDB <https://www.mongodb.com/>`_ which will be used in some features such as `Task Management <../advanced/task_management.html>`_, with high performance and clustered processing.
80+
Users need finished `installation <https://www.mongodb.com/try/download/community>`_ firstly, and run it in a fixed URL.
81+
82+
.. code-block:: Python
83+
84+
# For example, you can initialize qlib below
85+
qlib.init(provider_uri=provider_uri, region=REG_CN, mongo={
86+
"task_url": "mongodb://localhost:27017/", # your mongo url
87+
"task_db_name": "rolling_db", # the database name of Task Management
88+
})

0 commit comments

Comments
 (0)