Prefer CTP PnL-consistent entry when vnpy avg differs from SimNow.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+40
-54
@@ -34,43 +34,6 @@ def parse_position_key(key: str) -> tuple[str, str, str]:
|
||||
return "", (key or "").lower(), "long"
|
||||
|
||||
|
||||
def avg_price_from_ctp_pnl(
|
||||
row: dict[str, Any],
|
||||
tick: Optional[float],
|
||||
) -> Optional[float]:
|
||||
"""用柜台持仓盈亏 + 现价快照反推开仓均价(与 SimNow 浮动盈亏一致)。"""
|
||||
if not tick or tick <= 0:
|
||||
return None
|
||||
lots = int(row.get("lots") or 0)
|
||||
if lots <= 0:
|
||||
return None
|
||||
pnl = float(row.get("pnl") or 0)
|
||||
if not pnl:
|
||||
return None
|
||||
sym = (row.get("symbol") or "").strip()
|
||||
if not sym:
|
||||
return None
|
||||
try:
|
||||
from contract_specs import get_contract_spec
|
||||
from symbols import ths_to_codes
|
||||
|
||||
codes = ths_to_codes(sym) or {}
|
||||
ths = codes.get("ths_code") or sym
|
||||
mult = float(get_contract_spec(ths).get("mult") or 10)
|
||||
except Exception:
|
||||
mult = 10.0
|
||||
if mult <= 0:
|
||||
return None
|
||||
direction = (row.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(derived, 2)
|
||||
|
||||
|
||||
def reconcile_position_avg(
|
||||
old: Optional[dict[str, Any]],
|
||||
new: dict[str, Any],
|
||||
@@ -80,7 +43,7 @@ def reconcile_position_avg(
|
||||
ths_sym: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""手数不变时锁定均价;滚仓/加仓(手数变化)时以柜台加权均价为准。"""
|
||||
from ctp_entry_price import resolve_ctp_entry
|
||||
from ctp_entry_price import entry_from_ctp_pnl, resolve_ctp_entry
|
||||
|
||||
row = dict(new)
|
||||
lots = int(row.get("lots") or 0)
|
||||
@@ -89,6 +52,7 @@ def reconcile_position_avg(
|
||||
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
|
||||
@@ -96,12 +60,22 @@ def reconcile_position_avg(
|
||||
and old.get("avg_price_locked")
|
||||
and float(old.get("avg_price") or 0) > 0
|
||||
):
|
||||
row["avg_price"] = float(old["avg_price"])
|
||||
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
|
||||
|
||||
sym = ths_sym or (row.get("symbol") or "")
|
||||
entry, _src = resolve_ctp_entry(sym, direction, row, trades)
|
||||
entry, _src = resolve_ctp_entry(sym, direction, row, trades, tick=tick)
|
||||
if entry > 0:
|
||||
row["avg_price"] = entry
|
||||
row["avg_price_locked"] = True
|
||||
@@ -111,13 +85,6 @@ def reconcile_position_avg(
|
||||
if pos_avg > 0:
|
||||
row["avg_price"] = pos_avg
|
||||
row["avg_price_locked"] = lots_changed or bool(tick)
|
||||
return row
|
||||
|
||||
if not lots_changed:
|
||||
refined = avg_price_from_ctp_pnl(row, tick)
|
||||
if refined and refined > 0:
|
||||
row["avg_price"] = refined
|
||||
row["avg_price_locked"] = True
|
||||
return row
|
||||
|
||||
|
||||
@@ -207,18 +174,37 @@ class CtpTradingState:
|
||||
return dict(row) if row else None
|
||||
|
||||
def try_lock_entry_prices(self) -> bool:
|
||||
"""有 tick 后一次性校正未锁定的持仓均价。"""
|
||||
"""有 tick 后校正持仓均价(含已锁定但与柜台盈亏不一致的)。"""
|
||||
from ctp_entry_price import resolve_ctp_entry
|
||||
|
||||
changed = False
|
||||
with self._lock:
|
||||
for pk, row in list(self._positions.items()):
|
||||
if row.get("avg_price_locked"):
|
||||
ex = row.get("exchange") or ""
|
||||
sym = row.get("symbol") or ""
|
||||
tick = self.get_tick_price(ex, sym)
|
||||
if not tick or tick <= 0:
|
||||
continue
|
||||
tick = self.get_tick_price(row.get("exchange") or "", row.get("symbol") or "")
|
||||
refined = avg_price_from_ctp_pnl(row, tick)
|
||||
if not refined or refined <= 0:
|
||||
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"] = refined
|
||||
updated["avg_price"] = entry
|
||||
updated["avg_price_locked"] = True
|
||||
self._positions[pk] = updated
|
||||
changed = True
|
||||
|
||||
Reference in New Issue
Block a user