Skip to content

Commit

Permalink
分析指标新增胜率和盈亏比
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Aug 25, 2022
1 parent fc28c5c commit b827969
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
13 changes: 13 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_analyser/mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(self):
self._total_benchmark_portfolios = []
self._sub_accounts = defaultdict(list)
self._positions = defaultdict(list)
self._daily_pnl = []

self._benchmark_daily_returns = []
self._portfolio_daily_returns = []
Expand Down Expand Up @@ -150,6 +151,7 @@ def _collect_daily(self, _):
"date": date,
"unit_net_value": (np.array(self._benchmark_daily_returns) + 1).prod()
})
self._daily_pnl.append(portfolio.daily_pnl)

for account_type, account in self._env.portfolio.accounts.items():
self._sub_accounts[account_type].append(self._to_account_record(date, account))
Expand Down Expand Up @@ -328,6 +330,16 @@ def tear_down(self, code, exception=None):
'excess_returns': risk.excess_return_rate,
'excess_annual_returns': risk.excess_annual_return,
'var': risk.var,
"win_rate": risk.win_rate,
})

# 盈亏比
daily_pnl = np.array(self._daily_pnl)
profit, loss = daily_pnl[daily_pnl > 0], daily_pnl[daily_pnl < 0]
profit, loss = profit.mean() if len(profit) else 0, loss.mean() if len(loss) else 0

summary.update({
"profit_loss_rate": np.abs(profit / loss) if loss else np.nan
})

summary.update({
Expand Down Expand Up @@ -412,6 +424,7 @@ def tear_down(self, code, exception=None):
'weekly_information_ratio': weekly_risk.information_ratio,
"weekly_tracking_error": weekly_risk.tracking_error,
"weekly_max_drawdown": weekly_risk.max_drawdown,
"weekly_win_rate": weekly_risk.win_rate,
})

plots = self._plot_store.get_plots()
Expand Down
15 changes: 9 additions & 6 deletions rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
USER_PLOT_AREA_HEIGHT = 2

# 指标的宽高
INDICATOR_WIDTH = 0.15
INDICATOR_WIDTH = 0.18
INDICATOR_VALUE_HEIGHT = 0.15
INDICATOR_LABEL_HEIGHT = 0.2

Expand All @@ -74,14 +74,19 @@
IndicatorInfo("beta", _("Beta"), BLACK, "{0:.4}", 11),
IndicatorInfo("sharpe", _("Sharpe"), BLACK, "{0:.4}", 11),
IndicatorInfo("sortino", _("Sortino"), BLACK, "{0:.4}", 11),
IndicatorInfo("information_ratio", _("InformationRatio"), BLACK, "{0:.4}", 11),
], [
IndicatorInfo("benchmark_total_returns", _("BenchmarkReturns"), BLUE, "{0:.3%}", 11),
IndicatorInfo("benchmark_annualized_returns", _("BenchmarkAnnual"), BLUE, "{0:.3%}", 11),
IndicatorInfo("volatility", _("Volatility"), BLACK, "{0:.4}", 11),
IndicatorInfo("max_drawdown", _("MaxDrawDown"), BLACK, "{0:.4}", 11),
IndicatorInfo("tracking_error", _("TrackingError"), BLACK, "{0:.4}", 11),
IndicatorInfo("downside_risk", _("DownsideRisk"), BLACK, "{0:.4}", 11),
IndicatorInfo("information_ratio", _("InformationRatio"), BLACK, "{0:.4}", 11),
], [
IndicatorInfo("max_drawdown", _("MaxDrawDown"), BLACK, "{0:.4}", 11),
IndicatorInfo("weekly_max_drawdown", _("WeeklyMaxDrawdown"), BLACK, "{0:.4}", 11),
IndicatorInfo("win_rate", _("WinRate"), BLACK, "{0:.4}", 11),
IndicatorInfo("weekly_win_rate", _("WeeklyWinRate"), BLACK, "{0:.4}", 11),
IndicatorInfo("profit_loss_rate", _("ProfitLossRate"), BLACK, "{0:.4}", 11),
IndicatorInfo("max_dd_ddd", _("MaxDD/MaxDDD"), BLACK, "{}", 6),
]]

Expand All @@ -92,7 +97,6 @@
IndicatorInfo("weekly_sortino", _("WeeklySortino"), BLACK, "{0:.4}", 11),
IndicatorInfo("weekly_information_ratio", _("WeeklyInfoRatio"), BLACK, "{0:.4}", 11),
IndicatorInfo("weekly_tracking_error", _("WeeklyTrackingError"), BLACK, "{0:.4}", 11),
IndicatorInfo("weekly_max_drawdown", _("WeeklyMaxDrawdown"), BLACK, "{0:.4}", 11),
]

EXCESS_INDICATORS = [
Expand All @@ -101,7 +105,6 @@
IndicatorInfo("excess_annual_returns", _("ExcessAnnual"), BLACK, "{0:.3%}", 11),
IndicatorInfo("excess_sharpe", _("ExcessSharpe"), BLACK, "{0:.4}", 11),
IndicatorInfo("excess_volatility", _("ExcessVolatility"), BLACK, "{0:.4}", 11),
# IndicatorInfo("excess_annual_volatility", _("ExcessAnnualVolatility"), BLACK, "{0:.4}", 11),
IndicatorInfo("excess_max_drawdown", _("ExcessMaxDD"), BLACK, "{0:.4}", 11),
# IndicatorInfo("excess_max_drawdown", _("ExcessMaxDD"), BLACK, "{0:.4}", 11),
IndicatorInfo("excess_max_dd_ddd", _("ExcessMaxDD/ExcessMaxDDD"), BLACK, "{}", 6),
]
Binary file modified rqalpha/mod/rqalpha_mod_sys_analyser/report/templates/summary.xlsx
Binary file not shown.
Binary file modified rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.mo
Binary file not shown.
68 changes: 40 additions & 28 deletions rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ msgid "BenchmarkWeekly"
msgstr "基准周度收益"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:65
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:82
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:85
msgid "MaxDrawDown"
msgstr "最大回撤"

Expand Down Expand Up @@ -487,87 +487,99 @@ msgstr "夏普率"
msgid "Sortino"
msgstr "索提诺比率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:77
msgid "InformationRatio"
msgstr "信息比率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:79
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:78
msgid "BenchmarkReturns"
msgstr "基准收益率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:80
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:79
msgid "BenchmarkAnnual"
msgstr "基准年化收益率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:81
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:80
msgid "Volatility"
msgstr "波动率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:83
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:81
msgid "TrackingError"
msgstr "跟踪误差"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:84
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:82
msgid "DownsideRisk"
msgstr "下行风险"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:85
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:83
msgid "InformationRatio"
msgstr "信息比率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:86
msgid "WeeklyMaxDrawdown"
msgstr "周度最大回撤"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:87
msgid "WinRate"
msgstr "胜率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:88
msgid "WeeklyWinRate"
msgstr "周度胜率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:89
msgid "ProfitLossRate"
msgstr "盈亏比"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:90
msgid "MaxDD/MaxDDD"
msgstr "最大回撤/最长回撤持续期"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:89
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:91
msgid "WeeklyAlpha"
msgstr "周度阿尔法"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:90
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:92
msgid "WeeklyBeta"
msgstr "周度贝塔"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:91
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:93
msgid "WeeklySharpe"
msgstr "周度夏普率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:92
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:94
msgid "WeeklySortino"
msgstr "周度索提诺比率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:93
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:95
msgid "WeeklyInfoRatio"
msgstr "周度信息比率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:94
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:96
msgid "WeeklyTrackingError"
msgstr "周度跟踪误差"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:95
msgid "WeeklyMaxDrawdown"
msgstr "周度最大回撤"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:99
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:103
msgid "ExcessCumReturns"
msgstr "超额累计收益"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:100
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:104
msgid "ExcessReturns"
msgstr "超额收益率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:101
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:105
msgid "ExcessAnnual"
msgstr "年化超额收益率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:102
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:106
msgid "ExcessSharpe"
msgstr "超额夏普率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:103
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:107
msgid "ExcessVolatility"
msgstr "超额收益波动率"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:105
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:108
msgid "ExcessMaxDD"
msgstr "超额收益最大回撤"

#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:106
#: rqalpha/mod/rqalpha_mod_sys_analyser/plot/consts.py:109
msgid "ExcessMaxDD/ExcessMaxDDD"
msgstr "超额收益最大回撤/最长回撤持续期"

Expand Down

0 comments on commit b827969

Please sign in to comment.