Added parameters explicitly to liquidation_price functions

This commit is contained in:
Sam Germain 2021-08-25 14:27:10 -06:00
parent a012b9ee84
commit 6f75a65325
2 changed files with 57 additions and 22 deletions

View File

@ -1,12 +1,17 @@
from typing import Optional
from freqtrade.enums import Collateral, TradingMode from freqtrade.enums import Collateral, TradingMode
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
def liquidation_price( def liquidation_price(
exchange_name: str, exchange_name: str,
open_rate: float,
is_short: bool,
leverage: float,
trading_mode: TradingMode, trading_mode: TradingMode,
** k collateral: Optional[Collateral]
): ) -> Optional[float]:
leverage_exchanges = [ leverage_exchanges = [
'binance', 'binance',
@ -16,21 +21,25 @@ def liquidation_price(
if trading_mode == TradingMode.SPOT or exchange_name.lower() not in leverage_exchanges: if trading_mode == TradingMode.SPOT or exchange_name.lower() not in leverage_exchanges:
return None 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": if exchange_name.lower() == "binance":
# TODO-lev: Get more variables from **k and pass them to binance return binance(open_rate, is_short, leverage, trading_mode, collateral)
return binance(trading_mode, collateral)
elif exchange_name.lower() == "kraken": elif exchange_name.lower() == "kraken":
# TODO-lev: Get more variables from **k and pass them to kraken return kraken(open_rate, is_short, leverage, trading_mode, collateral)
return kraken(trading_mode, collateral)
elif exchange_name.lower() == "ftx": elif exchange_name.lower() == "ftx":
return ftx(trading_mode, collateral) return ftx(open_rate, is_short, leverage, trading_mode, collateral)
return raise OperationalException(
f"liquidation_price is not yet implemented for {exchange_name}"
)
def exception( def exception(
exchange_name: str, exchange: str,
trading_mode: TradingMode, trading_mode: TradingMode,
collateral: Collateral collateral: Collateral
): ):
@ -41,10 +50,16 @@ def exception(
:param collateral: cross, isolated :param collateral: cross, isolated
""" """
raise OperationalException( 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 Calculates the liquidation price on Binance
:param name: Name of the exchange :param name: Name of the exchange
@ -70,7 +85,13 @@ def binance(trading_mode: TradingMode, collateral: Collateral):
exception("binance", trading_mode, 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 Calculates the liquidation price on Kraken
:param name: Name of the exchange :param name: Name of the exchange
@ -91,7 +112,13 @@ def kraken(trading_mode: TradingMode, collateral: Collateral):
exception("kraken", trading_mode, 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 Calculates the liquidation price on FTX
:param name: Name of the exchange :param name: Name of the exchange

View File

@ -14,7 +14,7 @@ from sqlalchemy.pool import StaticPool
from sqlalchemy.sql.schema import UniqueConstraint from sqlalchemy.sql.schema import UniqueConstraint
from freqtrade.constants import DATETIME_PRINT_FORMAT 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.exceptions import DependencyException, OperationalException
from freqtrade.leverage import interest, liquidation_price from freqtrade.leverage import interest, liquidation_price
from freqtrade.misc import safe_value_fallback from freqtrade.misc import safe_value_fallback
@ -265,6 +265,8 @@ class LocalTrade():
buy_tag: Optional[str] = None buy_tag: Optional[str] = None
timeframe: Optional[int] = None timeframe: Optional[int] = None
trading_mode: TradingMode
# Leverage trading properties # Leverage trading properties
is_short: bool = False is_short: bool = False
isolated_liq: Optional[float] = None isolated_liq: Optional[float] = None
@ -344,17 +346,23 @@ class LocalTrade():
self.stop_loss_pct = -1 * abs(percent) self.stop_loss_pct = -1 * abs(percent)
self.stoploss_last_update = datetime.utcnow() 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. Method you should use to set self.liquidation price.
Assures stop_loss is not passed the liquidation price Assures stop_loss is not passed the liquidation price
""" """
if k['isolated_liq']: if not isolated_liq:
isolated_liq: float = k['isolated_liq'] isolated_liq = liquidation_price(
else: exchange_name=self.exchange,
isolated_liq: float = liquidation_price( open_rate=self.open_rate,
exchange=self.exchange_name, is_short=self.is_short,
**k 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: if self.stop_loss is not None: