|
@@ -1,6 +1,7 @@
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import json
|
|
import json
|
|
|
|
|
+import logging
|
|
|
import os
|
|
import os
|
|
|
import time
|
|
import time
|
|
|
import threading
|
|
import threading
|
|
@@ -21,6 +22,7 @@ FINISHED_ORDER_STATUS = "finished"
|
|
|
_CANCEL_BREAKER_LOCK = threading.Lock()
|
|
_CANCEL_BREAKER_LOCK = threading.Lock()
|
|
|
_CANCEL_BREAKER_NEXT_ALLOWED: dict[str, float] = {}
|
|
_CANCEL_BREAKER_NEXT_ALLOWED: dict[str, float] = {}
|
|
|
_CANCEL_BREAKER_SECONDS = 3.0
|
|
_CANCEL_BREAKER_SECONDS = 3.0
|
|
|
|
|
+logger = logging.getLogger("exec_mcp")
|
|
|
|
|
|
|
|
|
|
|
|
|
def _cancel_breaker_is_open(account_id: str) -> bool:
|
|
def _cancel_breaker_is_open(account_id: str) -> bool:
|
|
@@ -238,6 +240,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:
|
|
def get_open_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
|
client_id = _normalize_client_id(client_id)
|
|
client_id = _normalize_client_id(client_id)
|
|
|
client = _get_client(account_id)
|
|
client = _get_client(account_id)
|
|
|
|
|
+ started = time.monotonic()
|
|
|
with get_connection() as conn:
|
|
with get_connection() as conn:
|
|
|
if client_id is None:
|
|
if client_id is None:
|
|
|
rows = conn.execute(
|
|
rows = conn.execute(
|
|
@@ -266,6 +269,8 @@ def get_open_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
|
|
|
|
|
|
orders = []
|
|
orders = []
|
|
|
delay = _bitstamp_call_delay_seconds()
|
|
delay = _bitstamp_call_delay_seconds()
|
|
|
|
|
+ if rows:
|
|
|
|
|
+ logger.info("get_open_orders scanning %d open row(s) for account_id=%s client_id=%s", len(rows), account_id, client_id)
|
|
|
for row in rows:
|
|
for row in rows:
|
|
|
order = dict(row)
|
|
order = dict(row)
|
|
|
bitstamp_order_id = order.get("bitstamp_order_id")
|
|
bitstamp_order_id = order.get("bitstamp_order_id")
|
|
@@ -296,11 +301,22 @@ def get_open_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
|
if delay > 0:
|
|
if delay > 0:
|
|
|
time.sleep(delay)
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
|
|
+ elapsed = time.monotonic() - started
|
|
|
|
|
+ if elapsed >= 5:
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "get_open_orders finished in %.3fs for account_id=%s client_id=%s rows=%d returned=%d",
|
|
|
|
|
+ elapsed,
|
|
|
|
|
+ account_id,
|
|
|
|
|
+ client_id,
|
|
|
|
|
+ len(rows),
|
|
|
|
|
+ len(orders),
|
|
|
|
|
+ )
|
|
|
return {"ok": True, "client_id": client_id, "orders": orders}
|
|
return {"ok": True, "client_id": client_id, "orders": orders}
|
|
|
|
|
|
|
|
|
|
|
|
|
def cancel_all_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
def cancel_all_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
|
client_id = _normalize_client_id(client_id)
|
|
client_id = _normalize_client_id(client_id)
|
|
|
|
|
+ started = time.monotonic()
|
|
|
orders = get_open_orders(account_id=account_id, client_id=client_id)["orders"]
|
|
orders = get_open_orders(account_id=account_id, client_id=client_id)["orders"]
|
|
|
results = []
|
|
results = []
|
|
|
delay = _bitstamp_call_delay_seconds()
|
|
delay = _bitstamp_call_delay_seconds()
|
|
@@ -329,6 +345,15 @@ def cancel_all_orders(*, account_id: str, client_id: str | None = None) -> dict:
|
|
|
if delay > 0:
|
|
if delay > 0:
|
|
|
time.sleep(delay)
|
|
time.sleep(delay)
|
|
|
invalidate_account_cache(account_id)
|
|
invalidate_account_cache(account_id)
|
|
|
|
|
+ elapsed = time.monotonic() - started
|
|
|
|
|
+ if elapsed >= 5:
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "cancel_all_orders finished in %.3fs for account_id=%s client_id=%s cancelled=%d",
|
|
|
|
|
+ elapsed,
|
|
|
|
|
+ account_id,
|
|
|
|
|
+ client_id,
|
|
|
|
|
+ len(results),
|
|
|
|
|
+ )
|
|
|
return {"ok": True, "client_id": client_id, "cancelled": results, "count": len(results)}
|
|
return {"ok": True, "client_id": client_id, "cancelled": results, "count": len(results)}
|
|
|
|
|
|
|
|
|
|
|