From 7b9e338a0d86d9596a4449fbbd3095e5018afe1d Mon Sep 17 00:00:00 2001 From: "wangwenxi.handsome" Date: Tue, 13 Jul 2021 22:01:40 +0800 Subject: [PATCH] add docs --- qlib/backtest/__init__.py | 50 +++++++++++++++++++++++++++++++++++---- qlib/backtest/account.py | 2 +- qlib/backtest/backtest.py | 2 ++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/qlib/backtest/__init__.py b/qlib/backtest/__init__.py index a171ef81e..b4c3e32b8 100644 --- a/qlib/backtest/__init__.py +++ b/qlib/backtest/__init__.py @@ -117,13 +117,13 @@ def create_account_instance( For `Position`: Using Account with a Position """ - if(type(account) in (int, float)): + if isinstance(account, (int, float)): pos_kwargs = {"init_cash": account} - elif(type(account) is Position): + elif isinstance(account, Position): pos_kwargs = { "init_cash": account.position["cash"], "position_dict": account.position, - } + } else: raise ValueError("account must be in (int, float, Position)") @@ -146,7 +146,7 @@ def get_strategy_executor( strategy: BaseStrategy, executor: BaseExecutor, benchmark: str = "SH000300", - account: Union[float, str] = 1e9, + account: Union[float, int, Position] = 1e9, exchange_kwargs: dict = {}, pos_type: str = "Position", ): @@ -184,7 +184,41 @@ def backtest( exchange_kwargs={}, pos_type: str = "Position", ): + """initialize the strategy and executor, then backtest funciton for the interaction of the outermost strategy and executor in the nested decision execution + Parameters + ---------- + start_time : pd.Timestamp|str + closed start time for backtest + **NOTE**: This will be applied to the outmost executor's calendar. + end_time : pd.Timestamp|str + closed end time for backtest + **NOTE**: This will be applied to the outmost executor's calendar. + E.g. Executor[day](Executor[1min]), setting `end_time == 20XX0301` will include all the minutes on 20XX0301 + strategy : Union[str, dict, BaseStrategy] + for initializing outermost portfolio strategy. Please refer to the docs of init_instance_by_config for more information. + executor : Union[str, dict, BaseExecutor] + for initializing the outermost executor. + benchmark: str + the benchmark for reporting. + account : Union[float, int, Position] + information for describing how to creating the account + For `float` or `int`: + Using Account with only initial cash + For `Position`: + Using Account with a Position + exchange_kwargs : dict + the kwargs for initializing Exchange + pos_type : str + the type of Position. + + Returns + ------- + report_dict: Report + it records the trading report information + indicator_dict: Indicator + it computes the trading indicator + """ trade_strategy, trade_executor = get_strategy_executor( start_time, end_time, @@ -210,7 +244,15 @@ def collect_data( exchange_kwargs={}, pos_type: str = "Position", ): + """initialize the strategy and executor, then collect the trade decision data for rl training + please refer to the docs of the backtest for the explanation of the parameters + + Yields + ------- + object + trade decision + """ trade_strategy, trade_executor = get_strategy_executor( start_time, end_time, diff --git a/qlib/backtest/account.py b/qlib/backtest/account.py index fee0a98c4..806f88a96 100644 --- a/qlib/backtest/account.py +++ b/qlib/backtest/account.py @@ -93,7 +93,7 @@ class Account: "kwargs": { "cash": init_cash, "position_dict": position_dict, - }, + }, "module_path": "qlib.backtest.position", } ) diff --git a/qlib/backtest/backtest.py b/qlib/backtest/backtest.py index 89b8c7830..5c9948b1b 100644 --- a/qlib/backtest/backtest.py +++ b/qlib/backtest/backtest.py @@ -21,6 +21,8 @@ def backtest_loop(start_time, end_time, trade_strategy: BaseStrategy, trade_exec ------- report: Report it records the trading report information + indicator: Indicator + it computes the trading indicator """ return_value = {} for _decision in collect_data_loop(start_time, end_time, trade_strategy, trade_executor, return_value):