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:
+4
-92
@@ -1,7 +1,7 @@
|
||||
# Copyright (c) 2025-2026 马建军. All rights reserved.
|
||||
# 详见 LICENSE.zh-CN.txt
|
||||
|
||||
"""CTP 持仓均价:成交加权 / 柜台持仓价 / 盈亏一致校正。"""
|
||||
"""CTP 持仓均价:仅使用柜台持仓回报(vnpy pos.price = PositionCost 加权)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
@@ -45,74 +45,6 @@ def round_to_tick(price: float, sym: str) -> float:
|
||||
return round(round(price / tick) * tick, 4)
|
||||
|
||||
|
||||
def entry_from_ctp_pnl(
|
||||
ctp: dict[str, Any],
|
||||
tick: Optional[float],
|
||||
*,
|
||||
ths_sym: str = "",
|
||||
) -> Optional[float]:
|
||||
"""用柜台持仓盈亏 + 现价反推均价(与 SimNow 浮动盈亏一致)。"""
|
||||
if not tick or tick <= 0:
|
||||
return None
|
||||
lots = int(ctp.get("lots") or 0)
|
||||
if lots <= 0:
|
||||
return None
|
||||
pnl = float(ctp.get("pnl") or 0)
|
||||
if not pnl:
|
||||
return None
|
||||
sym = ths_sym or (ctp.get("symbol") or "")
|
||||
mult = float(get_contract_spec(_ths_code(sym)).get("mult") or 10)
|
||||
if mult <= 0:
|
||||
return None
|
||||
direction = (ctp.get("direction") or "long").strip().lower()
|
||||
if direction == "long":
|
||||
derived = tick - pnl / (mult * lots)
|
||||
else:
|
||||
derived = tick + pnl / (mult * lots)
|
||||
if derived <= 0:
|
||||
return None
|
||||
return round_to_tick(derived, sym)
|
||||
|
||||
|
||||
def avg_from_trades(
|
||||
trades: list[dict[str, Any]],
|
||||
sym: str,
|
||||
direction: str,
|
||||
*,
|
||||
expect_lots: int = 0,
|
||||
) -> Optional[float]:
|
||||
"""按成交回报移动加权均价(滚仓多笔开仓后应与柜台一致)。"""
|
||||
direction = (direction or "long").strip().lower()
|
||||
vol = 0
|
||||
cost = 0.0
|
||||
for t in sorted(trades, key=lambda x: (x.get("datetime") or "", x.get("trade_id") or "")):
|
||||
if not symbols_match(t.get("symbol") or "", sym):
|
||||
continue
|
||||
off = (t.get("offset") or "").strip().lower()
|
||||
pos_dir = (
|
||||
t.get("position_direction") or t.get("direction") or "long"
|
||||
).strip().lower()
|
||||
if pos_dir != direction:
|
||||
continue
|
||||
lots = int(t.get("lots") or 0)
|
||||
px = float(t.get("price") or 0)
|
||||
if lots <= 0 or px <= 0:
|
||||
continue
|
||||
if off == "open":
|
||||
cost += px * lots
|
||||
vol += lots
|
||||
elif off == "close" and vol > 0:
|
||||
avg = cost / vol
|
||||
dec = min(lots, vol)
|
||||
cost -= avg * dec
|
||||
vol -= dec
|
||||
if vol <= 0:
|
||||
return None
|
||||
if expect_lots > 0 and vol != expect_lots:
|
||||
return None
|
||||
return round_to_tick(cost / vol, sym)
|
||||
|
||||
|
||||
def resolve_ctp_entry(
|
||||
sym: str,
|
||||
direction: str,
|
||||
@@ -121,31 +53,11 @@ def resolve_ctp_entry(
|
||||
*,
|
||||
tick: Optional[float] = None,
|
||||
) -> tuple[float, str]:
|
||||
"""均价:成交加权 > 盈亏一致校正 > 柜台持仓价。"""
|
||||
"""均价:仅柜台持仓价(trades/tick 参数保留兼容,不参与计算)。"""
|
||||
del direction, trades, tick
|
||||
if not ctp:
|
||||
return 0.0, "none"
|
||||
direction = (direction or "long").strip().lower()
|
||||
lots = int(ctp.get("lots") or 0)
|
||||
|
||||
if trades:
|
||||
trade_avg = avg_from_trades(trades, sym, direction, expect_lots=lots)
|
||||
if trade_avg and trade_avg > 0:
|
||||
return float(trade_avg), "trades"
|
||||
|
||||
pos_avg = float(ctp.get("avg_price") or 0)
|
||||
if pos_avg > 0:
|
||||
pos_avg = round_to_tick(pos_avg, sym)
|
||||
|
||||
pnl_avg = entry_from_ctp_pnl(ctp, tick, ths_sym=sym)
|
||||
tick_sz = float(get_contract_spec(_ths_code(sym)).get("tick_size") or 1.0)
|
||||
|
||||
if pnl_avg and pos_avg > 0:
|
||||
if abs(pnl_avg - pos_avg) >= max(tick_sz * 0.5, 0.01):
|
||||
return float(pnl_avg), "pnl"
|
||||
return pos_avg, "ctp"
|
||||
|
||||
if pos_avg > 0:
|
||||
return pos_avg, "ctp"
|
||||
if pnl_avg and pnl_avg > 0:
|
||||
return float(pnl_avg), "pnl"
|
||||
return round_to_tick(pos_avg, sym), "ctp"
|
||||
return 0.0, "none"
|
||||
|
||||
Reference in New Issue
Block a user