122 lines
5.6 KiB
Python
122 lines
5.6 KiB
Python
"""Account-centric read model for live trading console."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.db.live_trading import _safe_float, get_live_account, list_live_order_events, list_live_order_intents
|
|
from app.integrations.binance_live import LiveTradingConfigError, build_binance_client
|
|
|
|
|
|
def _compact_balance(balance: dict) -> dict:
|
|
total = balance.get("total") if isinstance(balance.get("total"), dict) else {}
|
|
free = balance.get("free") if isinstance(balance.get("free"), dict) else {}
|
|
used = balance.get("used") if isinstance(balance.get("used"), dict) else {}
|
|
assets = []
|
|
for asset in sorted(set(total) | set(free) | set(used)):
|
|
total_value = _safe_float(total.get(asset))
|
|
free_value = _safe_float(free.get(asset))
|
|
used_value = _safe_float(used.get(asset))
|
|
if abs(total_value) > 0 or abs(free_value) > 0 or abs(used_value) > 0:
|
|
assets.append({"asset": asset, "free": free_value, "used": used_value, "total": total_value})
|
|
return {
|
|
"assets": assets,
|
|
"usdt": {
|
|
"free": _safe_float(free.get("USDT")),
|
|
"used": _safe_float(used.get("USDT")),
|
|
"total": _safe_float(total.get("USDT")),
|
|
},
|
|
}
|
|
|
|
|
|
def _compact_position(item: dict) -> dict:
|
|
info = item.get("info") if isinstance(item.get("info"), dict) else {}
|
|
contracts = _safe_float(item.get("contracts") or info.get("positionAmt"))
|
|
notional = _safe_float(item.get("notional") or info.get("notional"))
|
|
return {
|
|
"symbol": item.get("symbol") or info.get("symbol"),
|
|
"side": item.get("side") or ("long" if contracts > 0 else ("short" if contracts < 0 else "")),
|
|
"contracts": contracts,
|
|
"entry_price": _safe_float(item.get("entryPrice") or info.get("entryPrice")),
|
|
"mark_price": _safe_float(item.get("markPrice") or info.get("markPrice")),
|
|
"notional": notional,
|
|
"unrealized_pnl": _safe_float(item.get("unrealizedPnl") or info.get("unrealizedProfit")),
|
|
"leverage": _safe_float(item.get("leverage") or info.get("leverage")),
|
|
}
|
|
|
|
|
|
def _compact_order(item: dict) -> dict:
|
|
info = item.get("info") if isinstance(item.get("info"), dict) else {}
|
|
return {
|
|
"id": str(item.get("id") or info.get("orderId") or ""),
|
|
"client_order_id": item.get("clientOrderId") or info.get("clientOrderId") or "",
|
|
"symbol": item.get("symbol") or info.get("symbol"),
|
|
"type": item.get("type") or info.get("type"),
|
|
"side": item.get("side") or info.get("side"),
|
|
"status": item.get("status") or info.get("status"),
|
|
"price": _safe_float(item.get("price") or info.get("price")),
|
|
"amount": _safe_float(item.get("amount") or info.get("origQty")),
|
|
"filled": _safe_float(item.get("filled") or info.get("executedQty")),
|
|
"average": _safe_float(item.get("average") or info.get("avgPrice")),
|
|
"timestamp": item.get("datetime") or item.get("timestamp") or info.get("updateTime") or info.get("time"),
|
|
}
|
|
|
|
|
|
def _account_risk_view(account: dict) -> dict:
|
|
risk = account.get("risk_config") if isinstance(account.get("risk_config"), dict) else {}
|
|
allowed = [str(x).strip().upper() for x in risk.get("allowed_symbols", []) if str(x).strip()]
|
|
max_leverage = _safe_float(risk.get("max_symbol_leverage"), 1)
|
|
margin = _safe_float(risk.get("max_order_margin_usdt"), 0)
|
|
return {
|
|
"max_order_margin_usdt": margin,
|
|
"max_symbol_leverage": max_leverage,
|
|
"max_order_notional_usdt": _safe_float(risk.get("max_order_notional_usdt"), margin * max(1.0, max_leverage)),
|
|
"max_cumulative_leverage": _safe_float(risk.get("max_cumulative_leverage"), 1),
|
|
"max_daily_order_count": int(risk.get("max_daily_order_count") or 0),
|
|
"allowed_symbols": allowed,
|
|
"symbol_policy": "all" if not allowed else "allowlist",
|
|
}
|
|
|
|
|
|
def get_live_account_overview(account_id: int, *, history_limit: int = 30) -> dict:
|
|
account = get_live_account(account_id)
|
|
if not account:
|
|
raise LiveTradingConfigError("live account not found")
|
|
overview = {
|
|
"account": account,
|
|
"risk": _account_risk_view(account),
|
|
"balance": {"assets": [], "usdt": {"free": 0, "used": 0, "total": 0}},
|
|
"positions": [],
|
|
"open_orders": [],
|
|
"order_history": [],
|
|
"intent_history": list_live_order_intents(limit=history_limit, account_id=account_id).get("items", []),
|
|
"events": list_live_order_events(limit=history_limit).get("items", []),
|
|
"errors": [],
|
|
}
|
|
if account.get("status") != "enabled":
|
|
return overview
|
|
try:
|
|
client = build_binance_client(account, require_testnet=True)
|
|
client.load_markets()
|
|
except Exception as exc:
|
|
overview["errors"].append(f"账户连接失败:{exc}")
|
|
return overview
|
|
try:
|
|
overview["balance"] = _compact_balance(client.fetch_balance())
|
|
except Exception as exc:
|
|
overview["errors"].append(f"余额读取失败:{exc}")
|
|
try:
|
|
overview["positions"] = [
|
|
item for item in (_compact_position(p) for p in client.fetch_positions(None))
|
|
if abs(_safe_float(item.get("contracts"))) > 0
|
|
]
|
|
except Exception as exc:
|
|
overview["errors"].append(f"持仓读取失败:{exc}")
|
|
try:
|
|
overview["open_orders"] = [_compact_order(o) for o in client.fetch_open_orders(None)]
|
|
except Exception as exc:
|
|
overview["errors"].append(f"挂单读取失败:{exc}")
|
|
try:
|
|
overview["order_history"] = [_compact_order(o) for o in client.fetch_orders(None, limit=history_limit)]
|
|
except Exception as exc:
|
|
overview["errors"].append(f"订单历史读取失败:{exc}")
|
|
return overview
|