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
+21 -9
View File
@@ -831,13 +831,23 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
direction: str,
ctp: Optional[dict],
) -> tuple[float, str]:
del mode, direction
if not ctp:
return 0.0, "none"
avg = float(ctp.get("avg_price") or 0)
if avg > 0:
return round_to_tick(avg, sym), "ctp"
return 0.0, "none"
from modules.ctp.ctp_entry_price import resolve_ctp_entry
trades = None
open_avg_lookup = None
if ctp_status(mode).get("connected"):
try:
trades = ctp_list_trades(mode)
open_avg_lookup = get_bridge()._lookup_position_open_avg
except Exception:
pass
return resolve_ctp_entry(
sym,
direction,
ctp,
trades,
open_avg_lookup=open_avg_lookup,
)
def _open_commission_from_ctp_trades(
mode: str, sym: str, direction: str,
@@ -1492,18 +1502,20 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
if lots <= 0:
return None
entry_src = "monitor" if mon else "ctp"
if ctp:
ctp_lots = int(ctp.get("lots") or 0)
if ctp_lots > 0:
lots = ctp_lots
ths_sym = _ctp_pos_to_ths_code(ctp) or sym
resolved_entry, _entry_src = _resolve_ctp_entry_price(
resolved_entry, entry_src = _resolve_ctp_entry_price(
mode, ths_sym, direction, ctp,
)
if resolved_entry > 0:
entry = resolved_entry
elif float(ctp.get("avg_price") or 0) > 0:
entry = float(ctp.get("avg_price") or 0)
entry_src = "position_cost"
ctp_margin = float(ctp.get("margin") or 0)
if (margin is None or float(margin or 0) <= 0) and ctp_margin > 0:
margin = ctp_margin
@@ -1536,7 +1548,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
float_pnl = pos_tmp.get("float_pnl")
if ctp and ctp_status(mode).get("connected"):
ctp_pnl = float(ctp.get("pnl") or 0)
if ctp_pnl != 0:
if entry_src == "position_cost" and ctp_pnl != 0:
float_pnl = round(ctp_pnl, 2)
fee_info = calc_fee_breakdown(