Show today win rate on daily stats bar.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -568,7 +568,7 @@ class Mt5Bridge:
|
||||
return buy, sell, profit
|
||||
|
||||
def today_stats(self, symbol: str) -> dict:
|
||||
"""今日开仓次数、已平盈亏、浮盈、盈亏比、盈利率等。"""
|
||||
"""今日开仓次数、已平盈亏、浮盈、盈亏比、盈利率、胜率等(仅当天)。"""
|
||||
self.ensure_symbol(symbol)
|
||||
now = datetime.now()
|
||||
day0 = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
@@ -577,6 +577,8 @@ class Mt5Bridge:
|
||||
closed_pnl = 0.0
|
||||
win_sum = 0.0
|
||||
loss_sum = 0.0 # 绝对值累加
|
||||
# 按持仓单号汇总当日平仓盈亏,用于胜率(部分平仓合并为一笔)
|
||||
pos_pnl: dict = {}
|
||||
if deals:
|
||||
for d in deals:
|
||||
if d.symbol != symbol:
|
||||
@@ -590,6 +592,8 @@ class Mt5Bridge:
|
||||
pnl = float(d.profit) + float(d.swap) + float(d.commission)
|
||||
if entry in (mt5.DEAL_ENTRY_OUT, mt5.DEAL_ENTRY_INOUT, mt5.DEAL_ENTRY_OUT_BY):
|
||||
closed_pnl += pnl
|
||||
pid = int(getattr(d, "position_id", 0) or 0) or int(d.ticket)
|
||||
pos_pnl[pid] = pos_pnl.get(pid, 0.0) + pnl
|
||||
if pnl > 1e-8:
|
||||
win_sum += pnl
|
||||
elif pnl < -1e-8:
|
||||
@@ -598,6 +602,14 @@ class Mt5Bridge:
|
||||
closed_pnl += float(d.commission)
|
||||
if float(d.commission) < -1e-8:
|
||||
loss_sum += -float(d.commission)
|
||||
wins = sum(1 for p in pos_pnl.values() if p > 1e-8)
|
||||
losses = sum(1 for p in pos_pnl.values() if p < -1e-8)
|
||||
closed_n = len(pos_pnl)
|
||||
if closed_n > 0:
|
||||
win_rate = wins / closed_n
|
||||
win_rate_txt = f"{win_rate * 100:.0f}%({wins}胜{losses}负)"
|
||||
else:
|
||||
win_rate_txt = "--"
|
||||
_, _, floating = self.exposure(symbol)
|
||||
total = closed_pnl + floating
|
||||
balance = self.account_balance()
|
||||
@@ -617,6 +629,10 @@ class Mt5Bridge:
|
||||
"balance": balance,
|
||||
"profit_rate": profit_rate,
|
||||
"pl_ratio_txt": pl_ratio_txt,
|
||||
"win_rate_txt": win_rate_txt,
|
||||
"wins": wins,
|
||||
"losses": losses,
|
||||
"closed_trades": closed_n,
|
||||
}
|
||||
|
||||
def check_day_trade_lock(self, symbol: str) -> None:
|
||||
@@ -1074,7 +1090,7 @@ class OrderApp(tk.Tk):
|
||||
)
|
||||
self.lbl_today_total.pack(side=tk.LEFT)
|
||||
self.lbl_today_ratio = tk.Label(
|
||||
today_row, text=" 盈亏比 -- 盈利率 --", bg=COLOR_CARD, fg=COLOR_MUTED,
|
||||
today_row, text=" 盈亏比 -- 盈利率 -- 胜率 --", bg=COLOR_CARD, fg=COLOR_MUTED,
|
||||
font=("Microsoft YaHei UI", 11, "bold"),
|
||||
)
|
||||
self.lbl_today_ratio.pack(side=tk.LEFT)
|
||||
@@ -1839,7 +1855,7 @@ class OrderApp(tk.Tk):
|
||||
self.lbl_today_closed.configure(text=" 已平 --", fg=COLOR_NEUTRAL)
|
||||
self.lbl_today_mid.configure(text=" 浮盈 -- 合计 ")
|
||||
self.lbl_today_total.configure(text="--", fg=COLOR_NEUTRAL)
|
||||
self.lbl_today_ratio.configure(text=" 盈亏比 -- 盈利率 --", fg=COLOR_NEUTRAL)
|
||||
self.lbl_today_ratio.configure(text=" 盈亏比 -- 盈利率 -- 胜率 --", fg=COLOR_NEUTRAL)
|
||||
|
||||
self._refresh_positions(symbol, digits)
|
||||
self._sync_open_buttons(symbol)
|
||||
@@ -1895,7 +1911,10 @@ class OrderApp(tk.Tk):
|
||||
self.lbl_today_mid.configure(text=f" 浮盈 {floating:+.2f} 合计 ")
|
||||
self.lbl_today_total.configure(text=f"{total:+.2f}", fg=self._pnl_color(total))
|
||||
self.lbl_today_ratio.configure(
|
||||
text=f" | 盈亏比 {st['pl_ratio_txt']} | 盈利率 {rate:+.2f}%",
|
||||
text=(
|
||||
f" | 盈亏比 {st['pl_ratio_txt']} | 盈利率 {rate:+.2f}%"
|
||||
f" | 胜率 {st['win_rate_txt']}"
|
||||
),
|
||||
fg=self._pnl_color(total),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user