1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-01 18:11:18 +08:00
This commit is contained in:
wangwenxi.handsome
2021-07-13 22:01:40 +08:00
committed by you-n-g
parent 0646e53d24
commit 7b9e338a0d
3 changed files with 49 additions and 5 deletions

View File

@@ -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,

View File

@@ -93,7 +93,7 @@ class Account:
"kwargs": {
"cash": init_cash,
"position_dict": position_dict,
},
},
"module_path": "qlib.backtest.position",
}
)

View File

@@ -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):