Add stats trading calendar and fix CTP position avg/sync.

Calendar shows daily closed trade count and PnL with emotion-day highlighting; day click loads review-first trade list. Use exchange-only entry average and improve vnpy position sync after CTP reconnect.
This commit is contained in:
dekun
2026-06-30 11:59:25 +08:00
parent d07fc4b70d
commit 8ebad6e8a2
8 changed files with 926 additions and 198 deletions
+12 -67
View File
@@ -42,49 +42,27 @@ def reconcile_position_avg(
trades: Optional[list[dict[str, Any]]] = None,
ths_sym: str = "",
) -> dict[str, Any]:
"""手数不变时锁定均价;滚仓/加仓(手数变化)时以柜台加权均价为准"""
from ctp_entry_price import entry_from_ctp_pnl, resolve_ctp_entry
"""手数变化时采用柜台回报均价;手数不变时保持已锁定柜台价"""
del tick, trades
from ctp_entry_price import round_to_tick
row = dict(new)
lots = int(row.get("lots") or 0)
if lots <= 0:
return row
direction = (row.get("direction") or "long").strip().lower()
old_lots = int(old.get("lots") or 0) if old else 0
lots_changed = not old or old_lots != lots
sym = ths_sym or (row.get("symbol") or "")
if (
not lots_changed
and old
and old.get("avg_price_locked")
and float(old.get("avg_price") or 0) > 0
):
locked = float(old["avg_price"])
corrected, _ = resolve_ctp_entry(sym, direction, row, trades, tick=tick)
pnl_entry = entry_from_ctp_pnl(row, tick, ths_sym=sym)
if corrected > 0 and abs(corrected - locked) >= 0.5:
row["avg_price"] = corrected
row["avg_price_locked"] = True
return row
if pnl_entry and abs(pnl_entry - locked) >= 0.5:
row["avg_price"] = pnl_entry
row["avg_price_locked"] = True
return row
row["avg_price"] = locked
row["avg_price_locked"] = True
return row
entry, _src = resolve_ctp_entry(sym, direction, row, trades, tick=tick)
if entry > 0:
row["avg_price"] = entry
row["avg_price_locked"] = True
return row
pos_avg = float(row.get("avg_price") or 0)
if pos_avg > 0:
row["avg_price"] = pos_avg
row["avg_price_locked"] = lots_changed or bool(tick)
row["avg_price"] = round_to_tick(pos_avg, sym)
row["avg_price_locked"] = True
return row
if not lots_changed and old and float(old.get("avg_price") or 0) > 0:
row["avg_price"] = float(old["avg_price"])
row["avg_price_locked"] = True
return row
@@ -174,41 +152,8 @@ class CtpTradingState:
return dict(row) if row else None
def try_lock_entry_prices(self) -> bool:
"""有 tick 后校正持仓均价(含已锁定但与柜台盈亏不一致的)。"""
from ctp_entry_price import resolve_ctp_entry
changed = False
with self._lock:
for pk, row in list(self._positions.items()):
ex = row.get("exchange") or ""
sym = row.get("symbol") or ""
tick = self.get_tick_price(ex, sym)
if not tick or tick <= 0:
continue
ths = sym
try:
from vnpy_bridge import CtpBridge
ths = CtpBridge._vnpy_sym_to_ths(sym, ex) or sym
except Exception:
pass
entry, _ = resolve_ctp_entry(
ths,
row.get("direction") or "long",
row,
tick=tick,
)
if not entry or entry <= 0:
continue
current = float(row.get("avg_price") or 0)
if row.get("avg_price_locked") and current > 0:
if abs(entry - current) < 0.5:
continue
updated = dict(row)
updated["avg_price"] = entry
updated["avg_price_locked"] = True
self._positions[pk] = updated
changed = True
return changed
"""均价以柜台为准,不按 tick 反推(避免均价随行情跳动)。"""
return False
def upsert_position(
self,