|
|
| Quick Start |
| ============ |
| .. currentmodule:: qlib |
|
|
| QlibRL provides an example of an implementation of a single asset order execution task and the following is an example of the config file to train with QlibRL. |
|
|
| .. code-block:: yaml |
|
|
| simulator: |
| |
| time_per_step: 30 |
| |
| vol_limit: null |
| env: |
| |
| concurrency: 1 |
| |
| parallel_mode: dummy |
| action_interpreter: |
| class: CategoricalActionInterpreter |
| kwargs: |
| |
| values: 14 |
| |
| max_step: 8 |
| module_path: qlib.rl.order_execution.interpreter |
| state_interpreter: |
| class: FullHistoryStateInterpreter |
| kwargs: |
| |
| data_dim: 6 |
| |
| data_ticks: 240 |
| |
| max_step: 8 |
| |
| processed_data_provider: |
| class: PickleProcessedDataProvider |
| module_path: qlib.rl.data.pickle_styled |
| kwargs: |
| data_dir: ./data/pickle_dataframe/feature |
| module_path: qlib.rl.order_execution.interpreter |
| reward: |
| class: PAPenaltyReward |
| kwargs: |
| |
| penalty: 100.0 |
| module_path: qlib.rl.order_execution.reward |
| data: |
| source: |
| order_dir: ./data/training_order_split |
| data_dir: ./data/pickle_dataframe/backtest |
| |
| total_time: 240 |
| |
| default_start_time: 0 |
| |
| default_end_time: 240 |
| proc_data_dim: 6 |
| num_workers: 0 |
| queue_size: 20 |
| network: |
| class: Recurrent |
| module_path: qlib.rl.order_execution.network |
| policy: |
| class: PPO |
| kwargs: |
| lr: 0.0001 |
| module_path: qlib.rl.order_execution.policy |
| runtime: |
| seed: 42 |
| use_cuda: false |
| trainer: |
| max_epoch: 2 |
| |
| repeat_per_collect: 5 |
| earlystop_patience: 2 |
| |
| episode_per_collect: 20 |
| batch_size: 16 |
| |
| val_every_n_epoch: 1 |
| checkpoint_path: ./checkpoints |
| checkpoint_every_n_iters: 1 |
|
|
|
|
| And the config file for backtesting: |
|
|
| .. code-block:: yaml |
|
|
| order_file: ./data/backtest_orders.csv |
| start_time: "9:45" |
| end_time: "14:44" |
| qlib: |
| provider_uri_1min: ./data/bin |
| feature_root_dir: ./data/pickle |
| |
| feature_columns_today: [ |
| "$open", "$high", "$low", "$close", "$vwap", "$volume", |
| ] |
| |
| feature_columns_yesterday: [ |
| "$open_v1", "$high_v1", "$low_v1", "$close_v1", "$vwap_v1", "$volume_v1", |
| ] |
| exchange: |
| |
| limit_threshold: ['$close == 0', '$close == 0'] |
| |
| deal_price: ["If($close == 0, $vwap, $close)", "If($close == 0, $vwap, $close)"] |
| volume_threshold: |
| |
| all: ["cum", "0.2 * DayCumsum($volume, '9:45', '14:44')"] |
| |
| buy: ["current", "$close"] |
| |
| sell: ["current", "$close"] |
| strategies: |
| 30min: |
| class: TWAPStrategy |
| module_path: qlib.contrib.strategy.rule_strategy |
| kwargs: {} |
| 1day: |
| class: SAOEIntStrategy |
| module_path: qlib.rl.order_execution.strategy |
| kwargs: |
| state_interpreter: |
| class: FullHistoryStateInterpreter |
| module_path: qlib.rl.order_execution.interpreter |
| kwargs: |
| max_step: 8 |
| data_ticks: 240 |
| data_dim: 6 |
| processed_data_provider: |
| class: PickleProcessedDataProvider |
| module_path: qlib.rl.data.pickle_styled |
| kwargs: |
| data_dir: ./data/pickle_dataframe/feature |
| action_interpreter: |
| class: CategoricalActionInterpreter |
| module_path: qlib.rl.order_execution.interpreter |
| kwargs: |
| values: 14 |
| max_step: 8 |
| network: |
| class: Recurrent |
| module_path: qlib.rl.order_execution.network |
| kwargs: {} |
| policy: |
| class: PPO |
| module_path: qlib.rl.order_execution.policy |
| kwargs: |
| lr: 1.0e-4 |
| |
| weight_file: ./checkpoints/latest.pth |
| |
| concurrency: 5 |
|
|
| With the above config files, you can start training the agent by the following command: |
|
|
| .. code-block:: console |
|
|
| $ python -m qlib.rl.contrib.train_onpolicy.py --config_path train_config.yml |
|
|
| After the training, you can backtest with the following command: |
|
|
| .. code-block:: console |
|
|
| $ python -m qlib.rl.contrib.backtest.py --config_path backtest_config.yml |
|
|
| In that case, :class:`~qlib.rl.order_execution.simulator_qlib.SingleAssetOrderExecution` and :class:`~qlib.rl.order_execution.simulator_simple.SingleAssetOrderExecutionSimple` as examples for simulator, :class:`qlib.rl.order_execution.interpreter.FullHistoryStateInterpreter` and :class:`qlib.rl.order_execution.interpreter.CategoricalActionInterpreter` as examples for interpreter, :class:`qlib.rl.order_execution.policy.PPO` as an example for policy, and :class:`qlib.rl.order_execution.reward.PAPenaltyReward` as an example for reward. |
| For the single asset order execution task, if developers have already defined their simulator/interpreters/reward function/policy, they could launch the training and backtest pipeline by simply modifying the corresponding settings in the config files. |
| The details about the example can be found `here <https://github.com/microsoft/qlib/blob/main/examples/rl/README.md>`_. |
|
|
| In the future, we will provide more examples for different scenarios such as RL-based portfolio construction. |
|
|