fix: resolve 4 critical bugs from code review

1. Margin ratio calculated on per_symbol_balance instead of total balance
   — previously amplified margin reduction by num_symbols factor
2. Replace Algo Order API (algoType=CONDITIONAL) with standard
   futures_create_order for SL/TP — algo API is for VP/TWAP, not
   conditional orders; SL/TP may have silently failed
3. Fallback PnL (SYNC close) now sums all recent income rows instead
   of using only the last entry — prevents daily_pnl corruption in
   multi-fill scenarios
4. Explicit state transition in _close_and_reenter — clear local
   position state after close order to prevent race with User Data
   Stream callback on position count

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
21in7
2026-03-16 22:39:51 +09:00
parent b188607d58
commit 8803c71bf9
3 changed files with 265 additions and 53 deletions

View File

@@ -91,8 +91,6 @@ class BinanceFuturesClient:
return float(b["balance"])
return 0.0
_ALGO_ORDER_TYPES = {"STOP_MARKET", "TAKE_PROFIT_MARKET", "STOP", "TAKE_PROFIT", "TRAILING_STOP_MARKET"}
async def place_order(
self,
side: str,
@@ -104,15 +102,6 @@ class BinanceFuturesClient:
) -> dict:
loop = asyncio.get_event_loop()
if order_type in self._ALGO_ORDER_TYPES:
return await self._place_algo_order(
side=side,
quantity=quantity,
order_type=order_type,
stop_price=stop_price,
reduce_only=reduce_only,
)
params = dict(
symbol=self.symbol,
side=side,
@@ -133,34 +122,6 @@ class BinanceFuturesClient:
logger.error(f"주문 실패: {e}")
raise
async def _place_algo_order(
self,
side: str,
quantity: float,
order_type: str,
stop_price: float = None,
reduce_only: bool = False,
) -> dict:
"""STOP_MARKET / TAKE_PROFIT_MARKET 등 Algo Order API(/fapi/v1/algoOrder)로 전송."""
loop = asyncio.get_event_loop()
params = dict(
symbol=self.symbol,
side=side,
algoType="CONDITIONAL",
type=order_type,
quantity=quantity,
reduceOnly="true" if reduce_only else "false",
)
if stop_price:
params["triggerPrice"] = stop_price
try:
return await loop.run_in_executor(
None, lambda: self.client.futures_create_algo_order(**params)
)
except BinanceAPIException as e:
logger.error(f"Algo 주문 실패: {e}")
raise
async def get_position(self) -> dict | None:
loop = asyncio.get_event_loop()
positions = await loop.run_in_executor(
@@ -175,7 +136,7 @@ class BinanceFuturesClient:
return None
async def cancel_all_orders(self):
"""일반 오픈 주문과 Algo 오픈 주문을 모두 취소한다."""
"""오픈 주문을 모두 취소한다."""
loop = asyncio.get_event_loop()
await loop.run_in_executor(
None,
@@ -183,15 +144,6 @@ class BinanceFuturesClient:
symbol=self.symbol
),
)
try:
await loop.run_in_executor(
None,
lambda: self.client.futures_cancel_all_algo_open_orders(
symbol=self.symbol
),
)
except Exception as e:
logger.warning(f"Algo 주문 전체 취소 실패 (무시): {e}")
async def get_recent_income(self, limit: int = 5) -> list[dict]:
"""최근 REALIZED_PNL + COMMISSION 내역을 조회한다."""