2021-08-06 07:15:18 +00:00
|
|
|
|
from typing import Optional
|
|
|
|
|
|
2021-09-19 23:17:09 +00:00
|
|
|
|
from freqtrade.enums import Collateral, TradingMode
|
2021-08-06 07:15:18 +00:00
|
|
|
|
from freqtrade.exceptions import OperationalException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def liquidation_price(
|
|
|
|
|
exchange_name: str,
|
|
|
|
|
open_rate: float,
|
|
|
|
|
is_short: bool,
|
|
|
|
|
leverage: float,
|
|
|
|
|
trading_mode: TradingMode,
|
2021-09-19 04:48:28 +00:00
|
|
|
|
collateral: Optional[Collateral],
|
|
|
|
|
wallet_balance: Optional[float],
|
|
|
|
|
maintenance_margin_ex_1: Optional[float],
|
|
|
|
|
unrealized_pnl_ex_1: Optional[float],
|
|
|
|
|
maintenance_amount_both: Optional[float],
|
|
|
|
|
position_1_both: Optional[float],
|
|
|
|
|
entry_price_1_both: Optional[float],
|
|
|
|
|
maintenance_margin_rate_both: Optional[float]
|
2021-08-06 07:15:18 +00:00
|
|
|
|
) -> Optional[float]:
|
2021-09-19 23:17:09 +00:00
|
|
|
|
|
2021-08-25 20:27:55 +00:00
|
|
|
|
if trading_mode == TradingMode.SPOT:
|
2021-08-06 07:15:18 +00:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
if not collateral:
|
|
|
|
|
raise OperationalException(
|
|
|
|
|
"Parameter collateral is required by liquidation_price when trading_mode is "
|
|
|
|
|
f"{trading_mode}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if exchange_name.lower() == "binance":
|
2021-09-19 04:48:28 +00:00
|
|
|
|
if not wallet_balance or not maintenance_margin_ex_1 or not unrealized_pnl_ex_1 or not maintenance_amount_both \
|
|
|
|
|
or not position_1_both or not entry_price_1_both or not maintenance_margin_rate_both:
|
|
|
|
|
raise OperationalException(
|
|
|
|
|
f"Parameters wallet_balance, maintenance_margin_ex_1, unrealized_pnl_ex_1, maintenance_amount_both, "
|
|
|
|
|
f"position_1_both, entry_price_1_both, maintenance_margin_rate_both is required by liquidation_price "
|
|
|
|
|
f"when exchange is {exchange_name.lower()}")
|
|
|
|
|
|
|
|
|
|
return binance(open_rate, is_short, leverage, trading_mode, collateral, wallet_balance, maintenance_margin_ex_1,
|
|
|
|
|
unrealized_pnl_ex_1, maintenance_amount_both, position_1_both, entry_price_1_both,
|
|
|
|
|
maintenance_margin_rate_both)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
elif exchange_name.lower() == "kraken":
|
|
|
|
|
return kraken(open_rate, is_short, leverage, trading_mode, collateral)
|
|
|
|
|
elif exchange_name.lower() == "ftx":
|
|
|
|
|
return ftx(open_rate, is_short, leverage, trading_mode, collateral)
|
|
|
|
|
raise OperationalException(
|
2021-08-25 20:27:55 +00:00
|
|
|
|
f"liquidation_price is not implemented for {exchange_name}"
|
2021-08-06 07:15:18 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exception(
|
|
|
|
|
exchange: str,
|
|
|
|
|
trading_mode: TradingMode,
|
2021-09-19 04:48:28 +00:00
|
|
|
|
collateral: Collateral,
|
2021-08-06 07:15:18 +00:00
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Raises an exception if exchange used doesn't support desired leverage mode
|
2021-09-19 04:48:28 +00:00
|
|
|
|
:param exchange: Name of the exchange
|
2021-08-06 07:15:18 +00:00
|
|
|
|
:param trading_mode: spot, margin, futures
|
|
|
|
|
:param collateral: cross, isolated
|
|
|
|
|
"""
|
2021-09-19 04:48:28 +00:00
|
|
|
|
|
2021-08-06 07:15:18 +00:00
|
|
|
|
raise OperationalException(
|
2021-09-19 04:48:28 +00:00
|
|
|
|
f"{exchange} does not support {collateral.value} Mode {trading_mode.value} trading ")
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def binance(
|
|
|
|
|
open_rate: float,
|
|
|
|
|
is_short: bool,
|
|
|
|
|
leverage: float,
|
|
|
|
|
trading_mode: TradingMode,
|
2021-09-17 02:47:15 +00:00
|
|
|
|
collateral: Collateral,
|
|
|
|
|
wallet_balance: float,
|
|
|
|
|
maintenance_margin_ex_1: float,
|
|
|
|
|
unrealized_pnl_ex_1: float,
|
|
|
|
|
maintenance_amount_both: float,
|
|
|
|
|
position_1_both: float,
|
|
|
|
|
entry_price_1_both: float,
|
|
|
|
|
maintenance_margin_rate_both: float,
|
2021-08-06 07:15:18 +00:00
|
|
|
|
):
|
2021-09-19 23:17:09 +00:00
|
|
|
|
"""
|
2021-08-06 07:15:18 +00:00
|
|
|
|
Calculates the liquidation price on Binance
|
2021-09-19 04:48:28 +00:00
|
|
|
|
:param open_rate: open_rate
|
|
|
|
|
:param is_short: true or false
|
|
|
|
|
:param leverage: leverage in float
|
2021-08-06 07:15:18 +00:00
|
|
|
|
:param trading_mode: spot, margin, futures
|
|
|
|
|
:param collateral: cross, isolated
|
2021-09-17 02:47:15 +00:00
|
|
|
|
|
|
|
|
|
:param wallet_balance: Wallet Balance is crossWalletBalance in Cross-Margin Mode.
|
|
|
|
|
Wallet Balance is isolatedWalletBalance in Isolated Margin Mode
|
|
|
|
|
|
|
|
|
|
:param maintenance_margin_ex_1: Maintenance Margin of all other contracts, excluding Contract 1.
|
|
|
|
|
If it is an isolated margin mode, then TMM=0
|
|
|
|
|
|
|
|
|
|
:param unrealized_pnl_ex_1: Unrealized PNL of all other contracts, excluding Contract 1.
|
|
|
|
|
If it is an isolated margin mode, then UPNL=0
|
|
|
|
|
|
|
|
|
|
:param maintenance_amount_both: Maintenance Amount of BOTH position (one-way mode)
|
|
|
|
|
|
|
|
|
|
:param position_1_both: Absolute value of BOTH position size (one-way mode)
|
|
|
|
|
|
|
|
|
|
:param entry_price_1_both: Entry Price of BOTH position (one-way mode)
|
|
|
|
|
|
|
|
|
|
:param maintenance_margin_rate_both: Maintenance margin rate of BOTH position (one-way mode)
|
|
|
|
|
|
2021-08-06 07:15:18 +00:00
|
|
|
|
"""
|
|
|
|
|
# TODO-lev: Additional arguments, fill in formulas
|
2021-09-17 02:47:15 +00:00
|
|
|
|
wb = wallet_balance
|
|
|
|
|
tmm_1 = 0.0 if collateral == Collateral.ISOLATED else maintenance_margin_ex_1
|
|
|
|
|
upnl_1 = 0.0 if collateral == Collateral.ISOLATED else unrealized_pnl_ex_1
|
|
|
|
|
cum_b = maintenance_amount_both
|
|
|
|
|
side_1_both = -1 if is_short else 1
|
|
|
|
|
position_1_both = abs(position_1_both)
|
|
|
|
|
ep1_both = entry_price_1_both
|
|
|
|
|
mmr_b = maintenance_margin_rate_both
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
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
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("binance", trading_mode, collateral)
|
|
|
|
|
elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED:
|
2021-09-19 04:48:28 +00:00
|
|
|
|
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
|
|
|
|
|
# Liquidation Price of USDⓈ-M Futures Contracts Isolated
|
|
|
|
|
|
|
|
|
|
# Isolated margin mode, then TMM=0,UPNL=0
|
|
|
|
|
return (wb + cum_b - (side_1_both * position_1_both * ep1_both)) / (
|
|
|
|
|
position_1_both * mmr_b - side_1_both * position_1_both)
|
|
|
|
|
|
|
|
|
|
elif trading_mode == TradingMode.FUTURES and collateral == Collateral.CROSS:
|
2021-09-19 23:17:09 +00:00
|
|
|
|
# TODO-lev: perform a calculation based on this formula
|
|
|
|
|
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
|
2021-09-19 04:48:28 +00:00
|
|
|
|
# Liquidation Price of USDⓈ-M Futures Contracts Cross
|
|
|
|
|
|
|
|
|
|
# Isolated margin mode, then TMM=0,UPNL=0
|
|
|
|
|
return (wb - tmm_1 + upnl_1 + cum_b - (side_1_both * position_1_both * ep1_both)) / (
|
|
|
|
|
position_1_both * mmr_b - side_1_both * position_1_both)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
# If nothing was returned
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("binance", trading_mode, collateral)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def kraken(
|
|
|
|
|
open_rate: float,
|
|
|
|
|
is_short: bool,
|
|
|
|
|
leverage: float,
|
|
|
|
|
trading_mode: TradingMode,
|
|
|
|
|
collateral: Collateral
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Calculates the liquidation price on Kraken
|
2021-09-19 23:17:09 +00:00
|
|
|
|
:param name: Name of the exchange
|
2021-08-06 07:15:18 +00:00
|
|
|
|
: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:
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("kraken", trading_mode, collateral)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
# If nothing was returned
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("kraken", trading_mode, collateral)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ftx(
|
|
|
|
|
open_rate: float,
|
|
|
|
|
is_short: bool,
|
|
|
|
|
leverage: float,
|
|
|
|
|
trading_mode: TradingMode,
|
|
|
|
|
collateral: Collateral
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Calculates the liquidation price on FTX
|
2021-09-19 23:17:09 +00:00
|
|
|
|
:param name: Name of the exchange
|
2021-08-06 07:15:18 +00:00
|
|
|
|
:param trading_mode: spot, margin, futures
|
|
|
|
|
:param collateral: cross, isolated
|
|
|
|
|
"""
|
|
|
|
|
if collateral == Collateral.CROSS:
|
|
|
|
|
# TODO-lev: Additional arguments, fill in formulas
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("ftx", trading_mode, collateral)
|
2021-08-06 07:15:18 +00:00
|
|
|
|
|
|
|
|
|
# If nothing was returned
|
2021-09-19 23:17:09 +00:00
|
|
|
|
exception("ftx", trading_mode, collateral)
|