Prefer CTP PnL-consistent entry when vnpy avg differs from SimNow.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-30 10:51:16 +08:00
parent e6208e403e
commit d07fc4b70d
4 changed files with 128 additions and 62 deletions
+63 -3
View File
@@ -1,12 +1,14 @@
# Copyright (c) 2025-2026 马建军. All rights reserved.
# 详见 LICENSE.zh-CN.txt
"""CTP 持仓均价:成交加权 / 柜台持仓价(滚仓加仓后以柜台为准)"""
"""CTP 持仓均价:成交加权 / 柜台持仓价 / 盈亏一致校正"""
from __future__ import annotations
from typing import Any, Optional
from contract_specs import get_contract_spec
from ctp_symbol import ths_to_vnpy_symbol
from symbols import ths_to_codes
def symbols_match(ctp_sym: str, ths: str) -> bool:
@@ -31,6 +33,47 @@ def symbols_match(ctp_sym: str, ths: str) -> bool:
return False
def _ths_code(sym: str) -> str:
codes = ths_to_codes(sym) or {}
return codes.get("ths_code") or sym
def round_to_tick(price: float, sym: str) -> float:
tick = float(get_contract_spec(_ths_code(sym)).get("tick_size") or 1.0)
if tick <= 0:
return round(price, 2)
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,
@@ -67,7 +110,7 @@ def avg_from_trades(
return None
if expect_lots > 0 and vol != expect_lots:
return None
return round(cost / vol, 4)
return round_to_tick(cost / vol, sym)
def resolve_ctp_entry(
@@ -75,17 +118,34 @@ def resolve_ctp_entry(
direction: str,
ctp: Optional[dict[str, Any]],
trades: Optional[list[dict[str, Any]]] = None,
*,
tick: Optional[float] = None,
) -> tuple[float, str]:
"""均价:成交加权 > 柜台 PositionCost 持仓价。"""
"""均价:成交加权 > 盈亏一致校正 > 柜台持仓价。"""
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 0.0, "none"