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
+17 -2
View File
@@ -464,19 +464,34 @@ class CtpBridge:
td = max(0, vol - yd)
margin = self.estimate_position_margin(sym, ex_name, d, vol, price, pos=pos)
open_time = self._lookup_position_open_time(sym, d) or None
return {
pnl = float(getattr(pos, "pnl", 0) or 0)
row = {
"symbol": sym,
"exchange": ex_name,
"direction": d,
"lots": vol,
"avg_price": price,
"pnl": float(getattr(pos, "pnl", 0) or 0),
"pnl": pnl,
"frozen": int(getattr(pos, "frozen", 0) or 0),
"margin": margin,
"open_time": open_time,
"yd_volume": yd,
"td_volume": td,
}
try:
from ctp_entry_price import entry_from_ctp_pnl, round_to_tick
from ctp_trading_state import trading_state
ths = CtpBridge._vnpy_sym_to_ths(sym, ex_name) or sym
tick = trading_state.get_tick_price(ex_name, sym)
pnl_entry = entry_from_ctp_pnl(row, tick, ths_sym=ths)
if pnl_entry and price > 0 and abs(pnl_entry - price) >= 0.5:
row["avg_price"] = pnl_entry
elif price > 0:
row["avg_price"] = round_to_tick(price, ths)
except Exception as exc:
logger.debug("position avg refine: %s", exc)
return row
except Exception as exc:
logger.debug("position_row_from_vnpy: %s", exc)
return None