diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 5a253e40c..444fe044a 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -1063,14 +1063,11 @@ class FreqtradeBot(LoggingMixin): """ trades_closed = 0 for trade in trades: - # TODO: get_total currently fails for futures! - wallet_amount = self.wallets.get_total(trade.safe_base_currency) - if wallet_amount < trade.amount: - # + if not self.wallets.check_exit_amount(trade): logger.warning( - f'Not enough {trade.safe_base_currency} in wallet to exit {trade.pair}. ' - f'Amount needed: {trade.amount}, amount available: {wallet_amount}') + f'Not enough {trade.safe_base_currency} in wallet to exit {trade}. ' + 'Trying to recover.') self.handle_onexchange_order(trade) try: diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index 6f86398f3..ecac638c6 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -181,6 +181,35 @@ class Wallets: def get_all_positions(self) -> Dict[str, PositionWallet]: return self._positions + def _check_exit_amount(self, trade: Trade) -> bool: + if trade.trading_mode != TradingMode.FUTURES: + # Slightly higher offset than in safe_exit_amount. + wallet_amount: float = self.get_total(trade.safe_base_currency) * 0.981 + else: + # wallet_amount: float = self.wallets.get_free(trade.safe_base_currency) + position = self._positions.get(trade.pair) + if position is None: + # We don't own anything :O + return False + wallet_amount = position.position + + if wallet_amount >= trade.amount: + return True + return False + + def check_exit_amount(self, trade: Trade) -> bool: + """ + Checks if the exit amount is available in the wallet. + :param trade: Trade to check + :return: True if the exit amount is available, False otherwise + """ + if not self._check_exit_amount(trade): + # Update wallets just to make sure + self.update() + return self._check_exit_amount(trade) + + return True + def get_starting_balance(self) -> float: """ Retrieves starting balance - based on either available capital,