mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-16 09:11:00 +08:00
black format
This commit is contained in:
@@ -16,6 +16,7 @@ from ...config import C
|
|||||||
|
|
||||||
logger = get_module_logger("backtest caller")
|
logger = get_module_logger("backtest caller")
|
||||||
|
|
||||||
|
|
||||||
def get_strategy(
|
def get_strategy(
|
||||||
strategy=None,
|
strategy=None,
|
||||||
topk=50,
|
topk=50,
|
||||||
@@ -61,7 +62,6 @@ def get_strategy(
|
|||||||
an initialized strategy object
|
an initialized strategy object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
# There will be 3 ways to return a strategy.
|
# There will be 3 ways to return a strategy.
|
||||||
if strategy is None:
|
if strategy is None:
|
||||||
# 1) create strategy with param `strategy`
|
# 1) create strategy with param `strategy`
|
||||||
@@ -72,6 +72,7 @@ def get_strategy(
|
|||||||
}
|
}
|
||||||
logger.info("Create new strategy ")
|
logger.info("Create new strategy ")
|
||||||
from .. import strategy as strategy_pool
|
from .. import strategy as strategy_pool
|
||||||
|
|
||||||
str_cls = getattr(strategy_pool, str_cls_dict.get(str_type))
|
str_cls = getattr(strategy_pool, str_cls_dict.get(str_type))
|
||||||
strategy = str_cls(
|
strategy = str_cls(
|
||||||
topk=topk,
|
topk=topk,
|
||||||
@@ -86,6 +87,7 @@ def get_strategy(
|
|||||||
strategy = init_instance_by_config(strategy)
|
strategy = init_instance_by_config(strategy)
|
||||||
|
|
||||||
from ..strategy.strategy import BaseStrategy
|
from ..strategy.strategy import BaseStrategy
|
||||||
|
|
||||||
# else: nothing happens. 3) Use the strategy directly
|
# else: nothing happens. 3) Use the strategy directly
|
||||||
if not isinstance(strategy, BaseStrategy):
|
if not isinstance(strategy, BaseStrategy):
|
||||||
raise TypeError("Strategy not supported")
|
raise TypeError("Strategy not supported")
|
||||||
@@ -193,12 +195,13 @@ def get_executor(
|
|||||||
:class: BaseExecutor
|
:class: BaseExecutor
|
||||||
an initialized BaseExecutor object
|
an initialized BaseExecutor object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# There will be 3 ways to return a executor.
|
# There will be 3 ways to return a executor.
|
||||||
if executor is None:
|
if executor is None:
|
||||||
# 1) create executor with param `executor`
|
# 1) create executor with param `executor`
|
||||||
logger.info("Create new executor ")
|
logger.info("Create new executor ")
|
||||||
from ..online.executor import SimulatorExecutor
|
from ..online.executor import SimulatorExecutor
|
||||||
|
|
||||||
executor = SimulatorExecutor(trade_exchange=trade_exchange, verbose=verbose)
|
executor = SimulatorExecutor(trade_exchange=trade_exchange, verbose=verbose)
|
||||||
elif isinstance(executor, (dict, str)):
|
elif isinstance(executor, (dict, str)):
|
||||||
# 2) create executor with config
|
# 2) create executor with config
|
||||||
@@ -206,11 +209,13 @@ def get_executor(
|
|||||||
executor = init_instance_by_config(executor)
|
executor = init_instance_by_config(executor)
|
||||||
|
|
||||||
from ..online.executor import BaseExecutor
|
from ..online.executor import BaseExecutor
|
||||||
|
|
||||||
# 3) Use the executor directly
|
# 3) Use the executor directly
|
||||||
if not isinstance(executor, BaseExecutor):
|
if not isinstance(executor, BaseExecutor):
|
||||||
raise TypeError("Executor not supported")
|
raise TypeError("Executor not supported")
|
||||||
return executor
|
return executor
|
||||||
|
|
||||||
|
|
||||||
# This is the API for compatibility for legacy code
|
# This is the API for compatibility for legacy code
|
||||||
def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, return_order=False, **kwargs):
|
def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, return_order=False, **kwargs):
|
||||||
"""This function will help you set a reasonable Exchange and provide default value for strategy
|
"""This function will help you set a reasonable Exchange and provide default value for strategy
|
||||||
@@ -316,4 +321,4 @@ def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, ret
|
|||||||
|
|
||||||
positions = report_dict.get("positions")
|
positions = report_dict.get("positions")
|
||||||
report_dict.update({"positions": {k: p.position for k, p in positions.items()}})
|
report_dict.update({"positions": {k: p.position for k, p in positions.items()}})
|
||||||
return report_dict
|
return report_dict
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ def risk_analysis(r, N=252):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# This is the API for compatibility for legacy code
|
# This is the API for compatibility for legacy code
|
||||||
def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, **kwargs):
|
def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, **kwargs):
|
||||||
"""This function will help you set a reasonable Exchange and provide default value for strategy
|
"""This function will help you set a reasonable Exchange and provide default value for strategy
|
||||||
@@ -120,8 +119,12 @@ def backtest(pred, account=1e9, shift=1, benchmark="SH000905", verbose=True, **k
|
|||||||
whether to print log.
|
whether to print log.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
warnings.warn("this function is deprecated, please use backtest function in qlib.contrib.backtest", DeprecationWarning)
|
warnings.warn(
|
||||||
report_dict = backtest_func(pred=pred, account=account, shift=shift, benchmark=benchmark, verbose=verbose, return_order=False, **kwargs)
|
"this function is deprecated, please use backtest function in qlib.contrib.backtest", DeprecationWarning
|
||||||
|
)
|
||||||
|
report_dict = backtest_func(
|
||||||
|
pred=pred, account=account, shift=shift, benchmark=benchmark, verbose=verbose, return_order=False, **kwargs
|
||||||
|
)
|
||||||
return report_dict.get("report_df"), report_dict.get("positions")
|
return report_dict.get("report_df"), report_dict.get("positions")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user