Add trailing BE to SL/TP dialog and speed up position refresh.
Use modal for monitor upsert with trailing checkbox, refresh CTP tick every second, and push full snapshot after orders. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+19
-7
@@ -1045,11 +1045,11 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
tp = float(mon["take_profit"]) if mon and mon.get("take_profit") is not None else None
|
||||
holding = _holding_duration(open_time, now_iso) if open_time else ""
|
||||
|
||||
if (mark is None or float(mark or 0) <= 0) and not fast and ctp_status(mode).get("connected"):
|
||||
if ctp_status(mode).get("connected"):
|
||||
live_mark = ctp_get_tick_price(mode, sym)
|
||||
if live_mark and live_mark > 0:
|
||||
mark = live_mark
|
||||
if (mark is None or float(mark or 0) <= 0) and not fast and codes:
|
||||
elif (mark is None or float(mark or 0) <= 0) and not fast and codes:
|
||||
mark = fetch_price(
|
||||
sym,
|
||||
codes.get("market_code", ""),
|
||||
@@ -1780,6 +1780,11 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
return jsonify({"ok": False, "error": "缺少品种代码"}), 400
|
||||
if sl is None and tp is None:
|
||||
return jsonify({"ok": False, "error": "请至少填写止损或止盈"}), 400
|
||||
trailing_on = bool(d.get("trailing_be"))
|
||||
if trailing_on and sl is None:
|
||||
return jsonify({"ok": False, "error": "移动保本须填写止损价"}), 400
|
||||
if trailing_on:
|
||||
tp = None
|
||||
mode = get_trading_mode(get_setting)
|
||||
conn = get_db()
|
||||
try:
|
||||
@@ -1801,7 +1806,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
break
|
||||
if not has_pos:
|
||||
return jsonify({"ok": False, "error": "未找到对应持仓"}), 400
|
||||
trailing_be = 1 if d.get("trailing_be") else (
|
||||
trailing_be = 1 if trailing_on else (
|
||||
int(mon.get("trailing_be") or 0) if mon else 0
|
||||
)
|
||||
mid = _upsert_open_monitor(
|
||||
@@ -1814,8 +1819,15 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
tp=tp,
|
||||
trailing_be=trailing_be,
|
||||
)
|
||||
if trailing_on and sl is not None:
|
||||
conn.execute(
|
||||
"""UPDATE trade_order_monitors SET
|
||||
take_profit=NULL, initial_stop_loss=?, trailing_r_locked=0
|
||||
WHERE id=?""",
|
||||
(sl, mid),
|
||||
)
|
||||
conn.commit()
|
||||
_push_position_snapshot_async()
|
||||
_push_position_snapshot_async(fast=False)
|
||||
return jsonify({
|
||||
"ok": True,
|
||||
"monitor_id": mid,
|
||||
@@ -2419,7 +2431,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
f"{trading_mode_label(get_setting)} {offset} {sym} {direction} {lots}手 @{price}"
|
||||
)
|
||||
conn.close()
|
||||
_push_position_snapshot_async()
|
||||
_push_position_snapshot_async(fast=False)
|
||||
return jsonify({
|
||||
"ok": True,
|
||||
"result": result,
|
||||
@@ -3196,8 +3208,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
||||
|
||||
start_position_worker(
|
||||
refresh_fn=_position_worker_refresh,
|
||||
interval=2,
|
||||
idle_interval=5,
|
||||
interval=1,
|
||||
idle_interval=3,
|
||||
)
|
||||
_bootstrap_trading_runtime()
|
||||
start_ctp_fee_worker(
|
||||
|
||||
Reference in New Issue
Block a user