mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-11 06:46:56 +08:00
fix comments & add VAStrategy & add trade indicator
This commit is contained in:
@@ -17,7 +17,7 @@ from ..log import get_module_logger
|
||||
from ..utils import flatten_dict
|
||||
from ..utils.resam import parse_freq
|
||||
from ..strategy.base import BaseStrategy
|
||||
from ..contrib.eva.alpha import calc_ic, calc_long_short_return
|
||||
from ..contrib.eva.alpha import calc_ic, calc_long_short_return, calc_long_short_prec
|
||||
|
||||
|
||||
logger = get_module_logger("workflow", logging.INFO)
|
||||
@@ -302,7 +302,7 @@ class PortAnaRecord(RecordTemp):
|
||||
define the executor class as well as the kwargs.
|
||||
config["backtest"] : dict
|
||||
define the backtest kwargs.
|
||||
risk_analysis_freq : int
|
||||
risk_analysis_freq : str|List[str]
|
||||
risk analysis freq of report
|
||||
"""
|
||||
super().__init__(recorder=recorder, **kwargs)
|
||||
@@ -310,8 +310,11 @@ class PortAnaRecord(RecordTemp):
|
||||
self.strategy_config = config["strategy"]
|
||||
self.executor_config = config["executor"]
|
||||
self.backtest_config = config["backtest"]
|
||||
_count, _freq = parse_freq(risk_analysis_freq)
|
||||
self.risk_analysis_freq = f"{_count}{_freq}"
|
||||
if isinstance(risk_analysis_freq, str):
|
||||
risk_analysis_freq = [risk_analysis_freq]
|
||||
self.risk_analysis_freq = [
|
||||
"{0}{1}".format(*parse_freq(_analysis_freq)) for _analysis_freq in risk_analysis_freq
|
||||
]
|
||||
self.report_freq = self._get_report_freq(self.executor_config)
|
||||
|
||||
def _get_report_freq(self, executor_config):
|
||||
@@ -336,34 +339,35 @@ class PortAnaRecord(RecordTemp):
|
||||
**{f"positions_normal_{report_freq}.pkl": positions_normal}, artifact_path=PortAnaRecord.get_path()
|
||||
)
|
||||
|
||||
if self.risk_analysis_freq not in report_dict:
|
||||
warnings.warn(
|
||||
f"the freq {self.risk_analysis_freq} report is not found, please set the corresponding env with `generate_report==True`"
|
||||
)
|
||||
else:
|
||||
report_normal, _ = report_dict.get(self.risk_analysis_freq)
|
||||
analysis = dict()
|
||||
analysis["excess_return_without_cost"] = risk_analysis(
|
||||
report_normal["return"] - report_normal["bench"], freq=self.risk_analysis_freq
|
||||
)
|
||||
analysis["excess_return_with_cost"] = risk_analysis(
|
||||
report_normal["return"] - report_normal["bench"] - report_normal["cost"], freq=self.risk_analysis_freq
|
||||
)
|
||||
analysis_df = pd.concat(analysis) # type: pd.DataFrame
|
||||
# log metrics
|
||||
self.recorder.log_metrics(**flatten_dict(analysis_df["risk"].unstack().T.to_dict()))
|
||||
# save results
|
||||
self.recorder.save_objects(
|
||||
**{f"port_analysis_{report_freq}.pkl": analysis_df}, artifact_path=PortAnaRecord.get_path()
|
||||
)
|
||||
logger.info(
|
||||
f"Portfolio analysis record 'port_analysis_{report_freq}.pkl' has been saved as the artifact of the Experiment {self.recorder.experiment_id}"
|
||||
)
|
||||
# print out results
|
||||
pprint("The following are analysis results of the excess return without cost.")
|
||||
pprint(analysis["excess_return_without_cost"])
|
||||
pprint("The following are analysis results of the excess return with cost.")
|
||||
pprint(analysis["excess_return_with_cost"])
|
||||
for _analysis_freq in self.risk_analysis_freq:
|
||||
if _analysis_freq not in report_dict:
|
||||
warnings.warn(
|
||||
f"the freq {_analysis_freq} report is not found, please set the corresponding env with `generate_report==True`"
|
||||
)
|
||||
else:
|
||||
report_normal, _ = report_dict.get(_analysis_freq)
|
||||
analysis = dict()
|
||||
analysis["excess_return_without_cost"] = risk_analysis(
|
||||
report_normal["return"] - report_normal["bench"], freq=_analysis_freq
|
||||
)
|
||||
analysis["excess_return_with_cost"] = risk_analysis(
|
||||
report_normal["return"] - report_normal["bench"] - report_normal["cost"], freq=_analysis_freq
|
||||
)
|
||||
analysis_df = pd.concat(analysis) # type: pd.DataFrame
|
||||
# log metrics
|
||||
self.recorder.log_metrics(**flatten_dict(analysis_df["risk"].unstack().T.to_dict()))
|
||||
# save results
|
||||
self.recorder.save_objects(
|
||||
**{f"port_analysis_{report_freq}.pkl": analysis_df}, artifact_path=PortAnaRecord.get_path()
|
||||
)
|
||||
logger.info(
|
||||
f"Portfolio analysis record 'port_analysis_{report_freq}.pkl' has been saved as the artifact of the Experiment {self.recorder.experiment_id}"
|
||||
)
|
||||
# print out results
|
||||
pprint("The following are analysis results of the excess return without cost.")
|
||||
pprint(analysis["excess_return_without_cost"])
|
||||
pprint("The following are analysis results of the excess return with cost.")
|
||||
pprint(analysis["excess_return_with_cost"])
|
||||
|
||||
def list(self):
|
||||
list_path = []
|
||||
@@ -374,6 +378,10 @@ class PortAnaRecord(RecordTemp):
|
||||
PortAnaRecord.get_path(f"positions_normal_{_freq}.pkl"),
|
||||
]
|
||||
)
|
||||
if _freq == self.risk_analysis_freq:
|
||||
list_path.append(PortAnaRecord.get_path(f"port_analysis_{_freq}.pkl"))
|
||||
|
||||
for _analysis_freq in self.risk_analysis_freq:
|
||||
if _analysis_freq in self.report_freq:
|
||||
list_path.append(PortAnaRecord.get_path(f"port_analysis_{_analysis_freq}.pkl"))
|
||||
else:
|
||||
warnings.warn(f"{_analysis_freq} is not found")
|
||||
return list_path
|
||||
|
||||
Reference in New Issue
Block a user