fix: Gate hub close sync and trade record open stop-loss snapshot

Sync order monitors from Gate position history after hub flat close. Store and display initial_stop_loss in trade records instead of post-amend exchange stops.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-09 18:01:59 +08:00
parent 02d2a6c70b
commit 59a45ed027
11 changed files with 449 additions and 40 deletions
+36
View File
@@ -207,6 +207,7 @@ def install_on_app(
ohlcv_fn=None,
account_fn=None,
volume_rank_fn=None,
reconcile_hub_flat_fn=None,
):
app.config["HUB_CTX"] = {
"exchange": exchange,
@@ -219,6 +220,7 @@ def install_on_app(
"views": views,
"ohlcv_fn": ohlcv_fn,
"volume_rank_fn": volume_rank_fn,
"reconcile_hub_flat_fn": reconcile_hub_flat_fn,
}
install_hub_embed_headers(app)
configure_hub_embed_session(app)
@@ -607,6 +609,40 @@ def register_hub_routes(app):
return jsonify({"ok": False, "msg": "该实例无趋势回调"}), 400
return jsonify(_invoke_view_get("stop_trend_pullback", f"/stop_trend_pullback/{pid}"))
@app.route("/api/hub/order/sync-flat", methods=["POST"])
@_hub_auth_required
def api_hub_order_sync_flat():
"""中控市价全平后:同步 order_monitors 并读 Gate 平仓历史写交易记录。"""
fn = _ctx().get("reconcile_hub_flat_fn")
if not callable(fn):
return jsonify({"ok": False, "msg": "该实例未配置 order sync-flat"}), 400
body = request.get_json(silent=True) or {}
symbol = (body.get("symbol") or request.form.get("symbol") or "").strip()
side = (
body.get("side")
or body.get("direction")
or request.form.get("side")
or ""
).strip().lower()
if not symbol:
return jsonify({"ok": False, "msg": "symbol 不能为空"}), 400
if side not in ("long", "short"):
return jsonify({"ok": False, "msg": "side 须为 long 或 short"}), 400
get_db = _ctx().get("get_db")
if not callable(get_db):
return jsonify({"ok": False, "msg": "HUB_CTX 缺少 get_db"}), 500
conn = get_db()
try:
out = fn(conn, symbol, side)
if not isinstance(out, dict):
out = {"ok": True, "synced": int(out or 0)}
conn.commit()
return jsonify(out)
except Exception as e:
return jsonify({"ok": False, "msg": str(e)}), 500
finally:
conn.close()
@app.route("/api/hub/trend/sync-flat", methods=["POST"])
@_hub_auth_required
def api_hub_trend_sync_flat():