Fix CTP open average price direction mapping and resolution order.

Correct PosiDirection 2=long/3=short so OpenCost caches under the right key, prefer open_cost over PositionCost for entry and float P/L, and refresh the cache when incomplete.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-02 21:13:28 +08:00
parent 870dfb3bc0
commit dca773d6be
3 changed files with 80 additions and 22 deletions
+14 -8
View File
@@ -4,7 +4,7 @@
"""CTP 持仓均价:优先 CTP OpenCost(柜台开仓均价),其次成交加权。"""
from __future__ import annotations
from typing import Any, Optional
from typing import Any, Callable, Optional
from modules.core.contract_specs import get_contract_spec
from modules.ctp.ctp_symbol import ths_to_vnpy_symbol
@@ -99,14 +99,20 @@ def resolve_ctp_entry(
trades: Optional[list[dict[str, Any]]] = None,
*,
tick: Optional[float] = None,
open_avg_lookup: Optional[Callable[[str, str], float]] = None,
) -> tuple[float, str]:
"""均价:优先 avg_priceOpenCost),否则成交加权"""
if not ctp:
return 0.0, "none"
pos_avg = float(ctp.get("avg_price") or 0)
if pos_avg > 0:
return round_to_tick(pos_avg, sym), "ctp"
trade_avg = compute_open_avg_from_trades(sym, direction or "long", trades)
"""均价:OpenCost 缓存 → 成交加权 → vnpy PositionCost"""
del tick
want = (direction or "long").strip().lower()
if open_avg_lookup:
cached = float(open_avg_lookup(sym, want) or 0)
if cached > 0:
return round_to_tick(cached, sym), "open_cost"
trade_avg = compute_open_avg_from_trades(sym, want, trades)
if trade_avg > 0:
return round_to_tick(trade_avg, sym), "trades"
if ctp:
pos_avg = float(ctp.get("avg_price") or 0)
if pos_avg > 0:
return round_to_tick(pos_avg, sym), "position_cost"
return 0.0, "none"