Lock CTP entry price from position PnL snapshot; match SimNow avg and float PnL.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+15
-5
@@ -591,20 +591,21 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
direction: str,
|
||||
ctp: Optional[dict],
|
||||
) -> tuple[float, str]:
|
||||
"""持仓均价:柜台持仓价 > 成交加权(均不随 tick 变化)。"""
|
||||
"""持仓均价:成交加权 > 柜台持仓价(锁定后不随 tick 变化)。"""
|
||||
if not ctp:
|
||||
return 0.0, "none"
|
||||
direction = (direction or "long").strip().lower()
|
||||
lots = int(ctp.get("lots") or 0)
|
||||
pos_avg = float(ctp.get("avg_price") or 0)
|
||||
if pos_avg > 0:
|
||||
return pos_avg, "ctp"
|
||||
|
||||
trade_avg = _ctp_avg_entry_from_trades(
|
||||
mode, 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:
|
||||
return pos_avg, "ctp"
|
||||
return 0.0, "none"
|
||||
|
||||
def _open_commission_from_ctp_trades(
|
||||
@@ -1289,6 +1290,10 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
direction, entry, sl or entry, tp or entry, lots, mark, capital, sym,
|
||||
)
|
||||
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:
|
||||
float_pnl = round(ctp_pnl, 2)
|
||||
|
||||
fee_info = calc_fee_breakdown(
|
||||
sym, entry, close_est, lots, open_time or now_iso, now_iso, trading_mode=mode,
|
||||
@@ -1866,7 +1871,10 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
if entry <= 0:
|
||||
continue
|
||||
mult = float(get_contract_spec(ths).get("mult") or 10)
|
||||
if direction == "long":
|
||||
ctp_pnl = float(p.get("pnl") or 0)
|
||||
if ctp_pnl != 0:
|
||||
float_pnl = round(ctp_pnl, 2)
|
||||
elif direction == "long":
|
||||
float_pnl = round((mark - entry) * mult * lots, 2)
|
||||
else:
|
||||
float_pnl = round((entry - mark) * mult * lots, 2)
|
||||
@@ -1888,6 +1896,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
if not is_trading_session():
|
||||
return
|
||||
mode = get_trading_mode(get_setting)
|
||||
if trading_state.try_lock_entry_prices():
|
||||
_push_position_snapshot_async(fast=True)
|
||||
payload = _build_position_quotes_payload(mode)
|
||||
if payload.get("quotes"):
|
||||
position_hub.push_event("position_quotes", payload)
|
||||
|
||||
Reference in New Issue
Block a user