Skip to content

Commit

Permalink
config.mod新增strategy_name字段
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Dec 1, 2023
1 parent 74ce989 commit a453586
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_analyser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"benchmark": None,
# 当不输出 csv/pickle/plot 等内容时,关闭该项可关闭策略运行过程中部分收集数据的逻辑,用以提升性能
"record": True,
# 策略名称,可在summary、回测报告,收益图中展示
"strategy_name": None,
# 回测结果输出的文件路径,该文件为 pickle 格式,内容为每日净值、头寸、流水及风险指标等;若不设置则不输出该文件
"output_file": None,
# 回测报告的数据目录,报告为 csv 格式;若不设置则不输出报告
Expand Down
7 changes: 5 additions & 2 deletions rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ def tear_down(self, code, exception=None):
if len(self._total_portfolios) == 0:
return

strategy_name = os.path.basename(self._env.config.base.strategy_file).split(".")[0]
if self._mod_config.strategy_name:
strategy_name = self._mod_config.strategy_name
else:
strategy_name = os.path.basename(self._env.config.base.strategy_file).split(".")[0]
data_proxy = self._env.data_proxy
start_date, end_date = attrgetter("start_date", "end_date")(self._env.config.base)
summary = {
Expand Down Expand Up @@ -536,7 +539,7 @@ def tear_down(self, code, exception=None):
_plot_template_cls = PLOT_TEMPLATE.get(self._mod_config.plot, DefaultPlot)
plot_result(
result_dict, self._mod_config.plot, self._mod_config.plot_save_file,
plot_config.weekly_indicators, plot_config.open_close_points, _plot_template_cls
plot_config.weekly_indicators, plot_config.open_close_points, _plot_template_cls, self._mod_config.strategy_name
)

return result_dict
3 changes: 3 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
LABEL_FONT_SIZE = 11
SUPPORT_CHINESE = True

TITLE_FONT_SIZE = 16

RED = "#aa4643"
BLUE = "#4572a7"
YELLOW = "#F3A423"
Expand All @@ -50,6 +52,7 @@
IMG_WIDTH = 15

# 两部分的相对高度
PLOT_TITLE_HEIGHT = 1
INDICATOR_AREA_HEIGHT = 3
PLOT_AREA_HEIGHT = 5
USER_PLOT_AREA_HEIGHT = 2
Expand Down
19 changes: 16 additions & 3 deletions rqalpha/mod/rqalpha_mod_sys_analyser/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from .utils import IndicatorInfo, LineInfo, max_dd as _max_dd, SpotInfo, max_ddd as _max_ddd
from .utils import weekly_returns, trading_dates_index
from .consts import PlotTemplate, DefaultPlot
from .consts import IMG_WIDTH, INDICATOR_AREA_HEIGHT, PLOT_AREA_HEIGHT, USER_PLOT_AREA_HEIGHT
from .consts import LABEL_FONT_SIZE, BLACK, SUPPORT_CHINESE
from .consts import IMG_WIDTH, INDICATOR_AREA_HEIGHT, PLOT_AREA_HEIGHT, USER_PLOT_AREA_HEIGHT, PLOT_TITLE_HEIGHT
from .consts import LABEL_FONT_SIZE, BLACK, SUPPORT_CHINESE, TITLE_FONT_SIZE
from .consts import MAX_DD, MAX_DDD, OPEN_POINT, CLOSE_POINT
from .consts import LINE_BENCHMARK, LINE_STRATEGY, LINE_WEEKLY_BENCHMARK, LINE_WEEKLY, LINE_EXCESS

Expand Down Expand Up @@ -130,6 +130,16 @@ def plot(self, ax: Axes):
pyplot.legend(loc="best").get_frame().set_alpha(0.5)


class TitlePlot(SubPlot):
height: int = PLOT_TITLE_HEIGHT

def __init__(self, strategy_name):
self._strategy_name = strategy_name

def plot(self, ax:Axes):
ax.axis("off")
ax.text(0.5, 0.5, self._strategy_name, ha='center', va='top', color=BLACK, fontsize=TITLE_FONT_SIZE)

class WaterMark:
def __init__(self, img_width, img_height):
logo_file = os.path.join(
Expand Down Expand Up @@ -167,7 +177,7 @@ def _plot(title: str, sub_plots: List[SubPlot]):

def plot_result(
result_dict, show=True, save=None, weekly_indicators: bool = False, open_close_points: bool = False,
plot_template_cls=DefaultPlot
plot_template_cls=DefaultPlot, strategy_name=None
):
summary = result_dict["summary"]
portfolio = result_dict["portfolio"]
Expand Down Expand Up @@ -222,6 +232,9 @@ def plot_result(
if "plots" in result_dict:
sub_plots.append(UserPlot(result_dict["plots"]))

if strategy_name:
sub_plots.insert(0, TitlePlot(strategy_name))

_plot(summary["strategy_file"], sub_plots)

if save:
Expand Down

0 comments on commit a453586

Please sign in to comment.