From c316d274443ad3840f057c235ca509034ac01494 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 30 Aug 2024 07:07:22 +0200 Subject: [PATCH] refactor: move exception handler into helper function --- freqtrade/freqtradebot.py | 17 +++---- freqtrade/leverage/liquidation_price.py | 62 +++++++++++++------------ 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 3c1122c9a..5097722fe 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -2233,16 +2233,13 @@ class FreqtradeBot(LoggingMixin): # Must also run for partial exits # TODO: Margin will need to use interest_rate as well. # interest_rate = self.exchange.get_interest_rate() - try: - update_liquidation_prices( - trade, - exchange=self.exchange, - wallets=self.wallets, - stake_currency=self.config["stake_currency"], - dry_run=self.config["dry_run"], - ) - except DependencyException: - logger.warning("Unable to calculate liquidation price") + update_liquidation_prices( + trade, + exchange=self.exchange, + wallets=self.wallets, + stake_currency=self.config["stake_currency"], + dry_run=self.config["dry_run"], + ) if self.strategy.use_custom_stoploss: current_rate = self.exchange.get_rate( trade.pair, side="exit", is_short=trade.is_short, refresh=True diff --git a/freqtrade/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py index 4053ba799..6b51397b5 100644 --- a/freqtrade/leverage/liquidation_price.py +++ b/freqtrade/leverage/liquidation_price.py @@ -1,6 +1,7 @@ import logging from freqtrade.enums import MarginMode +from freqtrade.exceptions import DependencyException from freqtrade.exchange import Exchange from freqtrade.persistence import LocalTrade, Trade from freqtrade.wallets import Wallets @@ -21,37 +22,40 @@ def update_liquidation_prices( Update trade liquidation price in isolated margin mode. Updates liquidation price for all trades in cross margin mode. """ - if exchange.margin_mode == MarginMode.CROSS: - total_wallet_stake = 0.0 - if dry_run: - # Parameters only needed for cross margin - total_wallet_stake = wallets.get_total(stake_currency) + try: + if exchange.margin_mode == MarginMode.CROSS: + total_wallet_stake = 0.0 + if dry_run: + # Parameters only needed for cross margin + total_wallet_stake = wallets.get_total(stake_currency) - 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( + 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( + 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, + other_trades=[], # TODO: Add other trades + ) + ) + else: + trade.set_liquidation_price( 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, + 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=total_wallet_stake, - other_trades=[], # TODO: Add other trades + wallet_balance=trade.stake_amount, + other_trades=[], ) ) - else: - trade.set_liquidation_price( - 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, - other_trades=[], - ) - ) + except DependencyException: + logger.warning("Unable to calculate liquidation price")