diff --git a/src/bot.py b/src/bot.py index 3415dc1..15f0c1f 100644 --- a/src/bot.py +++ b/src/bot.py @@ -437,6 +437,7 @@ class TradingBot: return side = "BUY" if signal == "LONG" else "SELL" + await self.exchange.set_margin_type("ISOLATED") await self.exchange.set_leverage(self.config.leverage) await self.exchange.place_order(side=side, quantity=quantity) diff --git a/src/exchange.py b/src/exchange.py index f398066..3795d3e 100644 --- a/src/exchange.py +++ b/src/exchange.py @@ -107,6 +107,21 @@ class BinanceFuturesClient: ), ) + async def set_margin_type(self, margin_type: str = "ISOLATED") -> None: + """마진 타입을 변경한다. 이미 동일 타입이면 무시.""" + try: + await self._run_api( + lambda: self.client.futures_change_margin_type( + symbol=self.symbol, marginType=margin_type + ), + ) + logger.info(f"[{self.symbol}] 마진 타입 변경: {margin_type}") + except BinanceAPIException as e: + if e.code == -4046: # "No need to change margin type." + logger.debug(f"[{self.symbol}] 마진 타입 이미 {margin_type}") + else: + raise + async def get_balance(self) -> float: balances = await self._run_api(self.client.futures_account_balance) for b in balances: