Przeglądaj źródła

Reconcile open orders against Bitstamp before returning

Lukas Goldschmidt 1 miesiąc temu
rodzic
commit
eb7c5fddb0
1 zmienionych plików z 26 dodań i 1 usunięć
  1. 26 1
      src/exec_mcp/services_orders.py

+ 26 - 1
src/exec_mcp/services_orders.py

@@ -145,6 +145,7 @@ def place_order(*, account_id: str, market: str, side: str, order_type: str, amo
 
 def get_open_orders(*, account_id: str, client_id: str | None = None) -> dict:
     client_id = _normalize_client_id(client_id)
+    client = _get_client(account_id)
     with get_connection() as conn:
         if client_id is None:
             rows = conn.execute(
@@ -166,7 +167,31 @@ def get_open_orders(*, account_id: str, client_id: str | None = None) -> dict:
                 """,
                 (account_id, client_id),
             ).fetchall()
-    return {"ok": True, "client_id": client_id, "orders": [dict(row) for row in rows]}
+
+    orders = []
+    for row in rows:
+        order = dict(row)
+        bitstamp_order_id = order.get("bitstamp_order_id")
+        if not bitstamp_order_id:
+            continue
+        try:
+            result = client.trading.order_status_v2(order_id=str(bitstamp_order_id), omit_transactions=True)
+            status = _normalize_status(result.get("status", "unknown"))
+            if status not in OPEN_ORDER_STATUSES:
+                _set_local_order_status(bitstamp_order_id=str(bitstamp_order_id), status=status)
+                continue
+            order["status"] = status
+            order["raw_json"] = json.dumps(result)
+            orders.append(order)
+        except BitstampError as exc:
+            msg = str(exc)
+            if "not found" in msg.lower():
+                _set_local_order_status(bitstamp_order_id=str(bitstamp_order_id), status="missing")
+                continue
+            # Best effort: keep the local record when the exchange cannot be queried.
+            orders.append(order)
+
+    return {"ok": True, "client_id": client_id, "orders": orders}
 
 
 def cancel_all_orders(*, account_id: str, client_id: str | None = None) -> dict: