Persist CTP average entry price to monitor DB on every position sync.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+21
-4
@@ -401,7 +401,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
rows: list[dict],
|
rows: list[dict],
|
||||||
mode: str,
|
mode: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""将柜台校正后的保证金、仓位占比、已扣开仓手续费写入 trade_order_monitors。"""
|
"""将柜台校正后的均价、手数、现价、浮盈、保证金等写入 trade_order_monitors。"""
|
||||||
if not ctp_status(mode).get("connected"):
|
if not ctp_status(mode).get("connected"):
|
||||||
return
|
return
|
||||||
ensure_monitor_order_columns(conn)
|
ensure_monitor_order_columns(conn)
|
||||||
@@ -409,20 +409,37 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
mid = row.get("monitor_id")
|
mid = row.get("monitor_id")
|
||||||
if not mid or row.get("order_state") == "pending":
|
if not mid or row.get("order_state") == "pending":
|
||||||
continue
|
continue
|
||||||
|
entry_price = row.get("entry_price")
|
||||||
|
lots = row.get("lots")
|
||||||
|
mark_price = row.get("mark_price")
|
||||||
|
if mark_price is None:
|
||||||
|
mark_price = row.get("current_price")
|
||||||
|
float_pnl = row.get("float_pnl")
|
||||||
margin = row.get("margin")
|
margin = row.get("margin")
|
||||||
position_pct = row.get("position_pct")
|
position_pct = row.get("position_pct")
|
||||||
open_fee = row.get("est_fee")
|
open_fee = row.get("est_fee")
|
||||||
if margin is None and position_pct is None and open_fee is None:
|
if (
|
||||||
|
entry_price is None and lots is None and mark_price is None
|
||||||
|
and float_pnl is None and margin is None
|
||||||
|
and position_pct is None and open_fee is None
|
||||||
|
):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
execute_retry(
|
execute_retry(
|
||||||
conn,
|
conn,
|
||||||
"""UPDATE trade_order_monitors SET
|
"""UPDATE trade_order_monitors SET
|
||||||
|
entry_price=COALESCE(?, entry_price),
|
||||||
|
lots=COALESCE(?, lots),
|
||||||
|
mark_price=COALESCE(?, mark_price),
|
||||||
|
float_pnl=COALESCE(?, float_pnl),
|
||||||
margin=COALESCE(?, margin),
|
margin=COALESCE(?, margin),
|
||||||
position_pct=COALESCE(?, position_pct),
|
position_pct=COALESCE(?, position_pct),
|
||||||
open_fee=COALESCE(?, open_fee)
|
open_fee=COALESCE(?, open_fee)
|
||||||
WHERE id=? AND status='active'""",
|
WHERE id=? AND status='active'""",
|
||||||
(margin, position_pct, open_fee, int(mid)),
|
(
|
||||||
|
entry_price, lots, mark_price, float_pnl,
|
||||||
|
margin, position_pct, open_fee, int(mid),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.debug("persist monitor ctp snapshot %s: %s", mid, exc)
|
logger.debug("persist monitor ctp snapshot %s: %s", mid, exc)
|
||||||
@@ -1554,7 +1571,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
mon = _find_pending_monitor(
|
mon = _find_pending_monitor(
|
||||||
conn, ths, p.get("direction") or "long",
|
conn, ths, p.get("direction") or "long",
|
||||||
)
|
)
|
||||||
if mon and not fast:
|
if mon:
|
||||||
_sync_monitor_from_ctp(
|
_sync_monitor_from_ctp(
|
||||||
conn, int(mon["id"]), mon.get("symbol") or ths,
|
conn, int(mon["id"]), mon.get("symbol") or ths,
|
||||||
mon.get("direction") or p.get("direction") or "long",
|
mon.get("direction") or p.get("direction") or "long",
|
||||||
|
|||||||
Reference in New Issue
Block a user