refactor: 将共用代码迁入 lib/ 模块化目录
统一 strategy、key_monitor、trade、hub 等共用库到 lib/ 子包,并补充 lib-structure 文档,便于四所与中控维护。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Shared library package."""
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Gate 平仓历史匹配(fetch_positions_history),供 reconcile / 中控全平同步共用。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def unified_symbol_for_match(symbol_str: str) -> str:
|
||||
x = (symbol_str or "").strip().upper()
|
||||
if ":" in x:
|
||||
x = x.split(":")[0]
|
||||
return x
|
||||
|
||||
|
||||
def pick_gate_position_close(
|
||||
hist: list[dict],
|
||||
symbol: str,
|
||||
direction: str,
|
||||
*,
|
||||
opened_at_ms: int | None = None,
|
||||
closed_at_ms: int | None = None,
|
||||
used_keys: set[str] | None = None,
|
||||
max_close_delta_ms: int = 25 * 60 * 1000,
|
||||
) -> dict | None:
|
||||
"""
|
||||
从 Gate 平仓历史列表中选取与 symbol/direction/开仓时间最匹配的一条。
|
||||
返回 normalize 后的 dict(含 close_ms、pnl、sync_key 等),无匹配则 None。
|
||||
"""
|
||||
if not hist:
|
||||
return None
|
||||
sym_u = unified_symbol_for_match(symbol)
|
||||
dir_l = (direction or "long").strip().lower()
|
||||
if dir_l not in ("long", "short"):
|
||||
return None
|
||||
used = used_keys or set()
|
||||
ref_ms = closed_at_ms or opened_at_ms
|
||||
best = None
|
||||
best_d = None
|
||||
for h in hist:
|
||||
if not isinstance(h, dict):
|
||||
continue
|
||||
sk = h.get("sync_key")
|
||||
if not sk or sk in used:
|
||||
continue
|
||||
if h.get("symbol_u") != sym_u:
|
||||
continue
|
||||
if (h.get("side") or "").strip().lower() != dir_l:
|
||||
continue
|
||||
cm = h.get("close_ms")
|
||||
if cm is None:
|
||||
continue
|
||||
if opened_at_ms is not None:
|
||||
if cm < opened_at_ms - 15 * 60 * 1000:
|
||||
continue
|
||||
if cm > opened_at_ms + 15 * 86400 * 1000:
|
||||
continue
|
||||
if ref_ms is not None:
|
||||
d = abs(int(cm) - int(ref_ms))
|
||||
else:
|
||||
d = 0
|
||||
if best_d is None or d < best_d:
|
||||
best_d = d
|
||||
best = h
|
||||
if best is None or best_d is None:
|
||||
return None
|
||||
if ref_ms is not None and best_d > max_close_delta_ms:
|
||||
return None
|
||||
return best
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Gate.io 资金划转(crypto_monitor_gate / crypto_monitor_gate_bot 共用)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
INVALID_KEY_HINT = (
|
||||
"。常见原因:① GATE_API_SECRET 错误或 .env 里多了空格/换行;② IP 白名单未包含当前服务器出口 IP;"
|
||||
"③ Gate「交易账户」类 API Key 若不支持钱包接口则无法走账户内划转 POST /wallet/transfers(需在官网确认该 Key 类型是否开放划转);"
|
||||
"④ Key 已重置或权限变更。你已勾选现货/统一账户仍报错时,优先核对 Secret 与白名单。"
|
||||
)
|
||||
|
||||
|
||||
def execute_transfer_usdt(
|
||||
exchange,
|
||||
amount: float,
|
||||
from_account: str,
|
||||
to_account: str,
|
||||
*,
|
||||
transfer_ccy: str = "USDT",
|
||||
ensure_live_ready: Callable[[], tuple[bool, str]],
|
||||
ensure_markets_loaded: Optional[Callable[[], None]] = None,
|
||||
) -> tuple[bool, str, Any]:
|
||||
if amount <= 0:
|
||||
return False, "划转金额必须大于0", None
|
||||
ok_live, reason = ensure_live_ready()
|
||||
if not ok_live:
|
||||
return False, reason, None
|
||||
if ensure_markets_loaded:
|
||||
try:
|
||||
ensure_markets_loaded()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
resp = exchange.transfer(transfer_ccy, float(amount), from_account, to_account)
|
||||
return True, "划转成功", resp
|
||||
except Exception as e:
|
||||
msg = str(e)
|
||||
if "INVALID_KEY" in msg or "Invalid key" in msg:
|
||||
msg += INVALID_KEY_HINT
|
||||
return False, msg, None
|
||||
|
||||
|
||||
def count_auto_transfer_blockers(conn, *, count_order_monitors: Callable[[Any], int]) -> int:
|
||||
"""自动划转持仓守卫:order_monitors active + 趋势回调已开仓计划。"""
|
||||
n = int(count_order_monitors(conn) or 0)
|
||||
if n > 0:
|
||||
return n
|
||||
try:
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) FROM trend_pullback_plans "
|
||||
"WHERE status='active' AND COALESCE(first_order_done, 0) != 0"
|
||||
).fetchone()
|
||||
return int(row[0] or 0) if row else 0
|
||||
except Exception:
|
||||
return n
|
||||
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
OKX 挂单聚合:普通委托 + 算法单(conditional / oco / trigger)。
|
||||
交易所 App「止盈止损」页多为 orders-algo-pending,仅 fetch_open_orders 默认拿不到。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _order_dedupe_key(order: dict) -> str:
|
||||
info = order.get("info") or {}
|
||||
if not isinstance(info, dict):
|
||||
info = {}
|
||||
return str(order.get("id") or info.get("algoId") or info.get("ordId") or "")
|
||||
|
||||
|
||||
def _okx_algo_cancel_id(order_id: str) -> str:
|
||||
oid = str(order_id or "")
|
||||
if ":" in oid:
|
||||
return oid.split(":", 1)[0]
|
||||
return oid
|
||||
|
||||
|
||||
def _okx_order_needs_stop_cancel_param(order: dict) -> bool:
|
||||
"""OKX 条件/算法单撤单须 params.stop=True,否则 cancel_order 走普通单接口会静默失败。"""
|
||||
if not isinstance(order, dict):
|
||||
return False
|
||||
info = order.get("info") or {}
|
||||
if not isinstance(info, dict):
|
||||
info = {}
|
||||
if order.get("stopLossPrice") is not None or order.get("takeProfitPrice") is not None:
|
||||
return True
|
||||
if info.get("algoId") or info.get("slTriggerPx") or info.get("tpTriggerPx"):
|
||||
return True
|
||||
typ = str(order.get("type") or info.get("ordType") or "").lower()
|
||||
for token in ("conditional", "oco", "trigger", "move_order_stop", "iceberg"):
|
||||
if token in typ:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def fetch_okx_all_open_orders(ex, exchange_symbol: str) -> list[dict]:
|
||||
"""合并 OKX 普通挂单与算法挂单(去重)。"""
|
||||
if not exchange_symbol:
|
||||
return []
|
||||
ex.load_markets()
|
||||
sym = exchange_symbol
|
||||
try:
|
||||
sym = ex.market(exchange_symbol)["symbol"]
|
||||
except Exception:
|
||||
pass
|
||||
seen: set[str] = set()
|
||||
out: list[dict] = []
|
||||
|
||||
def add_batch(batch: list | None) -> None:
|
||||
for o in batch or []:
|
||||
if not isinstance(o, dict):
|
||||
continue
|
||||
k = _order_dedupe_key(o)
|
||||
if not k or k in seen:
|
||||
continue
|
||||
seen.add(k)
|
||||
out.append(o)
|
||||
|
||||
try:
|
||||
add_batch(ex.fetch_open_orders(sym))
|
||||
except Exception:
|
||||
pass
|
||||
for params in (
|
||||
{"ordType": "conditional"},
|
||||
{"ordType": "oco"},
|
||||
{"trigger": True},
|
||||
):
|
||||
try:
|
||||
add_batch(ex.fetch_open_orders(sym, params=dict(params)))
|
||||
except Exception:
|
||||
pass
|
||||
return out
|
||||
|
||||
|
||||
def cancel_okx_all_open_orders(ex, exchange_symbol: str) -> int:
|
||||
"""
|
||||
撤销某合约全部挂单(普通 + 条件/算法)。
|
||||
OKX 止盈止损在 orders-algo-pending,必须用 stop=True 才能撤掉。
|
||||
"""
|
||||
if not exchange_symbol:
|
||||
return 0
|
||||
ex.load_markets()
|
||||
sym = exchange_symbol
|
||||
try:
|
||||
sym = ex.market(exchange_symbol)["symbol"]
|
||||
except Exception:
|
||||
pass
|
||||
n = 0
|
||||
for o in fetch_okx_all_open_orders(ex, sym):
|
||||
oid = _order_dedupe_key(o)
|
||||
if not oid:
|
||||
continue
|
||||
cancel_id = _okx_algo_cancel_id(oid)
|
||||
params = {"stop": True} if _okx_order_needs_stop_cancel_param(o) else None
|
||||
try:
|
||||
ex.cancel_order(cancel_id, sym, params)
|
||||
n += 1
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
ex.cancel_order(oid, sym, params)
|
||||
n += 1
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
ex.cancel_all_orders(sym)
|
||||
except Exception:
|
||||
pass
|
||||
return n
|
||||
Reference in New Issue
Block a user