diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index fe270f670..d65ee8d0f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -2157,6 +2157,45 @@ class FreqtradeBot(LoggingMixin): # Common update trade state methods # + def update_liquidation_prices(self, trade: Optional[Trade] = None): + """ + Update trade liquidation price in isolated margin mode. + Updates liquidation price for all trades in cross margin mode. + TODO: this is missing a dedicated test! + """ + total_wallet_stake = 0.0 + if self.config["dry_run"] and self.exchange.margin_mode == MarginMode.CROSS: + # Parameters only needed for cross margin + total_wallet_stake = self.wallets.get_total(self.config["stake_currency"]) + + if self.exchange.margin_mode == MarginMode.CROSS: + logger.info("Updating liquidation price for all open trades.") + for t in Trade.get_open_trades(): + # TODO: This should be done in a batch update + t.set_liquidation_price( + self.exchange.get_liquidation_price( + pair=t.pair, + open_rate=t.open_rate, + is_short=t.is_short, + amount=t.amount, + stake_amount=t.stake_amount, + leverage=trade.leverage, + wallet_balance=total_wallet_stake, + ) + ) + elif trade: + trade.set_liquidation_price( + self.exchange.get_liquidation_price( + pair=trade.pair, + open_rate=trade.open_rate, + is_short=trade.is_short, + amount=trade.amount, + stake_amount=trade.stake_amount, + leverage=trade.leverage, + wallet_balance=trade.stake_amount, + ) + ) + def update_trade_state( self, trade: Trade, @@ -2234,17 +2273,7 @@ class FreqtradeBot(LoggingMixin): # TODO: Margin will need to use interest_rate as well. # interest_rate = self.exchange.get_interest_rate() try: - trade.set_liquidation_price( - self.exchange.get_liquidation_price( - pair=trade.pair, - open_rate=trade.open_rate, - is_short=trade.is_short, - amount=trade.amount, - stake_amount=trade.stake_amount, - leverage=trade.leverage, - wallet_balance=trade.stake_amount, - ) - ) + self.update_liquidation_prices(trade) except DependencyException: logger.warning("Unable to calculate liquidation price") if self.strategy.use_custom_stoploss: