Add Binance live trading, anti-stuck open/close recovery, and configurable rate limits.
OKX/Binance LIVE share half_open and option_closed_perp_pending repair paths; private REST throttles default to 1s and are tunable in settings. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""实盘执行适配层。"""
|
||||
|
||||
from .executor import BinanceLiveStub, OkxLiveExecutor, get_executor
|
||||
from .binance_executor import BinanceLiveExecutor
|
||||
from .executor import OkxLiveExecutor, get_executor
|
||||
|
||||
__all__ = ["get_executor", "OkxLiveExecutor", "BinanceLiveStub"]
|
||||
__all__ = ["get_executor", "OkxLiveExecutor", "BinanceLiveExecutor"]
|
||||
|
||||
@@ -0,0 +1,901 @@
|
||||
"""币安实盘执行:eapi 期权 + fapi 永续;先期权后永续(含 anti-stuck 状态机)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
from ..config import get_settings
|
||||
from ..env_store import live_ready
|
||||
from ..sim.liquidity import contracts_for_eth
|
||||
from ..sim.matcher import CloseResult, Matcher, OpenResult
|
||||
from ..sim.pricing import option_expiry_settle, option_intrinsic
|
||||
from ..strategy.session import get_session
|
||||
from .binance_trade import BinanceTradeClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BinanceLiveExecutor(Matcher):
|
||||
def __init__(self, db=None) -> None:
|
||||
super().__init__(db)
|
||||
self._trade: BinanceTradeClient | None = None
|
||||
|
||||
def _client(self) -> BinanceTradeClient:
|
||||
if self._trade is None:
|
||||
self._trade = BinanceTradeClient()
|
||||
return self._trade
|
||||
|
||||
def _guard_live(self) -> str | None:
|
||||
ok, reason = live_ready()
|
||||
if not ok:
|
||||
return reason
|
||||
return None
|
||||
|
||||
def open_group(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
bias: str,
|
||||
option_side: str,
|
||||
perp_side: str,
|
||||
option_inst_id: str,
|
||||
entry_index_px: float,
|
||||
strike: float | None = None,
|
||||
expiry_ymd: str | None = None,
|
||||
) -> OpenResult:
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return OpenResult(ok=False, detail=err)
|
||||
|
||||
s = get_settings()
|
||||
if self.has_open_position():
|
||||
st = self.position_status()
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
detail=f"已有持仓/半仓状态({st}),请先修复或平仓",
|
||||
)
|
||||
|
||||
client = self._client()
|
||||
perp_qty = self.ledger.get_setting_float("perp_qty_eth", s.perp_qty_eth)
|
||||
opt_qty = self.ledger.get_setting_float("option_qty_eth", s.option_qty_eth)
|
||||
ct_mult = self._ct_mult(option_inst_id)
|
||||
opt_contracts = contracts_for_eth(opt_qty, ct_mult)
|
||||
|
||||
try:
|
||||
opt_fill = client.place_option_market(
|
||||
symbol=option_inst_id,
|
||||
side="BUY",
|
||||
quantity=opt_contracts,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("binance live open option failed")
|
||||
return OpenResult(ok=False, detail=f"币安开期权失败: {e}")
|
||||
|
||||
# 永续市价失败(多为保证金不足)→ 必须回滚期权
|
||||
try:
|
||||
if perp_side == "long":
|
||||
side, pos_side = "BUY", "LONG"
|
||||
else:
|
||||
side, pos_side = "SELL", "SHORT"
|
||||
perp_fill_live = client.place_perp_market(
|
||||
symbol=s.perp_inst_id,
|
||||
side=side,
|
||||
qty_eth=perp_qty,
|
||||
position_side=pos_side,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("binance live open perp failed (likely margin); rollback option")
|
||||
try:
|
||||
client.place_option_market(
|
||||
symbol=option_inst_id,
|
||||
side="SELL",
|
||||
quantity=opt_contracts,
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e2:
|
||||
logger.exception("binance option rollback failed: %s", e2)
|
||||
self._persist_half_open(
|
||||
group_id=group_id,
|
||||
bias=bias,
|
||||
option_side=option_side,
|
||||
perp_side=perp_side,
|
||||
option_inst_id=option_inst_id,
|
||||
entry_index_px=entry_index_px,
|
||||
strike=strike,
|
||||
expiry_ymd=expiry_ymd,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=float(opt_fill.avg_px),
|
||||
of_fee=float(opt_fill.fee),
|
||||
detail=f"保证金开永续失败且期权回滚失败: {e} / {e2}",
|
||||
)
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
group_id=group_id,
|
||||
detail=f"永续开仓失败(保证金)且期权回滚失败,已标记 half_open: {e} / {e2}",
|
||||
)
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
detail=f"永续开仓失败(多为保证金不足),已回滚期权: {e}",
|
||||
)
|
||||
|
||||
of_px = float(opt_fill.avg_px)
|
||||
pf_px = float(perp_fill_live.avg_px)
|
||||
of_fee = float(opt_fill.fee)
|
||||
pf_fee = float(perp_fill_live.fee)
|
||||
initial_premium = of_px * opt_qty
|
||||
of_notional = of_px * opt_qty
|
||||
pf_notional = pf_px * perp_qty
|
||||
|
||||
# LIVE:交易所已成交,本地账本允许透支镜像,禁止因账本拒记导致「交易所有仓、DB 空」
|
||||
self.ledger.apply_cash(
|
||||
-(of_notional + of_fee),
|
||||
kind="open_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN open option {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
-pf_fee,
|
||||
kind="open_perp_fee",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN open perp {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO groups(
|
||||
group_id, status, bias, option_side, perp_side, option_inst_id, perp_inst_id,
|
||||
strike, expiry_ymd, entry_index_px, initial_premium, open_at_ms, fees, slip_cost,
|
||||
exec_mode
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"open",
|
||||
bias,
|
||||
option_side,
|
||||
perp_side,
|
||||
option_inst_id,
|
||||
s.perp_inst_id,
|
||||
strike,
|
||||
expiry_ymd,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
now,
|
||||
of_fee + pf_fee,
|
||||
0.0,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"open",
|
||||
"long",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
0.0,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"perp",
|
||||
"open",
|
||||
perp_side,
|
||||
s.perp_inst_id,
|
||||
perp_qty,
|
||||
None,
|
||||
pf_px,
|
||||
pf_px,
|
||||
pf_fee,
|
||||
0.0,
|
||||
pf_notional,
|
||||
now + 1,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=?, perp_side=?, perp_qty_eth=?, perp_entry_px=?,
|
||||
option_inst_id=?, option_side=?, option_qty_eth=?, option_qty_contracts=?,
|
||||
option_entry_px=?, entry_index_px=?, initial_premium=?, status=?
|
||||
WHERE id=1""",
|
||||
(
|
||||
group_id,
|
||||
perp_side,
|
||||
perp_qty,
|
||||
pf_px,
|
||||
option_inst_id,
|
||||
option_side,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
"open",
|
||||
),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
return OpenResult(
|
||||
ok=True,
|
||||
group_id=group_id,
|
||||
detail="opened_live_binance",
|
||||
data={
|
||||
"group_id": group_id,
|
||||
"exec_mode": "LIVE",
|
||||
"exchange": "binance",
|
||||
"option_ord": opt_fill.ord_id,
|
||||
"perp_ord": perp_fill_live.ord_id,
|
||||
"initial_premium": initial_premium,
|
||||
"fees": of_fee + pf_fee,
|
||||
},
|
||||
)
|
||||
|
||||
def _persist_half_open(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
bias: str,
|
||||
option_side: str,
|
||||
perp_side: str,
|
||||
option_inst_id: str,
|
||||
entry_index_px: float,
|
||||
strike: float | None,
|
||||
expiry_ymd: str | None,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
detail: str,
|
||||
) -> None:
|
||||
"""期权已成交、永续未开且回滚失败 → 落 half_open,禁止新开,待 repair。"""
|
||||
s = get_settings()
|
||||
initial_premium = of_px * opt_qty
|
||||
self.ledger.apply_cash(
|
||||
-(of_px * opt_qty + of_fee),
|
||||
kind="open_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN half_open option {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
existing = self.db._conn.execute(
|
||||
"SELECT group_id FROM groups WHERE group_id=?", (group_id,)
|
||||
).fetchone()
|
||||
if existing is None:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO groups(
|
||||
group_id, status, bias, option_side, perp_side, option_inst_id, perp_inst_id,
|
||||
strike, expiry_ymd, entry_index_px, initial_premium, open_at_ms, fees, slip_cost,
|
||||
exec_mode, note
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"half_open",
|
||||
bias,
|
||||
option_side,
|
||||
perp_side,
|
||||
option_inst_id,
|
||||
s.perp_inst_id,
|
||||
strike,
|
||||
expiry_ymd,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
now,
|
||||
of_fee,
|
||||
0.0,
|
||||
"LIVE",
|
||||
detail[:200],
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"open",
|
||||
"long",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
0.0,
|
||||
of_px * opt_qty,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=?, perp_side=?, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=?, option_side=?, option_qty_eth=?, option_qty_contracts=?,
|
||||
option_entry_px=?, entry_index_px=?, initial_premium=?, status='half_open'
|
||||
WHERE id=1""",
|
||||
(
|
||||
group_id,
|
||||
perp_side,
|
||||
option_inst_id,
|
||||
option_side,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
def repair_half_open(self) -> CloseResult:
|
||||
"""卖出 half_open 残留期权,清本地状态。"""
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return CloseResult(ok=False, detail=err)
|
||||
pos = self.current_position()
|
||||
if pos.get("status") != "half_open":
|
||||
return CloseResult(ok=False, detail="非 half_open 状态")
|
||||
group_id = str(pos.get("group_id") or "")
|
||||
option_inst_id = str(pos.get("option_inst_id") or "")
|
||||
opt_contracts = float(pos.get("option_qty_contracts") or 0)
|
||||
opt_qty = float(pos.get("option_qty_eth") or 0)
|
||||
if not option_inst_id or opt_contracts <= 0:
|
||||
return CloseResult(ok=False, detail="half_open 缺期权合约信息")
|
||||
client = self._client()
|
||||
try:
|
||||
opt_live = client.place_option_market(
|
||||
symbol=option_inst_id,
|
||||
side="SELL",
|
||||
quantity=opt_contracts,
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e:
|
||||
return CloseResult(ok=False, detail=f"half_open 平期权失败: {e}")
|
||||
of_px = float(opt_live.avg_px)
|
||||
of_fee = float(opt_live.fee)
|
||||
of_notional = of_px * opt_qty
|
||||
opt_entry = float(pos.get("option_entry_px") or of_px)
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id or None,
|
||||
note="LIVE-BN repair half_open",
|
||||
allow_negative=True,
|
||||
)
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
if group_id:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"close",
|
||||
"flat",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
0.0,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
opt_pnl = (of_px - opt_entry) * opt_qty - of_fee
|
||||
self.db._conn.execute(
|
||||
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?, note=?
|
||||
WHERE group_id=?""",
|
||||
(
|
||||
"closed",
|
||||
now,
|
||||
"half_open_repair",
|
||||
float(opt_pnl),
|
||||
"repaired half_open",
|
||||
group_id,
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=NULL, perp_side=NULL, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=NULL, option_side=NULL, option_qty_eth=0, option_qty_contracts=0,
|
||||
option_entry_px=NULL, entry_index_px=NULL, initial_premium=0, status='flat'
|
||||
WHERE id=1"""
|
||||
)
|
||||
self.db._conn.commit()
|
||||
return CloseResult(
|
||||
ok=True,
|
||||
detail="half_open_repaired",
|
||||
data={"group_id": group_id, "exec_mode": "LIVE", "exchange": "binance"},
|
||||
)
|
||||
|
||||
def close_group(self, *, reason: str, bypass_liquidity: bool = False) -> CloseResult:
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return CloseResult(ok=False, detail=err)
|
||||
|
||||
s = get_settings()
|
||||
pos = self.current_position()
|
||||
st = str(pos.get("status") or "")
|
||||
if st == "half_open":
|
||||
return self.repair_half_open()
|
||||
if st not in ("open", "option_closed_perp_pending") or not pos.get("group_id"):
|
||||
return CloseResult(ok=False, detail="无持仓可平")
|
||||
|
||||
group_id = str(pos["group_id"])
|
||||
option_inst_id = str(pos["option_inst_id"])
|
||||
option_side = str(pos["option_side"])
|
||||
perp_side = str(pos["perp_side"])
|
||||
opt_qty = float(pos["option_qty_eth"])
|
||||
perp_qty = float(pos["perp_qty_eth"])
|
||||
opt_contracts = float(pos["option_qty_contracts"] or 0)
|
||||
client = self._client()
|
||||
is_expiry = reason == "expiry"
|
||||
fee_rate = self._fee_rate()
|
||||
pending_perp_only = st == "option_closed_perp_pending"
|
||||
|
||||
sess = get_session()
|
||||
snap = sess.snapshot()
|
||||
strike = self._group_strike(group_id, option_inst_id)
|
||||
spot = self._close_spot_px(snap)
|
||||
intrinsic = None
|
||||
if strike is not None and spot is not None:
|
||||
intrinsic = option_intrinsic(
|
||||
option_side=option_side, strike=float(strike), spot=float(spot)
|
||||
)
|
||||
|
||||
of_px = 0.0
|
||||
of_fee = 0.0
|
||||
of_slip = 0.0
|
||||
of_notional = 0.0
|
||||
|
||||
if pending_perp_only:
|
||||
# 期权已在上次成交并入账;只读上次平期权 fill
|
||||
prev = self.db.fetchone(
|
||||
"""SELECT fill_px, fee, notional, slip FROM fills
|
||||
WHERE group_id=? AND leg='option' AND action='close'
|
||||
ORDER BY id DESC LIMIT 1""",
|
||||
(group_id,),
|
||||
)
|
||||
if prev is None:
|
||||
return CloseResult(
|
||||
ok=False,
|
||||
detail="option_closed_perp_pending 缺期权平仓记录,请人工核对",
|
||||
)
|
||||
of_px = float(prev["fill_px"])
|
||||
of_fee = float(prev["fee"] or 0)
|
||||
of_notional = float(prev["notional"] or (of_px * opt_qty))
|
||||
of_slip = float(prev["slip"] or 0)
|
||||
elif is_expiry:
|
||||
if intrinsic is None:
|
||||
return CloseResult(ok=False, detail="到期结算失败:缺行权价或标的价")
|
||||
of = option_expiry_settle(
|
||||
intrinsic=float(intrinsic), qty_eth=opt_qty, fee_rate=fee_rate
|
||||
)
|
||||
of_px, of_fee, of_slip, of_notional = of.fill_px, of.fee, of.slip, of.notional
|
||||
else:
|
||||
try:
|
||||
opt_live = client.place_option_market(
|
||||
symbol=option_inst_id,
|
||||
side="SELL",
|
||||
quantity=opt_contracts,
|
||||
reduce_only=True,
|
||||
)
|
||||
of_px = float(opt_live.avg_px)
|
||||
of_fee = float(opt_live.fee)
|
||||
of_notional = of_px * opt_qty
|
||||
except Exception as e:
|
||||
if not bypass_liquidity:
|
||||
return CloseResult(
|
||||
ok=False,
|
||||
detail=f"币安平期权失败: {e}",
|
||||
liquidity_wait=True,
|
||||
)
|
||||
return CloseResult(ok=False, detail=f"币安平期权失败: {e}")
|
||||
|
||||
# 期权已平:立刻落 pending,避免永续失败后重试再卖期权
|
||||
self._mark_option_closed_perp_pending(
|
||||
group_id=group_id,
|
||||
option_inst_id=option_inst_id,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=of_px,
|
||||
of_fee=of_fee,
|
||||
of_notional=of_notional,
|
||||
of_slip=of_slip,
|
||||
reason=reason,
|
||||
)
|
||||
pending_perp_only = True
|
||||
|
||||
try:
|
||||
if perp_side == "long":
|
||||
side, pos_side = "SELL", "LONG"
|
||||
else:
|
||||
side, pos_side = "BUY", "SHORT"
|
||||
perp_live = client.place_perp_market(
|
||||
symbol=s.perp_inst_id,
|
||||
side=side,
|
||||
qty_eth=perp_qty,
|
||||
position_side=pos_side,
|
||||
reduce_only=True,
|
||||
)
|
||||
pf_px = float(perp_live.avg_px)
|
||||
pf_fee = float(perp_live.fee)
|
||||
except Exception as e:
|
||||
return CloseResult(
|
||||
ok=False,
|
||||
detail=f"期权已平,永续待平(option_closed_perp_pending): {e}",
|
||||
)
|
||||
|
||||
return self._finalize_dual_close(
|
||||
pos=pos,
|
||||
group_id=group_id,
|
||||
option_inst_id=option_inst_id,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=of_px,
|
||||
of_fee=of_fee,
|
||||
of_slip=of_slip,
|
||||
of_notional=of_notional,
|
||||
pf_px=pf_px,
|
||||
pf_fee=pf_fee,
|
||||
reason=reason,
|
||||
option_fill_already_written=(
|
||||
st == "option_closed_perp_pending"
|
||||
or (pending_perp_only and not is_expiry)
|
||||
),
|
||||
skip_option_cash=(
|
||||
st == "option_closed_perp_pending"
|
||||
or (pending_perp_only and not is_expiry)
|
||||
),
|
||||
)
|
||||
|
||||
def _mark_option_closed_perp_pending(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
option_inst_id: str,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
of_notional: float,
|
||||
of_slip: float,
|
||||
reason: str,
|
||||
) -> None:
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN close option pending perp {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"close",
|
||||
"flat",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
of_slip,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"UPDATE positions SET status='option_closed_perp_pending' WHERE id=1"
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"UPDATE groups SET fees=COALESCE(fees,0)+?, note=? WHERE group_id=?",
|
||||
(of_fee, f"option_closed_perp_pending:{reason}", group_id),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
def _finalize_dual_close(
|
||||
self,
|
||||
*,
|
||||
pos: dict,
|
||||
group_id: str,
|
||||
option_inst_id: str,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
of_slip: float,
|
||||
of_notional: float,
|
||||
pf_px: float,
|
||||
pf_fee: float,
|
||||
reason: str,
|
||||
option_fill_already_written: bool,
|
||||
skip_option_cash: bool,
|
||||
) -> CloseResult:
|
||||
s = get_settings()
|
||||
perp_side = str(pos["perp_side"])
|
||||
perp_qty = float(pos["perp_qty_eth"])
|
||||
opt_entry = float(pos["option_entry_px"])
|
||||
perp_entry = float(pos["perp_entry_px"] or pf_px)
|
||||
opt_pnl = (of_px - opt_entry) * opt_qty
|
||||
if perp_side == "long":
|
||||
perp_pnl = (pf_px - perp_entry) * perp_qty
|
||||
else:
|
||||
perp_pnl = (perp_entry - pf_px) * perp_qty
|
||||
|
||||
if not skip_option_cash:
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN close option {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
perp_pnl - pf_fee,
|
||||
kind="close_perp",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN close perp {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
base_fees = float((g["fees"] if g else 0) or 0)
|
||||
fees = base_fees + (0.0 if skip_option_cash else of_fee) + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0) + (
|
||||
0.0 if option_fill_already_written else of_slip
|
||||
)
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
|
||||
with self.db._lock:
|
||||
if not option_fill_already_written:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"close",
|
||||
"flat",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
of_slip,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"perp",
|
||||
"close",
|
||||
"flat",
|
||||
s.perp_inst_id,
|
||||
perp_qty,
|
||||
None,
|
||||
pf_px,
|
||||
pf_px,
|
||||
pf_fee,
|
||||
0.0,
|
||||
pf_px * perp_qty,
|
||||
now + 1,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
fills = self.db._conn.execute(
|
||||
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC", (group_id,)
|
||||
).fetchall()
|
||||
summary = summarize_fills_pnl(list(fills))
|
||||
net = summary.get("net_pnl")
|
||||
if net is None:
|
||||
net = opt_pnl + perp_pnl - of_fee - pf_fee
|
||||
self.db._conn.execute(
|
||||
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?,
|
||||
fees=?, slip_cost=? WHERE group_id=?""",
|
||||
("closed", now, reason, float(net), fees, slip, group_id),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=NULL, perp_side=NULL, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=NULL, option_side=NULL, option_qty_eth=0, option_qty_contracts=0,
|
||||
option_entry_px=NULL, entry_index_px=NULL, initial_premium=0, status='flat'
|
||||
WHERE id=1"""
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
return CloseResult(
|
||||
ok=True,
|
||||
detail="closed_live_binance",
|
||||
data={"group_id": group_id, "reason": reason, "net_pnl": net, "exec_mode": "LIVE"},
|
||||
)
|
||||
|
||||
def close_perp_abandon_option(
|
||||
self, *, reason: str = "target_perp_only", require_deep_otm: bool = True
|
||||
) -> CloseResult:
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return CloseResult(ok=False, detail=err)
|
||||
if require_deep_otm and not self.option_is_deep_otm():
|
||||
return CloseResult(ok=False, detail="期权非远虚,应走双腿全平")
|
||||
|
||||
s = get_settings()
|
||||
pos = self.current_position()
|
||||
st = str(pos.get("status") or "")
|
||||
if st not in ("open", "option_closed_perp_pending") or not pos.get("group_id"):
|
||||
return CloseResult(ok=False, detail="无持仓可平")
|
||||
# 若期权已平只剩永续,走 close_group 续平即可
|
||||
if st == "option_closed_perp_pending":
|
||||
return self.close_group(reason=reason, bypass_liquidity=True)
|
||||
|
||||
group_id = str(pos["group_id"])
|
||||
perp_side = str(pos["perp_side"])
|
||||
perp_qty = float(pos["perp_qty_eth"])
|
||||
perp_entry = float(pos["perp_entry_px"])
|
||||
client = self._client()
|
||||
try:
|
||||
if perp_side == "long":
|
||||
side, pos_side = "SELL", "LONG"
|
||||
else:
|
||||
side, pos_side = "BUY", "SHORT"
|
||||
perp_live = client.place_perp_market(
|
||||
symbol=s.perp_inst_id,
|
||||
side=side,
|
||||
qty_eth=perp_qty,
|
||||
position_side=pos_side,
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e:
|
||||
return CloseResult(ok=False, detail=f"币安平永续失败: {e}")
|
||||
|
||||
pf_px = float(perp_live.avg_px)
|
||||
pf_fee = float(perp_live.fee)
|
||||
if perp_side == "long":
|
||||
perp_pnl = (pf_px - perp_entry) * perp_qty
|
||||
else:
|
||||
perp_pnl = (perp_entry - pf_px) * perp_qty
|
||||
|
||||
self.ledger.apply_cash(
|
||||
perp_pnl - pf_fee,
|
||||
kind="close_perp",
|
||||
group_id=group_id,
|
||||
note=f"LIVE-BN close perp abandon option {reason}",
|
||||
)
|
||||
|
||||
option_inst_id = str(pos["option_inst_id"])
|
||||
option_side = str(pos["option_side"])
|
||||
strike = self._group_strike(group_id, option_inst_id)
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
expiry_ymd = str(g["expiry_ymd"]) if g and g["expiry_ymd"] else None
|
||||
expiry_ms = None
|
||||
if expiry_ymd:
|
||||
try:
|
||||
from ..exchange.expiry import expiry_ms_from_ymd
|
||||
|
||||
expiry_ms = int(expiry_ms_from_ymd(expiry_ymd))
|
||||
except Exception:
|
||||
expiry_ms = None
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
open_fees = float((g["fees"] if g else 0) or 0)
|
||||
fees = open_fees + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0)
|
||||
interim_net = perp_pnl - open_fees - pf_fee
|
||||
spot = self._close_spot_px(get_session().snapshot())
|
||||
|
||||
with self.db._lock:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"perp",
|
||||
"close",
|
||||
"flat",
|
||||
s.perp_inst_id,
|
||||
perp_qty,
|
||||
None,
|
||||
pf_px,
|
||||
pf_px,
|
||||
pf_fee,
|
||||
0.0,
|
||||
pf_px * perp_qty,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO residual_options(
|
||||
group_id, option_inst_id, option_side, option_qty_eth, option_qty_contracts,
|
||||
option_entry_px, strike, expiry_ymd, expiry_ms, entry_index_px,
|
||||
initial_premium, status, created_at_ms, note
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
option_inst_id,
|
||||
option_side,
|
||||
float(pos["option_qty_eth"]),
|
||||
float(pos["option_qty_contracts"] or 0),
|
||||
float(pos["option_entry_px"]),
|
||||
float(strike) if strike is not None else None,
|
||||
expiry_ymd,
|
||||
expiry_ms,
|
||||
float(pos["entry_index_px"] or 0),
|
||||
float(pos["initial_premium"] or 0),
|
||||
"pending",
|
||||
now,
|
||||
f"LIVE-BN abandoned after {reason}; spot={spot}",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE groups SET status=?, close_reason=?, realized_pnl=?,
|
||||
fees=?, slip_cost=?, note=?, exec_mode=? WHERE group_id=?""",
|
||||
(
|
||||
"option_residual",
|
||||
reason,
|
||||
interim_net,
|
||||
fees,
|
||||
slip,
|
||||
"LIVE-BN perp_closed; option residual until expiry",
|
||||
"LIVE",
|
||||
group_id,
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=NULL, perp_side=NULL, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=NULL, option_side=NULL, option_qty_eth=0, option_qty_contracts=0,
|
||||
option_entry_px=NULL, entry_index_px=NULL, initial_premium=0, status='flat'
|
||||
WHERE id=1"""
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
return CloseResult(
|
||||
ok=True,
|
||||
detail="perp_closed_option_residual_live_binance",
|
||||
data={"group_id": group_id, "reason": reason, "mode": "target_perp_only", "exec_mode": "LIVE"},
|
||||
)
|
||||
@@ -0,0 +1,237 @@
|
||||
"""币安私有交易:USDT-M 永续 (fapi) + 欧洲期权 (eapi)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
import logging
|
||||
import time
|
||||
from typing import Any
|
||||
from urllib.parse import urlencode
|
||||
|
||||
import httpx
|
||||
|
||||
from ..config import Settings, get_settings
|
||||
from ..exchange.okx.parse import safe_float
|
||||
from .okx_trade import LiveFill
|
||||
from .rate_limit import RateLimitError, get_throttle, parse_retry_after_header
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BinanceTradeClient:
|
||||
def __init__(self, settings: Settings | None = None) -> None:
|
||||
self.settings = settings or get_settings()
|
||||
proxy = (self.settings.binance_http_proxy or "").strip() or None
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "eth-hedge-live/0.1",
|
||||
"X-MBX-APIKEY": self.settings.binance_api_key or "",
|
||||
}
|
||||
self._fapi = httpx.Client(
|
||||
base_url=self.settings.binance_fapi_base.rstrip("/"),
|
||||
timeout=20.0,
|
||||
proxy=proxy,
|
||||
headers=headers,
|
||||
trust_env=False,
|
||||
)
|
||||
self._eapi = httpx.Client(
|
||||
base_url=self.settings.binance_eapi_base.rstrip("/"),
|
||||
timeout=20.0,
|
||||
proxy=proxy,
|
||||
headers=headers,
|
||||
trust_env=False,
|
||||
)
|
||||
self._hedge: bool | None = None
|
||||
self._fapi_throttle = get_throttle("binance_fapi_trade", min_interval_sec=1.0)
|
||||
self._eapi_throttle = get_throttle(
|
||||
"binance_eapi_trade",
|
||||
min_interval_sec=1.0,
|
||||
cooldown_429_sec=20.0,
|
||||
cooldown_418_sec=120.0,
|
||||
)
|
||||
|
||||
def close(self) -> None:
|
||||
self._fapi.close()
|
||||
self._eapi.close()
|
||||
|
||||
def _sign(self, params: dict[str, Any]) -> str:
|
||||
qs = urlencode(params, doseq=True)
|
||||
secret = (self.settings.binance_api_secret or "").encode("utf-8")
|
||||
return hmac.new(secret, qs.encode("utf-8"), hashlib.sha256).hexdigest()
|
||||
|
||||
def _throttle_for(self, client: httpx.Client):
|
||||
if client is self._eapi:
|
||||
return self._eapi_throttle
|
||||
return self._fapi_throttle
|
||||
|
||||
def _signed(
|
||||
self,
|
||||
client: httpx.Client,
|
||||
method: str,
|
||||
path: str,
|
||||
params: dict[str, Any] | None = None,
|
||||
) -> Any:
|
||||
throttle = self._throttle_for(client)
|
||||
throttle.before_request()
|
||||
p = dict(params or {})
|
||||
p["timestamp"] = int(time.time() * 1000)
|
||||
p["signature"] = self._sign(p)
|
||||
r = client.request(method.upper(), path, params=p)
|
||||
if r.status_code in (418, 429):
|
||||
ra = parse_retry_after_header(r.headers)
|
||||
throttle.mark_http(r.status_code, ra)
|
||||
raise RateLimitError(
|
||||
f"Binance {path} HTTP {r.status_code}: {r.text[:200]}",
|
||||
retry_after=throttle.remaining_cooldown(),
|
||||
)
|
||||
if r.status_code >= 400:
|
||||
raise RuntimeError(f"Binance {path} HTTP {r.status_code}: {r.text[:400]}")
|
||||
data = r.json()
|
||||
if isinstance(data, dict) and "code" in data and "orderId" not in data:
|
||||
code = data.get("code")
|
||||
try:
|
||||
code_i = int(code)
|
||||
except (TypeError, ValueError):
|
||||
code_i = None
|
||||
msg = str(data.get("msg") or "")
|
||||
# -1003 too many requests; -1015 too many orders
|
||||
if code_i in (-1003, -1015) or "too many" in msg.lower():
|
||||
throttle.mark_seconds(20.0)
|
||||
raise RateLimitError(
|
||||
f"Binance rate-limited code={code} msg={msg}",
|
||||
retry_after=throttle.remaining_cooldown(),
|
||||
)
|
||||
if code_i is not None and code_i != 0:
|
||||
raise RuntimeError(f"Binance error code={code} msg={msg}")
|
||||
if code_i is None:
|
||||
raise RuntimeError(f"Binance error code={code} msg={msg}")
|
||||
return data
|
||||
|
||||
def is_hedge_mode(self) -> bool:
|
||||
if self._hedge is not None:
|
||||
return self._hedge
|
||||
try:
|
||||
data = self._signed(self._fapi, "GET", "/fapi/v1/positionSide/dual")
|
||||
self._hedge = bool(data.get("dualSidePosition") in (True, "true", "True"))
|
||||
except Exception as e:
|
||||
logger.warning("binance hedge mode probe failed: %s; assume one-way", e)
|
||||
self._hedge = False
|
||||
return self._hedge
|
||||
|
||||
def place_perp_market(
|
||||
self,
|
||||
*,
|
||||
symbol: str,
|
||||
side: str, # BUY|SELL
|
||||
qty_eth: float,
|
||||
position_side: str | None = None, # LONG|SHORT|None
|
||||
reduce_only: bool = False,
|
||||
) -> LiveFill:
|
||||
# ETHUSDT 数量单位为 ETH
|
||||
qty = f"{float(qty_eth):.3f}".rstrip("0").rstrip(".")
|
||||
if not qty or qty == "0":
|
||||
qty = "0.001"
|
||||
params: dict[str, Any] = {
|
||||
"symbol": symbol,
|
||||
"side": side.upper(),
|
||||
"type": "MARKET",
|
||||
"quantity": qty,
|
||||
}
|
||||
hedge = self.is_hedge_mode()
|
||||
if hedge:
|
||||
ps = (position_side or ("LONG" if side.upper() == "BUY" else "SHORT")).upper()
|
||||
params["positionSide"] = ps
|
||||
elif reduce_only:
|
||||
params["reduceOnly"] = "true"
|
||||
data = self._signed(self._fapi, "POST", "/fapi/v1/order", params)
|
||||
return self._fill_from_fapi(symbol, data)
|
||||
|
||||
def _fill_from_fapi(self, symbol: str, data: dict[str, Any]) -> LiveFill:
|
||||
ord_id = str(data.get("orderId") or "")
|
||||
avg = safe_float(data.get("avgPrice"))
|
||||
sz = safe_float(data.get("executedQty"))
|
||||
if (not avg or avg <= 0) and ord_id:
|
||||
q = self._signed(
|
||||
self._fapi,
|
||||
"GET",
|
||||
"/fapi/v1/order",
|
||||
{"symbol": symbol, "orderId": ord_id},
|
||||
)
|
||||
avg = safe_float(q.get("avgPrice")) or avg
|
||||
sz = safe_float(q.get("executedQty")) or sz
|
||||
data = q
|
||||
if not avg or avg <= 0:
|
||||
raise RuntimeError(f"币安永续无成交均价: {data}")
|
||||
# 手续费:优先 cumCommission;否则用名义×费率估
|
||||
fee = abs(safe_float(data.get("cumCommission")) or 0.0)
|
||||
if fee <= 0:
|
||||
fee = float(avg) * float(sz or 0) * float(self.settings.fee_rate)
|
||||
return LiveFill(
|
||||
inst_id=symbol,
|
||||
side=str(data.get("side") or "").lower(),
|
||||
avg_px=float(avg),
|
||||
sz=float(sz or 0),
|
||||
fee=float(fee),
|
||||
ord_id=ord_id,
|
||||
raw=data if isinstance(data, dict) else {},
|
||||
)
|
||||
|
||||
def place_option_market(
|
||||
self,
|
||||
*,
|
||||
symbol: str,
|
||||
side: str, # BUY|SELL
|
||||
quantity: float,
|
||||
reduce_only: bool = False,
|
||||
) -> LiveFill:
|
||||
qty = str(int(round(quantity)))
|
||||
if qty == "0":
|
||||
qty = "1"
|
||||
params: dict[str, Any] = {
|
||||
"symbol": symbol,
|
||||
"side": side.upper(),
|
||||
"type": "MARKET",
|
||||
"quantity": qty,
|
||||
}
|
||||
if reduce_only:
|
||||
params["reduceOnly"] = "true"
|
||||
data = self._signed(self._eapi, "POST", "/eapi/v1/order", params)
|
||||
return self._fill_from_eapi(symbol, data)
|
||||
|
||||
def _fill_from_eapi(self, symbol: str, data: dict[str, Any]) -> LiveFill:
|
||||
ord_id = str(data.get("orderId") or data.get("id") or "")
|
||||
avg = safe_float(data.get("avgPrice")) or safe_float(data.get("price"))
|
||||
sz = safe_float(data.get("executedQty")) or safe_float(data.get("quantity"))
|
||||
if (not avg or avg <= 0) and ord_id:
|
||||
# 轮询几轮
|
||||
for _ in range(8):
|
||||
time.sleep(0.2)
|
||||
q = self._signed(
|
||||
self._eapi,
|
||||
"GET",
|
||||
"/eapi/v1/order",
|
||||
{"symbol": symbol, "orderId": ord_id},
|
||||
)
|
||||
avg = safe_float(q.get("avgPrice")) or safe_float(q.get("price"))
|
||||
sz = safe_float(q.get("executedQty")) or safe_float(q.get("quantity"))
|
||||
st = str(q.get("status") or "").upper()
|
||||
data = q
|
||||
if avg and avg > 0 and st in ("FILLED", "PARTIALLY_FILLED"):
|
||||
break
|
||||
if st in ("CANCELED", "REJECTED", "EXPIRED"):
|
||||
raise RuntimeError(f"币安期权订单失败 status={st} {q}")
|
||||
if not avg or avg <= 0:
|
||||
raise RuntimeError(f"币安期权无成交均价: {data}")
|
||||
fee = abs(safe_float(data.get("fee")) or 0.0)
|
||||
if fee <= 0:
|
||||
fee = float(avg) * float(sz or 0) * float(self.settings.fee_rate)
|
||||
return LiveFill(
|
||||
inst_id=symbol,
|
||||
side=str(data.get("side") or "").lower(),
|
||||
avg_px=float(avg),
|
||||
sz=float(sz or 0),
|
||||
fee=float(fee),
|
||||
ord_id=ord_id,
|
||||
raw=data if isinstance(data, dict) else {},
|
||||
)
|
||||
+406
-62
@@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from ..config import get_settings
|
||||
from ..env_store import live_ready
|
||||
@@ -54,9 +53,12 @@ class OkxLiveExecutor(Matcher):
|
||||
return OpenResult(ok=False, detail=err)
|
||||
|
||||
s = get_settings()
|
||||
pos = self.current_position()
|
||||
if pos.get("status") == "open" and pos.get("group_id"):
|
||||
return OpenResult(ok=False, detail="已有持仓组,请先平仓")
|
||||
if self.has_open_position():
|
||||
st = self.position_status()
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
detail=f"已有持仓/半仓状态({st}),请先修复或平仓",
|
||||
)
|
||||
|
||||
client = self._client()
|
||||
perp_qty = self.ledger.get_setting_float("perp_qty_eth", s.perp_qty_eth)
|
||||
@@ -76,7 +78,7 @@ class OkxLiveExecutor(Matcher):
|
||||
logger.exception("live open option failed")
|
||||
return OpenResult(ok=False, detail=f"实盘开期权失败: {e}")
|
||||
|
||||
# 永续:按仓位方向
|
||||
# 永续市价:按产品假设,失败原因实质为保证金不足 → 必须回滚期权
|
||||
try:
|
||||
ct_val = client.get_ct_val(s.perp_inst_id, inst_type="SWAP")
|
||||
perp_sz = max(1, int(round(perp_qty / ct_val)))
|
||||
@@ -92,7 +94,7 @@ class OkxLiveExecutor(Matcher):
|
||||
pos_side=pos_side,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("live open perp failed; attempting option close")
|
||||
logger.exception("live open perp failed (likely margin); rollback option")
|
||||
try:
|
||||
client.place_market(
|
||||
inst_id=option_inst_id,
|
||||
@@ -103,11 +105,30 @@ class OkxLiveExecutor(Matcher):
|
||||
)
|
||||
except Exception as e2:
|
||||
logger.exception("live option rollback failed: %s", e2)
|
||||
self._persist_half_open(
|
||||
group_id=group_id,
|
||||
bias=bias,
|
||||
option_side=option_side,
|
||||
perp_side=perp_side,
|
||||
option_inst_id=option_inst_id,
|
||||
entry_index_px=entry_index_px,
|
||||
strike=strike,
|
||||
expiry_ymd=expiry_ymd,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=float(opt_fill.avg_px),
|
||||
of_fee=float(opt_fill.fee),
|
||||
detail=f"保证金开永续失败且期权回滚失败: {e} / {e2}",
|
||||
)
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
detail=f"永续开仓失败且期权回滚失败: {e} / {e2}",
|
||||
group_id=group_id,
|
||||
detail=f"永续开仓失败(保证金)且期权回滚失败,已标记 half_open: {e} / {e2}",
|
||||
)
|
||||
return OpenResult(ok=False, detail=f"永续开仓失败,已尝试平期权: {e}")
|
||||
return OpenResult(
|
||||
ok=False,
|
||||
detail=f"永续开仓失败(多为保证金不足),已回滚期权: {e}",
|
||||
)
|
||||
|
||||
of_px = float(opt_fill.avg_px)
|
||||
pf_px = float(perp_fill_live.avg_px)
|
||||
@@ -117,21 +138,21 @@ class OkxLiveExecutor(Matcher):
|
||||
of_notional = of_px * opt_qty
|
||||
pf_notional = pf_px * perp_qty
|
||||
|
||||
try:
|
||||
self.ledger.apply_cash(
|
||||
-(of_notional + of_fee),
|
||||
kind="open_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE open option {group_id}",
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
-pf_fee,
|
||||
kind="open_perp_fee",
|
||||
group_id=group_id,
|
||||
note=f"LIVE open perp {group_id}",
|
||||
)
|
||||
except RuntimeError as e:
|
||||
return OpenResult(ok=False, detail=str(e))
|
||||
# LIVE:交易所已成交,本地账本允许透支镜像,禁止因账本拒记导致「交易所有仓、DB 空」
|
||||
self.ledger.apply_cash(
|
||||
-(of_notional + of_fee),
|
||||
kind="open_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE open option {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
-pf_fee,
|
||||
kind="open_perp_fee",
|
||||
group_id=group_id,
|
||||
note=f"LIVE open perp {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
@@ -238,6 +259,192 @@ class OkxLiveExecutor(Matcher):
|
||||
},
|
||||
)
|
||||
|
||||
def _persist_half_open(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
bias: str,
|
||||
option_side: str,
|
||||
perp_side: str,
|
||||
option_inst_id: str,
|
||||
entry_index_px: float,
|
||||
strike: float | None,
|
||||
expiry_ymd: str | None,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
detail: str,
|
||||
) -> None:
|
||||
"""期权已成交、永续未开且回滚失败 → 落 half_open,禁止新开,待 repair。"""
|
||||
s = get_settings()
|
||||
initial_premium = of_px * opt_qty
|
||||
self.ledger.apply_cash(
|
||||
-(of_px * opt_qty + of_fee),
|
||||
kind="open_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE half_open option {group_id}",
|
||||
allow_negative=True,
|
||||
)
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
existing = self.db._conn.execute(
|
||||
"SELECT group_id FROM groups WHERE group_id=?", (group_id,)
|
||||
).fetchone()
|
||||
if existing is None:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO groups(
|
||||
group_id, status, bias, option_side, perp_side, option_inst_id, perp_inst_id,
|
||||
strike, expiry_ymd, entry_index_px, initial_premium, open_at_ms, fees, slip_cost,
|
||||
exec_mode, note
|
||||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"half_open",
|
||||
bias,
|
||||
option_side,
|
||||
perp_side,
|
||||
option_inst_id,
|
||||
s.perp_inst_id,
|
||||
strike,
|
||||
expiry_ymd,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
now,
|
||||
of_fee,
|
||||
0.0,
|
||||
"LIVE",
|
||||
detail[:200],
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"open",
|
||||
"long",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
0.0,
|
||||
of_px * opt_qty,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=?, perp_side=?, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=?, option_side=?, option_qty_eth=?, option_qty_contracts=?,
|
||||
option_entry_px=?, entry_index_px=?, initial_premium=?, status='half_open'
|
||||
WHERE id=1""",
|
||||
(
|
||||
group_id,
|
||||
perp_side,
|
||||
option_inst_id,
|
||||
option_side,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
entry_index_px,
|
||||
initial_premium,
|
||||
),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
def repair_half_open(self) -> CloseResult:
|
||||
"""卖出 half_open 残留期权,清本地状态。"""
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return CloseResult(ok=False, detail=err)
|
||||
pos = self.current_position()
|
||||
if pos.get("status") != "half_open":
|
||||
return CloseResult(ok=False, detail="非 half_open 状态")
|
||||
group_id = str(pos.get("group_id") or "")
|
||||
option_inst_id = str(pos.get("option_inst_id") or "")
|
||||
opt_contracts = float(pos.get("option_qty_contracts") or 0)
|
||||
opt_qty = float(pos.get("option_qty_eth") or 0)
|
||||
if not option_inst_id or opt_contracts <= 0:
|
||||
return CloseResult(ok=False, detail="half_open 缺期权合约信息")
|
||||
client = self._client()
|
||||
try:
|
||||
opt_live = client.place_market(
|
||||
inst_id=option_inst_id,
|
||||
side="sell",
|
||||
sz=str(int(round(opt_contracts))),
|
||||
td_mode="cash",
|
||||
reduce_only=True,
|
||||
)
|
||||
except Exception as e:
|
||||
return CloseResult(ok=False, detail=f"half_open 平期权失败: {e}")
|
||||
of_px = float(opt_live.avg_px)
|
||||
of_fee = float(opt_live.fee)
|
||||
of_notional = of_px * opt_qty
|
||||
opt_entry = float(pos.get("option_entry_px") or of_px)
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id or None,
|
||||
note="LIVE repair half_open",
|
||||
allow_negative=True,
|
||||
)
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
if group_id:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"close",
|
||||
"flat",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
0.0,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
opt_pnl = (of_px - opt_entry) * opt_qty - of_fee
|
||||
self.db._conn.execute(
|
||||
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?, note=?
|
||||
WHERE group_id=?""",
|
||||
(
|
||||
"closed",
|
||||
now,
|
||||
"half_open_repair",
|
||||
float(opt_pnl),
|
||||
"repaired half_open",
|
||||
group_id,
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
group_id=NULL, perp_side=NULL, perp_qty_eth=0, perp_entry_px=NULL,
|
||||
option_inst_id=NULL, option_side=NULL, option_qty_eth=0, option_qty_contracts=0,
|
||||
option_entry_px=NULL, entry_index_px=NULL, initial_premium=0, status='flat'
|
||||
WHERE id=1"""
|
||||
)
|
||||
self.db._conn.commit()
|
||||
return CloseResult(
|
||||
ok=True,
|
||||
detail="half_open_repaired",
|
||||
data={"group_id": group_id, "exec_mode": "LIVE"},
|
||||
)
|
||||
|
||||
def close_group(self, *, reason: str, bypass_liquidity: bool = False) -> CloseResult:
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
@@ -245,7 +452,10 @@ class OkxLiveExecutor(Matcher):
|
||||
|
||||
s = get_settings()
|
||||
pos = self.current_position()
|
||||
if pos.get("status") != "open" or not pos.get("group_id"):
|
||||
st = str(pos.get("status") or "")
|
||||
if st == "half_open":
|
||||
return self.repair_half_open()
|
||||
if st not in ("open", "option_closed_perp_pending") or not pos.get("group_id"):
|
||||
return CloseResult(ok=False, detail="无持仓可平")
|
||||
|
||||
group_id = str(pos["group_id"])
|
||||
@@ -258,6 +468,7 @@ class OkxLiveExecutor(Matcher):
|
||||
client = self._client()
|
||||
is_expiry = reason == "expiry"
|
||||
fee_rate = self._fee_rate()
|
||||
pending_perp_only = st == "option_closed_perp_pending"
|
||||
|
||||
sess = get_session()
|
||||
snap = sess.snapshot()
|
||||
@@ -274,7 +485,24 @@ class OkxLiveExecutor(Matcher):
|
||||
of_slip = 0.0
|
||||
of_notional = 0.0
|
||||
|
||||
if is_expiry:
|
||||
if pending_perp_only:
|
||||
# 期权已在上次成交并入账;只读上次平期权 fill
|
||||
prev = self.db.fetchone(
|
||||
"""SELECT fill_px, fee, notional, slip FROM fills
|
||||
WHERE group_id=? AND leg='option' AND action='close'
|
||||
ORDER BY id DESC LIMIT 1""",
|
||||
(group_id,),
|
||||
)
|
||||
if prev is None:
|
||||
return CloseResult(
|
||||
ok=False,
|
||||
detail="option_closed_perp_pending 缺期权平仓记录,请人工核对",
|
||||
)
|
||||
of_px = float(prev["fill_px"])
|
||||
of_fee = float(prev["fee"] or 0)
|
||||
of_notional = float(prev["notional"] or (of_px * opt_qty))
|
||||
of_slip = float(prev["slip"] or 0)
|
||||
elif is_expiry:
|
||||
if intrinsic is None:
|
||||
return CloseResult(ok=False, detail="到期结算失败:缺行权价或标的价")
|
||||
of = option_expiry_settle(
|
||||
@@ -302,6 +530,20 @@ class OkxLiveExecutor(Matcher):
|
||||
)
|
||||
return CloseResult(ok=False, detail=f"实盘平期权失败: {e}")
|
||||
|
||||
# 期权已平:立刻落 pending,避免永续失败后重试再卖期权
|
||||
self._mark_option_closed_perp_pending(
|
||||
group_id=group_id,
|
||||
option_inst_id=option_inst_id,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=of_px,
|
||||
of_fee=of_fee,
|
||||
of_notional=of_notional,
|
||||
of_slip=of_slip,
|
||||
reason=reason,
|
||||
)
|
||||
pending_perp_only = True
|
||||
|
||||
try:
|
||||
ct_val = client.get_ct_val(s.perp_inst_id, inst_type="SWAP")
|
||||
perp_sz = max(1, int(round(perp_qty / ct_val)))
|
||||
@@ -320,35 +562,55 @@ class OkxLiveExecutor(Matcher):
|
||||
pf_px = float(perp_live.avg_px)
|
||||
pf_fee = float(perp_live.fee)
|
||||
except Exception as e:
|
||||
return CloseResult(ok=False, detail=f"期权已平但永续平仓失败: {e}")
|
||||
return CloseResult(
|
||||
ok=False,
|
||||
detail=f"期权已平,永续待平(option_closed_perp_pending): {e}",
|
||||
)
|
||||
|
||||
opt_entry = float(pos["option_entry_px"])
|
||||
perp_entry = float(pos["perp_entry_px"])
|
||||
opt_pnl = (of_px - opt_entry) * opt_qty
|
||||
if perp_side == "long":
|
||||
perp_pnl = (pf_px - perp_entry) * perp_qty
|
||||
else:
|
||||
perp_pnl = (perp_entry - pf_px) * perp_qty
|
||||
return self._finalize_dual_close(
|
||||
pos=pos,
|
||||
group_id=group_id,
|
||||
option_inst_id=option_inst_id,
|
||||
opt_qty=opt_qty,
|
||||
opt_contracts=opt_contracts,
|
||||
of_px=of_px,
|
||||
of_fee=of_fee,
|
||||
of_slip=of_slip,
|
||||
of_notional=of_notional,
|
||||
pf_px=pf_px,
|
||||
pf_fee=pf_fee,
|
||||
reason=reason,
|
||||
option_fill_already_written=(
|
||||
st == "option_closed_perp_pending"
|
||||
or (pending_perp_only and not is_expiry)
|
||||
),
|
||||
skip_option_cash=(
|
||||
st == "option_closed_perp_pending"
|
||||
or (pending_perp_only and not is_expiry)
|
||||
),
|
||||
)
|
||||
|
||||
def _mark_option_closed_perp_pending(
|
||||
self,
|
||||
*,
|
||||
group_id: str,
|
||||
option_inst_id: str,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
of_notional: float,
|
||||
of_slip: float,
|
||||
reason: str,
|
||||
) -> None:
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE close option {reason}",
|
||||
note=f"LIVE close option pending perp {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
perp_pnl - pf_fee,
|
||||
kind="close_perp",
|
||||
group_id=group_id,
|
||||
note=f"LIVE close perp {reason}",
|
||||
)
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
fees = float((g["fees"] if g else 0) or 0) + of_fee + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0) + of_slip
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
|
||||
with self.db._lock:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
@@ -371,6 +633,92 @@ class OkxLiveExecutor(Matcher):
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"UPDATE positions SET status='option_closed_perp_pending' WHERE id=1"
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"UPDATE groups SET fees=COALESCE(fees,0)+?, note=? WHERE group_id=?",
|
||||
(of_fee, f"option_closed_perp_pending:{reason}", group_id),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
|
||||
def _finalize_dual_close(
|
||||
self,
|
||||
*,
|
||||
pos: dict,
|
||||
group_id: str,
|
||||
option_inst_id: str,
|
||||
opt_qty: float,
|
||||
opt_contracts: float,
|
||||
of_px: float,
|
||||
of_fee: float,
|
||||
of_slip: float,
|
||||
of_notional: float,
|
||||
pf_px: float,
|
||||
pf_fee: float,
|
||||
reason: str,
|
||||
option_fill_already_written: bool,
|
||||
skip_option_cash: bool,
|
||||
) -> CloseResult:
|
||||
s = get_settings()
|
||||
perp_side = str(pos["perp_side"])
|
||||
perp_qty = float(pos["perp_qty_eth"])
|
||||
opt_entry = float(pos["option_entry_px"])
|
||||
perp_entry = float(pos["perp_entry_px"] or pf_px)
|
||||
opt_pnl = (of_px - opt_entry) * opt_qty
|
||||
if perp_side == "long":
|
||||
perp_pnl = (pf_px - perp_entry) * perp_qty
|
||||
else:
|
||||
perp_pnl = (perp_entry - pf_px) * perp_qty
|
||||
|
||||
if not skip_option_cash:
|
||||
self.ledger.apply_cash(
|
||||
of_notional - of_fee,
|
||||
kind="close_option",
|
||||
group_id=group_id,
|
||||
note=f"LIVE close option {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
self.ledger.apply_cash(
|
||||
perp_pnl - pf_fee,
|
||||
kind="close_perp",
|
||||
group_id=group_id,
|
||||
note=f"LIVE close perp {reason}",
|
||||
allow_negative=True,
|
||||
)
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
base_fees = float((g["fees"] if g else 0) or 0)
|
||||
fees = base_fees + (0.0 if skip_option_cash else of_fee) + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0) + (
|
||||
0.0 if option_fill_already_written else of_slip
|
||||
)
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
|
||||
with self.db._lock:
|
||||
if not option_fill_already_written:
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
group_id,
|
||||
"option",
|
||||
"close",
|
||||
"flat",
|
||||
option_inst_id,
|
||||
opt_qty,
|
||||
opt_contracts,
|
||||
of_px,
|
||||
of_px,
|
||||
of_fee,
|
||||
of_slip,
|
||||
of_notional,
|
||||
now,
|
||||
"LIVE",
|
||||
),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""INSERT INTO fills(group_id, leg, action, side, inst_id, qty_eth, qty_contracts,
|
||||
base_px, fill_px, fee, slip, notional, ts_ms, exec_mode)
|
||||
@@ -419,18 +767,23 @@ class OkxLiveExecutor(Matcher):
|
||||
data={"group_id": group_id, "reason": reason, "net_pnl": net, "exec_mode": "LIVE"},
|
||||
)
|
||||
|
||||
def close_perp_abandon_option(self, *, reason: str = "target_perp_only") -> CloseResult:
|
||||
def close_perp_abandon_option(
|
||||
self, *, reason: str = "target_perp_only", require_deep_otm: bool = True
|
||||
) -> CloseResult:
|
||||
err = self._guard_live()
|
||||
if err:
|
||||
return CloseResult(ok=False, detail=err)
|
||||
# 先校验远虚,再实盘只平永续,其余写入复用父类逻辑的简化版:
|
||||
if not self.option_is_deep_otm():
|
||||
if require_deep_otm and not self.option_is_deep_otm():
|
||||
return CloseResult(ok=False, detail="期权非远虚,应走双腿全平")
|
||||
|
||||
s = get_settings()
|
||||
pos = self.current_position()
|
||||
if pos.get("status") != "open" or not pos.get("group_id"):
|
||||
st = str(pos.get("status") or "")
|
||||
if st not in ("open", "option_closed_perp_pending") or not pos.get("group_id"):
|
||||
return CloseResult(ok=False, detail="无持仓可平")
|
||||
# 若期权已平只剩永续,走 close_group 续平即可
|
||||
if st == "option_closed_perp_pending":
|
||||
return self.close_group(reason=reason, bypass_liquidity=True)
|
||||
|
||||
group_id = str(pos["group_id"])
|
||||
perp_side = str(pos["perp_side"])
|
||||
@@ -567,17 +920,6 @@ class OkxLiveExecutor(Matcher):
|
||||
)
|
||||
|
||||
|
||||
class BinanceLiveStub(Matcher):
|
||||
def open_group(self, **kwargs: Any) -> OpenResult: # type: ignore[override]
|
||||
return OpenResult(ok=False, detail="币安实盘下单尚未接入,请使用 OKX 或切回 SIM")
|
||||
|
||||
def close_group(self, **kwargs: Any) -> CloseResult: # type: ignore[override]
|
||||
return CloseResult(ok=False, detail="币安实盘下单尚未接入,请使用 OKX 或切回 SIM")
|
||||
|
||||
def close_perp_abandon_option(self, **kwargs: Any) -> CloseResult: # type: ignore[override]
|
||||
return CloseResult(ok=False, detail="币安实盘下单尚未接入,请使用 OKX 或切回 SIM")
|
||||
|
||||
|
||||
def get_executor(db=None) -> Matcher:
|
||||
"""按 MODE + 交易所返回执行器。"""
|
||||
from ..models.db import get_db
|
||||
@@ -588,5 +930,7 @@ def get_executor(db=None) -> Matcher:
|
||||
return Matcher(database)
|
||||
ex = load_runtime_settings().exchange
|
||||
if ex == "binance":
|
||||
return BinanceLiveStub(database)
|
||||
from .binance_executor import BinanceLiveExecutor
|
||||
|
||||
return BinanceLiveExecutor(database)
|
||||
return OkxLiveExecutor(database)
|
||||
|
||||
@@ -15,6 +15,7 @@ import httpx
|
||||
|
||||
from ..config import Settings, get_settings
|
||||
from ..exchange.okx.parse import safe_float
|
||||
from .rate_limit import RateLimitError, get_throttle, parse_retry_after_header
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -41,6 +42,7 @@ class OkxTradeClient:
|
||||
headers={"Accept": "application/json", "User-Agent": "eth-hedge-live/0.1"},
|
||||
)
|
||||
self._ct_val_cache: dict[str, float] = {}
|
||||
self._throttle = get_throttle("okx_trade", min_interval_sec=1.0)
|
||||
|
||||
def close(self) -> None:
|
||||
self._client.close()
|
||||
@@ -70,6 +72,7 @@ class OkxTradeClient:
|
||||
def _request(
|
||||
self, method: str, path: str, body: dict[str, Any] | None = None
|
||||
) -> list[dict[str, Any]]:
|
||||
self._throttle.before_request()
|
||||
payload = "" if body is None else json.dumps(body, separators=(",", ":"))
|
||||
ts = self._ts()
|
||||
sign = self._sign(ts, method, path, payload)
|
||||
@@ -78,11 +81,31 @@ class OkxTradeClient:
|
||||
r = self._client.get(path, headers=headers)
|
||||
else:
|
||||
r = self._client.request(method.upper(), path, content=payload, headers=headers)
|
||||
r.raise_for_status()
|
||||
if r.status_code in (418, 429):
|
||||
ra = parse_retry_after_header(r.headers)
|
||||
self._throttle.mark_http(r.status_code, ra)
|
||||
raise RateLimitError(
|
||||
f"OKX HTTP {r.status_code}: {r.text[:200]}",
|
||||
retry_after=self._throttle.remaining_cooldown(),
|
||||
)
|
||||
try:
|
||||
r.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
raise RuntimeError(f"OKX HTTP {r.status_code}: {r.text[:300]}") from e
|
||||
data = r.json()
|
||||
if str(data.get("code")) != "0":
|
||||
code = str(data.get("code") or "")
|
||||
msg = str(data.get("msg") or "")
|
||||
# OKX 业务层频率类错误
|
||||
if code != "0":
|
||||
low = f"{code} {msg}".lower()
|
||||
if code in ("50011", "50061") or "too many" in low or "频率" in msg:
|
||||
self._throttle.mark_seconds(20.0)
|
||||
raise RateLimitError(
|
||||
f"OKX trade rate-limited code={code} msg={msg}",
|
||||
retry_after=self._throttle.remaining_cooldown(),
|
||||
)
|
||||
raise RuntimeError(
|
||||
f"OKX trade error code={data.get('code')} msg={data.get('msg')} data={data.get('data')}"
|
||||
f"OKX trade error code={code} msg={msg} data={data.get('data')}"
|
||||
)
|
||||
rows = data.get("data") or []
|
||||
return [x for x in rows if isinstance(x, dict)]
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
"""实盘交易限流:私有 REST 冷却 + 失败退避。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_DEFAULT_429_SEC = 20.0
|
||||
_DEFAULT_418_SEC = 120.0
|
||||
_INTERVAL_MIN = 0.2
|
||||
_INTERVAL_MAX = 30.0
|
||||
|
||||
|
||||
def resolve_live_order_interval_sec() -> float:
|
||||
"""读取前端可配的 LIVE 下单最小间隔(秒),默认 1。"""
|
||||
try:
|
||||
from ..config import get_settings
|
||||
from ..models.db import get_db
|
||||
|
||||
s = get_settings()
|
||||
default = float(s.live_order_interval_sec)
|
||||
raw = get_db().get_setting("live_order_interval_sec", str(default))
|
||||
v = float(raw if raw not in (None, "") else default)
|
||||
if v != v: # NaN
|
||||
return 1.0
|
||||
return max(_INTERVAL_MIN, min(_INTERVAL_MAX, v))
|
||||
except Exception:
|
||||
return 1.0
|
||||
|
||||
|
||||
class RateLimitError(RuntimeError):
|
||||
"""处于限流/冷却中,调用方应退避,勿立即重试下单。"""
|
||||
|
||||
def __init__(self, message: str, *, retry_after: float = 0.0) -> None:
|
||||
super().__init__(message)
|
||||
self.retry_after = float(retry_after)
|
||||
|
||||
|
||||
class TradeThrottle:
|
||||
"""按通道节流:最小间隔 + 418/429 冷却。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
*,
|
||||
min_interval_sec: float = 1.0,
|
||||
cooldown_429_sec: float = _DEFAULT_429_SEC,
|
||||
cooldown_418_sec: float = _DEFAULT_418_SEC,
|
||||
) -> None:
|
||||
self.name = name
|
||||
self.min_interval_sec = float(min_interval_sec)
|
||||
self.cooldown_429_sec = float(cooldown_429_sec)
|
||||
self.cooldown_418_sec = float(cooldown_418_sec)
|
||||
self._lock = threading.Lock()
|
||||
self._last_at = 0.0
|
||||
self._cool_until = 0.0
|
||||
|
||||
def remaining_cooldown(self) -> float:
|
||||
with self._lock:
|
||||
return max(0.0, self._cool_until - time.monotonic())
|
||||
|
||||
def before_request(self) -> None:
|
||||
"""请求前调用:冷却中抛 RateLimitError;否则等待最小间隔(可读设置)。"""
|
||||
interval = resolve_live_order_interval_sec()
|
||||
with self._lock:
|
||||
self.min_interval_sec = interval
|
||||
now = time.monotonic()
|
||||
if now < self._cool_until:
|
||||
left = self._cool_until - now
|
||||
raise RateLimitError(
|
||||
f"{self.name} rate-limit cooldown {left:.1f}s",
|
||||
retry_after=left,
|
||||
)
|
||||
gap = now - self._last_at
|
||||
wait = interval - gap
|
||||
if wait > 0:
|
||||
time.sleep(wait)
|
||||
with self._lock:
|
||||
self._last_at = time.monotonic()
|
||||
|
||||
def mark_http(self, status_code: int, retry_after: float | None = None) -> None:
|
||||
if status_code not in (418, 429):
|
||||
return
|
||||
if status_code == 418:
|
||||
wait = self.cooldown_418_sec
|
||||
else:
|
||||
wait = float(retry_after) if retry_after and retry_after > 0 else self.cooldown_429_sec
|
||||
wait = max(wait, self.cooldown_429_sec)
|
||||
with self._lock:
|
||||
self._cool_until = time.monotonic() + wait
|
||||
logger.warning("%s HTTP %s → cooldown %.0fs", self.name, status_code, wait)
|
||||
|
||||
def mark_seconds(self, seconds: float) -> None:
|
||||
wait = max(1.0, float(seconds))
|
||||
with self._lock:
|
||||
self._cool_until = max(self._cool_until, time.monotonic() + wait)
|
||||
logger.warning("%s cooldown %.0fs (manual)", self.name, wait)
|
||||
|
||||
|
||||
_THROTTLES: dict[str, TradeThrottle] = {}
|
||||
_THROTTLES_LOCK = threading.Lock()
|
||||
|
||||
|
||||
def get_throttle(name: str, **kwargs: Any) -> TradeThrottle:
|
||||
with _THROTTLES_LOCK:
|
||||
t = _THROTTLES.get(name)
|
||||
if t is None:
|
||||
t = TradeThrottle(name, **kwargs)
|
||||
_THROTTLES[name] = t
|
||||
return t
|
||||
|
||||
|
||||
def is_rate_limit_error(exc: BaseException | str) -> bool:
|
||||
if isinstance(exc, RateLimitError):
|
||||
return True
|
||||
text = str(exc).lower()
|
||||
needles = (
|
||||
"429",
|
||||
"418",
|
||||
"rate limit",
|
||||
"rate-limit",
|
||||
"ratelimit",
|
||||
"too many request",
|
||||
"cooldown",
|
||||
"banned",
|
||||
"frequency",
|
||||
"请求过于频繁",
|
||||
"超出频率",
|
||||
)
|
||||
return any(n in text for n in needles)
|
||||
|
||||
|
||||
def parse_retry_after_header(headers: Any) -> float | None:
|
||||
try:
|
||||
raw = headers.get("Retry-After") if headers is not None else None
|
||||
if raw is None:
|
||||
return None
|
||||
return float(raw)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
class LiveRetryGate:
|
||||
"""引擎侧失败退避:避免 half_open / pending / liquidity 每秒砸单。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
base_sec: float = 2.0,
|
||||
max_sec: float = 60.0,
|
||||
rate_limit_min_sec: float = 20.0,
|
||||
trip_after: int = 12,
|
||||
trip_cooldown_sec: float = 180.0,
|
||||
) -> None:
|
||||
self.base_sec = float(base_sec)
|
||||
self.max_sec = float(max_sec)
|
||||
self.rate_limit_min_sec = float(rate_limit_min_sec)
|
||||
self.trip_after = int(trip_after)
|
||||
self.trip_cooldown_sec = float(trip_cooldown_sec)
|
||||
self._fails: dict[str, int] = {}
|
||||
self._next_at: dict[str, float] = {}
|
||||
|
||||
def allow(self, key: str) -> tuple[bool, float]:
|
||||
"""返回 (可否执行, 剩余等待秒)。"""
|
||||
left = max(0.0, self._next_at.get(key, 0.0) - time.monotonic())
|
||||
return left <= 0.0, left
|
||||
|
||||
def success(self, key: str) -> None:
|
||||
self._fails.pop(key, None)
|
||||
self._next_at.pop(key, None)
|
||||
|
||||
def fail(self, key: str, *, rate_limited: bool = False) -> float:
|
||||
n = int(self._fails.get(key, 0)) + 1
|
||||
self._fails[key] = n
|
||||
if rate_limited:
|
||||
delay = max(self.rate_limit_min_sec, self.rate_limit_min_sec * (1.5 ** min(n - 1, 4)))
|
||||
delay = min(delay, 120.0)
|
||||
elif n >= self.trip_after:
|
||||
delay = self.trip_cooldown_sec
|
||||
logger.error(
|
||||
"live retry gate tripped key=%s fails=%s cooldown=%.0fs",
|
||||
key,
|
||||
n,
|
||||
delay,
|
||||
)
|
||||
else:
|
||||
delay = min(self.max_sec, self.base_sec * (2 ** min(n - 1, 5)))
|
||||
self._next_at[key] = time.monotonic() + delay
|
||||
return delay
|
||||
|
||||
def fails(self, key: str) -> int:
|
||||
return int(self._fails.get(key, 0))
|
||||
Reference in New Issue
Block a user