From 6f75a65325984ae1a71275aa60d365eea2d7f451 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Wed, 25 Aug 2021 14:27:10 -0600 Subject: [PATCH] Added parameters explicitly to liquidation_price functions --- freqtrade/leverage/liquidation_price.py | 55 ++++++++++++++++++------- freqtrade/persistence/models.py | 24 +++++++---- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/freqtrade/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py index 2aeba6b78..98ceb1704 100644 --- a/freqtrade/leverage/liquidation_price.py +++ b/freqtrade/leverage/liquidation_price.py @@ -1,12 +1,17 @@ +from typing import Optional + from freqtrade.enums import Collateral, TradingMode from freqtrade.exceptions import OperationalException def liquidation_price( exchange_name: str, + open_rate: float, + is_short: bool, + leverage: float, trading_mode: TradingMode, - ** k -): + collateral: Optional[Collateral] +) -> Optional[float]: leverage_exchanges = [ 'binance', @@ -16,21 +21,25 @@ def liquidation_price( if trading_mode == TradingMode.SPOT or exchange_name.lower() not in leverage_exchanges: return None - collateral: Collateral = k['collateral'] + if not collateral: + raise OperationalException( + "Parameter collateral is required by liquidation_price when trading_mode is " + f"{trading_mode}" + ) if exchange_name.lower() == "binance": - # TODO-lev: Get more variables from **k and pass them to binance - return binance(trading_mode, collateral) + return binance(open_rate, is_short, leverage, trading_mode, collateral) elif exchange_name.lower() == "kraken": - # TODO-lev: Get more variables from **k and pass them to kraken - return kraken(trading_mode, collateral) + return kraken(open_rate, is_short, leverage, trading_mode, collateral) elif exchange_name.lower() == "ftx": - return ftx(trading_mode, collateral) - return + return ftx(open_rate, is_short, leverage, trading_mode, collateral) + raise OperationalException( + f"liquidation_price is not yet implemented for {exchange_name}" + ) def exception( - exchange_name: str, + exchange: str, trading_mode: TradingMode, collateral: Collateral ): @@ -41,10 +50,16 @@ def exception( :param collateral: cross, isolated """ raise OperationalException( - f"{exchange_name} does not support {collateral.value} {trading_mode.value} trading") + f"{exchange} does not support {collateral.value} {trading_mode.value} trading") -def binance(trading_mode: TradingMode, collateral: Collateral): +def binance( + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Collateral +): """ Calculates the liquidation price on Binance :param name: Name of the exchange @@ -70,7 +85,13 @@ def binance(trading_mode: TradingMode, collateral: Collateral): exception("binance", trading_mode, collateral) -def kraken(trading_mode: TradingMode, collateral: Collateral): +def kraken( + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Collateral +): """ Calculates the liquidation price on Kraken :param name: Name of the exchange @@ -91,7 +112,13 @@ def kraken(trading_mode: TradingMode, collateral: Collateral): exception("kraken", trading_mode, collateral) -def ftx(trading_mode: TradingMode, collateral: Collateral): +def ftx( + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Collateral +): """ Calculates the liquidation price on FTX :param name: Name of the exchange diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 9dc87dc69..682db9497 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -14,7 +14,7 @@ from sqlalchemy.pool import StaticPool from sqlalchemy.sql.schema import UniqueConstraint from freqtrade.constants import DATETIME_PRINT_FORMAT -from freqtrade.enums import SellType +from freqtrade.enums import Collateral, SellType, TradingMode from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.leverage import interest, liquidation_price from freqtrade.misc import safe_value_fallback @@ -265,6 +265,8 @@ class LocalTrade(): buy_tag: Optional[str] = None timeframe: Optional[int] = None + trading_mode: TradingMode + # Leverage trading properties is_short: bool = False isolated_liq: Optional[float] = None @@ -344,17 +346,23 @@ class LocalTrade(): self.stop_loss_pct = -1 * abs(percent) self.stoploss_last_update = datetime.utcnow() - def set_isolated_liq(self, **k): + def set_isolated_liq(self, isolated_liq: Optional[float]): """ Method you should use to set self.liquidation price. Assures stop_loss is not passed the liquidation price """ - if k['isolated_liq']: - isolated_liq: float = k['isolated_liq'] - else: - isolated_liq: float = liquidation_price( - exchange=self.exchange_name, - **k + if not isolated_liq: + isolated_liq = liquidation_price( + exchange_name=self.exchange, + open_rate=self.open_rate, + is_short=self.is_short, + leverage=self.leverage, + trading_mode=self.trading_mode, + collateral=Collateral.ISOLATED + ) + if isolated_liq is None: + raise OperationalException( + "leverage/isolated_liq returned None. This exception should never happen" ) if self.stop_loss is not None: