From 79538368dbe0ced14a21a74a9122c6d90f3c9a69 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 28 Feb 2022 19:45:15 +0100 Subject: [PATCH] Simplify liquidation price calculation --- freqtrade/exchange/exchange.py | 24 ++++++++++++------------ freqtrade/freqtradebot.py | 5 +++-- freqtrade/optimize/backtesting.py | 7 +++++-- tests/exchange/test_exchange.py | 5 ++--- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index bef2f766e..4fd0b86d2 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -2055,23 +2055,23 @@ class Exchange: except ccxt.BaseError as e: raise OperationalException(e) from e - def leverage_prep( + def get_interest_rate(self) -> float: + """ + Calculate interest rate - necessary for Margin trading. + """ + return 0.0 + + def get_liquidation_price( self, pair: str, open_rate: float, amount: float, # quote currency, includes leverage leverage: float, is_short: bool - ) -> Tuple[float, Optional[float]]: + ) -> Optional[float]: - # if TradingMode == TradingMode.MARGIN: - # interest_rate = self.get_interest_rate( - # pair=pair, - # open_rate=open_rate, - # is_short=is_short - # ) - if self.trading_mode == TradingMode.SPOT: - return (0.0, None) + if self.trading_mode in (TradingMode.SPOT, TradingMode.MARGIN): + return None elif ( self.margin_mode == MarginMode.ISOLATED and self.trading_mode == TradingMode.FUTURES @@ -2086,7 +2086,7 @@ class Exchange: mm_ex_1=0.0, upnl_ex_1=0.0, ) - return (0.0, isolated_liq) + return isolated_liq else: raise OperationalException( "Freqtrade only supports isolated futures for leverage trading") @@ -2231,7 +2231,7 @@ class Exchange: return 0.0 @retrier - def get_liquidation_price( + def get_or_calculate_liquidation_price( self, pair: str, # Dry-run diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 70540c398..8f38ab8aa 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -19,7 +19,7 @@ from freqtrade.edge import Edge from freqtrade.enums import (MarginMode, RPCMessageType, RunMode, SellType, SignalDirection, State, TradingMode) from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError, - InvalidOrderException, OperationalException, PricingError) + InvalidOrderException, PricingError) from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.misc import safe_value_fallback, safe_value_fallback2 from freqtrade.mixins import LoggingMixin @@ -688,13 +688,14 @@ class FreqtradeBot(LoggingMixin): amount = safe_value_fallback(order, 'filled', 'amount') enter_limit_filled_price = safe_value_fallback(order, 'average', 'price') - interest_rate, isolated_liq = self.exchange.leverage_prep( + isolated_liq = self.exchange.get_liquidation_price( leverage=leverage, pair=pair, amount=amount, open_rate=enter_limit_filled_price, is_short=is_short ) + interest_rate = self.exchange.get_interest_rate() # Fee is applied twice because we make a LIMIT_BUY and LIMIT_SELL fee = self.exchange.get_fee(symbol=pair, taker_or_maker='maker') diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index bde887998..6d562b6ab 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -666,13 +666,16 @@ class Backtesting: self.order_id_counter += 1 amount = round((stake_amount / propose_rate) * leverage, 8) is_short = (direction == 'short') - (interest_rate, isolated_liq) = self.exchange.leverage_prep( + isolated_liq = self.exchange.get_liquidation_price( pair=pair, open_rate=propose_rate, amount=amount, leverage=leverage, is_short=is_short, ) + # Necessary for Margin trading. Disabled until support is enabled. + # interest_rate = self.exchange.get_interest_rate() + if trade is None: # Enter trade self.trade_id_counter += 1 @@ -694,7 +697,7 @@ class Backtesting: is_short=is_short, trading_mode=self.trading_mode, leverage=leverage, - interest_rate=interest_rate, + # interest_rate=interest_rate, orders=[], ) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index f937038e0..fadcacc53 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -4560,7 +4560,7 @@ def test__get_params(mocker, default_conf, exchange_name): # (False, 'futures', 'okx', 'isolated', 8.085708510208207), ] ) -def test_leverage_prep( +def test_get_liquidation_price( mocker, default_conf_usdt, is_short, @@ -4637,14 +4637,13 @@ def test_leverage_prep( # default_conf_usdt.update({ # "dry_run": False, # }) - (interest, liq) = exchange.leverage_prep( + liq = exchange.get_liquidation_price( pair='ETH/USDT:USDT', open_rate=open_rate, amount=amount, leverage=leverage, is_short=is_short, ) - assert interest == 0.0 if expected_liq is None: assert liq is None else: