增加单仓平仓
This commit is contained in:
@@ -385,6 +385,42 @@ class CloseAllBody(BaseModel):
|
||||
exclude_ids: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ClosePositionBody(BaseModel):
|
||||
symbol: str
|
||||
side: str
|
||||
|
||||
|
||||
@app.post("/api/close/{exchange_id}/position")
|
||||
async def api_close_position(exchange_id: str, body: ClosePositionBody):
|
||||
ex = _find_exchange(exchange_id)
|
||||
if not ex or not ex.get("enabled"):
|
||||
raise HTTPException(status_code=404, detail="账户未启用")
|
||||
sym = (body.symbol or "").strip()
|
||||
side = (body.side or "").strip().lower()
|
||||
if not sym:
|
||||
raise HTTPException(status_code=400, detail="symbol 不能为空")
|
||||
if side not in ("long", "short"):
|
||||
raise HTTPException(status_code=400, detail="side 须为 long 或 short")
|
||||
url = f"{ex['agent_url'].rstrip('/')}/emergency/close-position"
|
||||
async with httpx.AsyncClient() as client:
|
||||
r = await client.post(
|
||||
url,
|
||||
headers=_agent_headers(),
|
||||
json={"symbol": sym, "side": side},
|
||||
timeout=120.0,
|
||||
)
|
||||
try:
|
||||
payload = r.json()
|
||||
except Exception:
|
||||
payload = {"raw": (r.text or "")[:2000]}
|
||||
return {
|
||||
"exchange": ex,
|
||||
"status_code": r.status_code,
|
||||
"payload": payload,
|
||||
"ok": bool(isinstance(payload, dict) and payload.get("ok")),
|
||||
}
|
||||
|
||||
|
||||
@app.post("/api/close/{exchange_id}")
|
||||
async def api_close_exchange(exchange_id: str):
|
||||
ex = _find_exchange(exchange_id)
|
||||
|
||||
Reference in New Issue
Block a user