Added liquidation_price function

This commit is contained in:
Sam Germain
2021-08-06 01:15:18 -06:00
parent ecdecb02fa
commit 04f254b885
5 changed files with 220 additions and 15 deletions

View File

@@ -1 +1,2 @@
# flake8: noqa: F401
from freqtrade.leverage.liquidation_price import liquidation_price

View File

@@ -0,0 +1,106 @@
from freqtrade.enums import Collateral, TradingMode
from freqtrade.exceptions import OperationalException
def liquidation_price(
exchange_name: str,
trading_mode: TradingMode,
** k
):
leverage_exchanges = [
'binance',
'kraken',
'ftx'
]
if trading_mode == TradingMode.SPOT or exchange_name.lower() not in leverage_exchanges:
return None
collateral: Collateral = k['collateral']
if exchange_name.lower() == "binance":
# TODO-lev: Get more variables from **k and pass them to binance
return binance(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)
elif exchange_name.lower() == "ftx":
return ftx(trading_mode, collateral)
return
def exception(
exchange_name: str,
trading_mode: TradingMode,
collateral: Collateral
):
"""
Raises an exception if exchange used doesn't support desired leverage mode
:param name: Name of the exchange
:param trading_mode: spot, margin, futures
:param collateral: cross, isolated
"""
raise OperationalException(
f"{exchange_name} does not support {collateral.value} {trading_mode.value} trading")
def binance(trading_mode: TradingMode, collateral: Collateral):
"""
Calculates the liquidation price on Binance
:param name: Name of the exchange
:param trading_mode: spot, margin, futures
:param collateral: cross, isolated
"""
# TODO-lev: Additional arguments, fill in formulas
if trading_mode == TradingMode.MARGIN and collateral == Collateral.CROSS:
# TODO-lev: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed
exception("binance", trading_mode, collateral)
elif trading_mode == TradingMode.FUTURES and collateral == Collateral.CROSS:
# TODO-lev: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
exception("binance", trading_mode, collateral)
elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED:
# TODO-lev: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
exception("binance", trading_mode, collateral)
# If nothing was returned
exception("binance", trading_mode, collateral)
def kraken(trading_mode: TradingMode, collateral: Collateral):
"""
Calculates the liquidation price on Kraken
:param name: Name of the exchange
:param trading_mode: spot, margin, futures
:param collateral: cross, isolated
"""
# TODO-lev: Additional arguments, fill in formulas
if collateral == Collateral.CROSS:
if trading_mode == TradingMode.MARGIN:
exception("kraken", trading_mode, collateral)
# TODO-lev: perform a calculation based on this formula
# https://support.kraken.com/hc/en-us/articles/203325763-Margin-Call-Level-and-Margin-Liquidation-Level
elif trading_mode == TradingMode.FUTURES:
exception("kraken", trading_mode, collateral)
# If nothing was returned
exception("kraken", trading_mode, collateral)
def ftx(trading_mode: TradingMode, collateral: Collateral):
"""
Calculates the liquidation price on FTX
:param name: Name of the exchange
:param trading_mode: spot, margin, futures
:param collateral: cross, isolated
"""
if collateral == Collateral.CROSS:
# TODO-lev: Additional arguments, fill in formulas
exception("ftx", trading_mode, collateral)
# If nothing was returned
exception("ftx", trading_mode, collateral)

View File

@@ -16,6 +16,7 @@ from sqlalchemy.sql.schema import UniqueConstraint
from freqtrade.constants import DATETIME_PRINT_FORMAT
from freqtrade.enums import InterestMode, SellType
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.leverage import liquidation_price
from freqtrade.misc import safe_value_fallback
from freqtrade.persistence.migrations import check_migrate
@@ -236,7 +237,7 @@ class LocalTrade():
close_rate_requested: Optional[float] = None
close_profit: Optional[float] = None
close_profit_abs: Optional[float] = None
stake_amount: float = 0.0 # TODO: This should probably be computed
stake_amount: float = 0.0
amount: float = 0.0
amount_requested: Optional[float] = None
open_date: datetime
@@ -316,7 +317,7 @@ class LocalTrade():
for key in kwargs:
setattr(self, key, kwargs[key])
if self.isolated_liq:
self.set_isolated_liq(self.isolated_liq)
self.set_isolated_liq(isolated_liq=self.isolated_liq)
self.recalc_open_trade_value()
def _set_stop_loss(self, stop_loss: float, percent: float):
@@ -342,11 +343,19 @@ class LocalTrade():
self.stop_loss_pct = -1 * abs(percent)
self.stoploss_last_update = datetime.utcnow()
def set_isolated_liq(self, isolated_liq: float):
def set_isolated_liq(self, **k):
"""
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 self.stop_loss is not None:
if self.is_short:
self.stop_loss = min(self.stop_loss, isolated_liq)