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:
15
src/bot.py
15
src/bot.py
@@ -191,7 +191,7 @@ class TradingBot:
|
||||
num_symbols = len(self.config.symbols)
|
||||
per_symbol_balance = balance / num_symbols
|
||||
price = df["close"].iloc[-1]
|
||||
margin_ratio = self.risk.get_dynamic_margin_ratio(balance)
|
||||
margin_ratio = self.risk.get_dynamic_margin_ratio(per_symbol_balance)
|
||||
quantity = self.exchange.calculate_quantity(
|
||||
balance=per_symbol_balance, price=price, leverage=self.config.leverage, margin_ratio=margin_ratio
|
||||
)
|
||||
@@ -325,11 +325,11 @@ class TradingBot:
|
||||
commission = 0.0
|
||||
exit_price = 0.0
|
||||
try:
|
||||
pnl_rows, comm_rows = await self.exchange.get_recent_income(limit=5)
|
||||
pnl_rows, comm_rows = await self.exchange.get_recent_income(limit=10)
|
||||
if pnl_rows:
|
||||
realized_pnl = float(pnl_rows[-1].get("income", "0"))
|
||||
realized_pnl = sum(float(r.get("income", "0")) for r in pnl_rows)
|
||||
if comm_rows:
|
||||
commission = abs(float(comm_rows[-1].get("income", "0")))
|
||||
commission = sum(abs(float(r.get("income", "0"))) for r in comm_rows)
|
||||
except Exception:
|
||||
pass
|
||||
net_pnl = realized_pnl - commission
|
||||
@@ -399,9 +399,16 @@ class TradingBot:
|
||||
"""기존 포지션을 청산하고, ML 필터 통과 시 반대 방향으로 즉시 재진입한다."""
|
||||
# 재진입 플래그: User Data Stream 콜백이 신규 포지션 상태를 초기화하지 않도록 보호
|
||||
self._is_reentering = True
|
||||
prev_side = self.current_trade_side
|
||||
try:
|
||||
await self._close_position(position)
|
||||
|
||||
# 청산 완료 확인: 콜백이 처리했든 아니든 로컬 상태를 명시적으로 Flat으로 전환
|
||||
self.current_trade_side = None
|
||||
self._entry_price = None
|
||||
self._entry_quantity = None
|
||||
await self.risk.close_position(self.symbol, 0.0) if prev_side and self.symbol not in self.risk.open_positions else None
|
||||
|
||||
if not await self.risk.can_open_new_position(self.symbol, signal):
|
||||
logger.info(f"[{self.symbol}] 최대 포지션 수 도달 — 재진입 건너뜀")
|
||||
return
|
||||
|
||||
@@ -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 내역을 조회한다."""
|
||||
|
||||
Reference in New Issue
Block a user