diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 97e5d2f1e..09bf0ea5a 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -342,7 +342,7 @@ class Exchange: def get_pair_base_currency(self, pair: str) -> str: """ - Return a pair's quote currency + Return a pair's base currency """ return self.markets.get(pair, {}).get('base', '') @@ -1158,6 +1158,39 @@ class Exchange: balances.pop("total", None) balances.pop("used", None) + if self.trading_mode == TradingMode.FUTURES: + + open_orders_response: List[dict] = self._api.fetch_open_orders() + open_orders: dict = {} + for order in open_orders_response: + symbol: str = order['symbol'] + open_orders[symbol] = order + + positions: List[dict] = self._api.fetch_positions() + for position in positions: + symbol = position['symbol'] + market: dict = self.markets[symbol] + size: float = self._contracts_to_amount(symbol, position['contracts']) + side: str = position['side'] + if size > 0: + + if symbol in open_orders: + order_amount: float = open_orders[symbol]['remaining'] + else: + order_amount = 0 + + if side == 'short': + currency: str = market['quote'] + else: + currency = market['base'] + + if currency in balances: + balances[currency] = { + 'free': size - order_amount, + 'used': order_amount, + 'total': size, + } + return balances except ccxt.DDoSProtection as e: raise DDosProtection(e) from e diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index c1ec9ca06..fd7203882 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1338,7 +1338,6 @@ class FreqtradeBot(LoggingMixin): trade_base_currency = self.exchange.get_pair_base_currency(pair) wallet_amount = self.wallets.get_free(trade_base_currency) logger.debug(f"{pair} - Wallet: {wallet_amount} - Trade-amount: {amount}") - # TODO-lev: Get wallet amount + value of positions if wallet_amount >= amount or self.trading_mode == TradingMode.FUTURES: # A safe exit amount isn't needed for futures, you can just exit/close the position return amount