Survive Binance eapi 418/429 with cooldown, soft start, and no ticker raise.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -230,8 +230,15 @@ class StrategyEngine:
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as e:
|
||||
err = str(e)
|
||||
# 限流时勿刷屏;拉长休眠给 eapi 冷却
|
||||
if "418" in err or "429" in err or "cooldown" in err.lower():
|
||||
logger.warning("strategy tick rate-limited: %s", err[:200])
|
||||
self._set_state(last_error="币安期权接口限流,稍后自动重试")
|
||||
await asyncio.sleep(15)
|
||||
continue
|
||||
logger.exception("strategy tick failed")
|
||||
self._set_state(last_error=str(e))
|
||||
self._set_state(last_error=err)
|
||||
await asyncio.sleep(1)
|
||||
|
||||
async def _tick_async(self) -> None:
|
||||
|
||||
@@ -112,7 +112,11 @@ class StrategySession:
|
||||
return
|
||||
self._started = True
|
||||
await self.ex.start()
|
||||
await asyncio.to_thread(self.align_instruments)
|
||||
try:
|
||||
await asyncio.to_thread(self.align_instruments)
|
||||
except Exception as e:
|
||||
# eapi 418/429 时允许先起会话,后续 refresh 再对齐
|
||||
logger.warning("initial ATM align failed (will retry): %s", e)
|
||||
await self.ex.resubscribe(
|
||||
[
|
||||
self.settings.perp_inst_id,
|
||||
@@ -205,6 +209,13 @@ class StrategySession:
|
||||
put_bids, put_asks, _ = self.ex.fetch_book(pair.put_inst_id, depth=5)
|
||||
call_ask = call_asks[0].px if call_asks else None
|
||||
put_ask = put_asks[0].px if put_asks else None
|
||||
# REST 被限流时回退 WS/缓存盘口
|
||||
if call_ask is None:
|
||||
cq = self.ex.quote(pair.call_inst_id)
|
||||
call_ask = cq.ask if cq else None
|
||||
if put_ask is None:
|
||||
pq = self.ex.quote(pair.put_inst_id)
|
||||
put_ask = pq.ask if pq else None
|
||||
sig = decide(
|
||||
call_ask,
|
||||
put_ask,
|
||||
@@ -335,7 +346,7 @@ class StrategySession:
|
||||
|
||||
async def _refresh_loop(self) -> None:
|
||||
while True:
|
||||
await asyncio.sleep(30)
|
||||
await asyncio.sleep(30 if self._pair is not None else 10)
|
||||
try:
|
||||
idx = await asyncio.to_thread(
|
||||
self.ex.fetch_index, self.settings.index_inst_id
|
||||
@@ -346,7 +357,10 @@ class StrategySession:
|
||||
)
|
||||
if mark:
|
||||
self.ex.set_mark_px(self.settings.perp_inst_id, mark)
|
||||
await self.ensure_atm_async(force=False)
|
||||
if self._pair is None:
|
||||
await self.ensure_atm_async(force=True)
|
||||
else:
|
||||
await self.ensure_atm_async(force=False)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user