中控条件单页:下发/取消/改出场,有仓客户端不可选。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+114
-7
@@ -1,4 +1,4 @@
|
||||
"""拉取各 crypto_okx 客户端快照;以及受限写指令(买一全平)."""
|
||||
"""拉取各 crypto_okx 客户端快照;以及受限写指令(买一全平/条件单)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
@@ -8,7 +8,7 @@ from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
from lib.instances_lib import list_instances
|
||||
from lib.instances_lib import get_instance, list_instances
|
||||
|
||||
|
||||
def _timeout() -> float:
|
||||
@@ -25,6 +25,14 @@ def _close_timeout() -> float:
|
||||
return 45.0
|
||||
|
||||
|
||||
def _headers(token: str) -> dict[str, str]:
|
||||
return {
|
||||
"X-Hub-Token": token,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
|
||||
def fetch_one_snapshot(inst: dict[str, Any]) -> dict[str, Any]:
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
@@ -82,11 +90,7 @@ def post_close_full(inst: dict[str, Any], *, inst_id: str) -> dict[str, Any]:
|
||||
try:
|
||||
r = requests.post(
|
||||
url,
|
||||
headers={
|
||||
"X-Hub-Token": token,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
headers=_headers(token),
|
||||
json={"inst_id": iid},
|
||||
timeout=_close_timeout(),
|
||||
)
|
||||
@@ -107,6 +111,88 @@ def post_close_full(inst: dict[str, Any], *, inst_id: str) -> dict[str, Any]:
|
||||
return {"ok": False, "msg": str(e) or "下发失败"}
|
||||
|
||||
|
||||
def post_cond_order(inst: dict[str, Any], payload: dict[str, Any]) -> dict[str, Any]:
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
if not base or not token:
|
||||
return {"ok": False, "msg": "缺少 URL 或 Token", "instance_id": inst.get("id")}
|
||||
url = f"{base}/api/hub/options/cond_orders"
|
||||
try:
|
||||
r = requests.post(url, headers=_headers(token), json=payload, timeout=_close_timeout())
|
||||
data = r.json() if r.content else {}
|
||||
if not isinstance(data, dict):
|
||||
data = {"ok": False, "msg": f"HTTP {r.status_code}"}
|
||||
data["instance_id"] = inst.get("id")
|
||||
data["instance_name"] = inst.get("name")
|
||||
return data
|
||||
except requests.Timeout:
|
||||
return {
|
||||
"ok": False,
|
||||
"msg": "客户端超时",
|
||||
"instance_id": inst.get("id"),
|
||||
"instance_name": inst.get("name"),
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"ok": False,
|
||||
"msg": str(e) or "下发失败",
|
||||
"instance_id": inst.get("id"),
|
||||
"instance_name": inst.get("name"),
|
||||
}
|
||||
|
||||
|
||||
def post_cond_cancel(inst: dict[str, Any], hub_order_id: str) -> dict[str, Any]:
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
oid = (hub_order_id or "").strip()
|
||||
if not base or not token or not oid:
|
||||
return {"ok": False, "msg": "参数不完整"}
|
||||
url = f"{base}/api/hub/options/cond_orders/{oid}/cancel"
|
||||
try:
|
||||
r = requests.post(url, headers=_headers(token), json={}, timeout=_timeout())
|
||||
data = r.json() if r.content else {}
|
||||
return data if isinstance(data, dict) else {"ok": False, "msg": f"HTTP {r.status_code}"}
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": str(e) or "取消失败"}
|
||||
|
||||
|
||||
def post_cond_exit(inst: dict[str, Any], hub_order_id: str, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
oid = (hub_order_id or "").strip()
|
||||
if not base or not token or not oid:
|
||||
return {"ok": False, "msg": "参数不完整"}
|
||||
url = f"{base}/api/hub/options/cond_orders/{oid}/exit"
|
||||
try:
|
||||
r = requests.post(url, headers=_headers(token), json=payload or {}, timeout=_timeout())
|
||||
data = r.json() if r.content else {}
|
||||
return data if isinstance(data, dict) else {"ok": False, "msg": f"HTTP {r.status_code}"}
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": str(e) or "更新失败"}
|
||||
|
||||
|
||||
def fetch_cond_orders(inst: dict[str, Any]) -> dict[str, Any]:
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
if not base or not token:
|
||||
return {"ok": False, "msg": "缺少 URL 或 Token", "orders": []}
|
||||
url = f"{base}/api/hub/options/cond_orders"
|
||||
try:
|
||||
r = requests.get(
|
||||
url,
|
||||
headers={"X-Hub-Token": token, "Accept": "application/json"},
|
||||
timeout=_timeout(),
|
||||
)
|
||||
data = r.json() if r.content else {}
|
||||
if isinstance(data, dict):
|
||||
data["instance_id"] = inst.get("id")
|
||||
data["instance_name"] = inst.get("name")
|
||||
return data
|
||||
return {"ok": False, "msg": f"HTTP {r.status_code}", "orders": []}
|
||||
except Exception as e:
|
||||
return {"ok": False, "msg": str(e) or "拉取失败", "orders": [], "instance_id": inst.get("id")}
|
||||
|
||||
|
||||
def pull_all_enabled() -> list[dict[str, Any]]:
|
||||
instances = list_instances(enabled_only=True)
|
||||
if not instances:
|
||||
@@ -121,3 +207,24 @@ def pull_all_enabled() -> list[dict[str, Any]]:
|
||||
for inst in instances:
|
||||
results.append(by_id.get(inst["id"]) or fetch_one_snapshot(inst))
|
||||
return results
|
||||
|
||||
|
||||
def dispatch_cond_order_to_ids(instance_ids: list[int], payload: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
results = []
|
||||
for iid in instance_ids:
|
||||
inst = get_instance(int(iid))
|
||||
if not inst:
|
||||
results.append({"ok": False, "msg": "实例不存在", "instance_id": iid})
|
||||
continue
|
||||
if not inst.get("enabled"):
|
||||
results.append(
|
||||
{
|
||||
"ok": False,
|
||||
"msg": "实例未启用",
|
||||
"instance_id": iid,
|
||||
"instance_name": inst.get("name"),
|
||||
}
|
||||
)
|
||||
continue
|
||||
results.append(post_cond_order(inst, payload))
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user