Binance Liquidation Price Hedge-Mode Removed

This commit is contained in:
Arunavo Ray 2021-09-19 10:18:28 +05:30
parent 13331e4dc7
commit 6a81730a74
3 changed files with 29 additions and 79 deletions

View File

@ -7,4 +7,3 @@ from freqtrade.enums.selltype import SellType
from freqtrade.enums.signaltype import SignalTagType, SignalType from freqtrade.enums.signaltype import SignalTagType, SignalType
from freqtrade.enums.state import State from freqtrade.enums.state import State
from freqtrade.enums.tradingmode import TradingMode from freqtrade.enums.tradingmode import TradingMode
from freqtrade.enums.marginmode import MarginMode

View File

@ -1,10 +0,0 @@
from enum import Enum
class MarginMode(Enum):
"""
Enum to distinguish between
one-way mode or hedge mode in Futures (Cross and Isolated) or Margin Trading
"""
ONE_WAY = "one-way"
HEDGE = "hedge"

View File

@ -1,6 +1,6 @@
from typing import Optional from typing import Optional
from freqtrade.enums import Collateral, TradingMode, MarginMode from freqtrade.enums import Collateral, TradingMode
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
@ -11,7 +11,13 @@ def liquidation_price(
leverage: float, leverage: float,
trading_mode: TradingMode, trading_mode: TradingMode,
collateral: Optional[Collateral], collateral: Optional[Collateral],
margin_mode: Optional[MarginMode] 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]
) -> Optional[float]: ) -> Optional[float]:
if trading_mode == TradingMode.SPOT: if trading_mode == TradingMode.SPOT:
return None return None
@ -23,11 +29,16 @@ def liquidation_price(
) )
if exchange_name.lower() == "binance": if exchange_name.lower() == "binance":
if not margin_mode: 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( raise OperationalException(
f"Parameter margin_mode is required by liquidation_price when exchange is {trading_mode}") 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, margin_mode, trading_mode, collateral) 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)
elif exchange_name.lower() == "kraken": elif exchange_name.lower() == "kraken":
return kraken(open_rate, is_short, leverage, trading_mode, collateral) return kraken(open_rate, is_short, leverage, trading_mode, collateral)
elif exchange_name.lower() == "ftx": elif exchange_name.lower() == "ftx":
@ -41,52 +52,37 @@ def exception(
exchange: str, exchange: str,
trading_mode: TradingMode, trading_mode: TradingMode,
collateral: Collateral, collateral: Collateral,
margin_mode: Optional[MarginMode] = None
): ):
""" """
Raises an exception if exchange used doesn't support desired leverage mode Raises an exception if exchange used doesn't support desired leverage mode
:param exchange: Name of the exchange :param exchange: Name of the exchange
:param margin_mode: one-way or hedge
:param trading_mode: spot, margin, futures :param trading_mode: spot, margin, futures
:param collateral: cross, isolated :param collateral: cross, isolated
""" """
if not margin_mode:
raise OperationalException(
f"{exchange} does not support {collateral.value} {trading_mode.value} trading ")
raise OperationalException( raise OperationalException(
f"{exchange} does not support {collateral.value} {margin_mode.value} Mode {trading_mode.value} trading ") f"{exchange} does not support {collateral.value} Mode {trading_mode.value} trading ")
def binance( def binance(
open_rate: float, open_rate: float,
is_short: bool, is_short: bool,
leverage: float, leverage: float,
margin_mode: MarginMode,
trading_mode: TradingMode, trading_mode: TradingMode,
collateral: Collateral, collateral: Collateral,
wallet_balance: float, wallet_balance: float,
maintenance_margin_ex_1: float, maintenance_margin_ex_1: float,
unrealized_pnl_ex_1: float, unrealized_pnl_ex_1: float,
maintenance_amount_both: float, maintenance_amount_both: float,
maintenance_amount_long: float,
maintenance_amount_short: float,
position_1_both: float, position_1_both: float,
entry_price_1_both: float, entry_price_1_both: float,
position_1_long: float,
entry_price_1_long: float,
position_1_short: float,
entry_price_1_short: float,
maintenance_margin_rate_both: float, maintenance_margin_rate_both: float,
maintenance_margin_rate_long: float,
maintenance_margin_rate_short: float,
): ):
r""" r"""
Calculates the liquidation price on Binance Calculates the liquidation price on Binance
:param open_rate: open_rate :param open_rate: open_rate
:param is_short: true or false :param is_short: true or false
:param leverage: leverage in float :param leverage: leverage in float
:param margin_mode: one-way or hedge
:param trading_mode: spot, margin, futures :param trading_mode: spot, margin, futures
:param collateral: cross, isolated :param collateral: cross, isolated
@ -101,80 +97,45 @@ def binance(
:param maintenance_amount_both: Maintenance Amount of BOTH position (one-way mode) :param maintenance_amount_both: Maintenance Amount of BOTH position (one-way mode)
:param maintenance_amount_long: Maintenance Amount of LONG position (hedge mode)
:param maintenance_amount_short: Maintenance Amount of SHORT position (hedge mode)
:param side_1_both: Direction of BOTH position, 1 as long position, -1 as short position derived from is_short
:param position_1_both: Absolute value of BOTH position size (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 entry_price_1_both: Entry Price of BOTH position (one-way mode)
:param position_1_long: Absolute value of LONG position size (hedge mode)
:param entry_price_1_long: Entry Price of LONG position (hedge mode)
:param position_1_short: Absolute value of SHORT position size (hedge mode)
:param entry_price_1_short: Entry Price of SHORT position (hedge mode)
:param maintenance_margin_rate_both: Maintenance margin rate of BOTH position (one-way mode) :param maintenance_margin_rate_both: Maintenance margin rate of BOTH position (one-way mode)
:param maintenance_margin_rate_long: Maintenance margin rate of LONG position (hedge mode)
:param maintenance_margin_rate_short: Maintenance margin rate of SHORT position (hedge mode)
""" """
# TODO-lev: Additional arguments, fill in formulas # TODO-lev: Additional arguments, fill in formulas
wb = wallet_balance wb = wallet_balance
tmm_1 = 0.0 if collateral == Collateral.ISOLATED else maintenance_margin_ex_1 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 upnl_1 = 0.0 if collateral == Collateral.ISOLATED else unrealized_pnl_ex_1
cum_b = maintenance_amount_both cum_b = maintenance_amount_both
cum_l = maintenance_amount_long
cum_s = maintenance_amount_short
side_1_both = -1 if is_short else 1 side_1_both = -1 if is_short else 1
position_1_both = abs(position_1_both) position_1_both = abs(position_1_both)
ep1_both = entry_price_1_both ep1_both = entry_price_1_both
position_1_long = abs(position_1_long)
ep1_long = entry_price_1_long
position_1_short = abs(position_1_short)
ep1_short = entry_price_1_short
mmr_b = maintenance_margin_rate_both mmr_b = maintenance_margin_rate_both
mmr_l = maintenance_margin_rate_long
mmr_s = maintenance_margin_rate_short
if trading_mode == TradingMode.MARGIN and collateral == Collateral.CROSS: if trading_mode == TradingMode.MARGIN and collateral == Collateral.CROSS:
# TODO-lev: perform a calculation based on this formula # TODO-lev: perform a calculation based on this formula
# https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed # https://www.binance.com/en/support/faq/f6b010588e55413aa58b7d63ee0125ed
exception("binance", trading_mode, collateral, margin_mode) exception("binance", trading_mode, collateral)
elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED: elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED:
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
# Liquidation Price of USDⓈ-M Futures Contracts Isolated # Liquidation Price of USDⓈ-M Futures Contracts Isolated
if margin_mode == MarginMode.HEDGE: # Isolated margin mode, then TMM=0UPNL=0
exception("binance", trading_mode, collateral, margin_mode) return (wb + cum_b - (side_1_both * position_1_both * ep1_both)) / (
position_1_both * mmr_b - side_1_both * position_1_both)
elif margin_mode == MarginMode.ONE_WAY:
# Isolated margin mode, then TMM=0UPNL=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: elif trading_mode == TradingMode.FUTURES and collateral == Collateral.CROSS:
# https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93
# Liquidation Price of USDⓈ-M Futures Contracts Cross # Liquidation Price of USDⓈ-M Futures Contracts Cross
if margin_mode == MarginMode.HEDGE: # Isolated margin mode, then TMM=0UPNL=0
return (wb - tmm_1 + upnl_1 + cum_l + cum_s - (position_1_long * ep1_long) + ( return (wb - tmm_1 + upnl_1 + cum_b - (side_1_both * position_1_both * ep1_both)) / (
position_1_short * ep1_short)) / ( position_1_both * mmr_b - side_1_both * position_1_both)
position_1_long * mmr_l + position_1_short * mmr_s - position_1_long + position_1_short)
elif margin_mode == MarginMode.ONE_WAY:
# Isolated margin mode, then TMM=0UPNL=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)
# If nothing was returned # If nothing was returned
exception("binance", trading_mode, collateral, margin_mode) exception("binance", trading_mode, collateral)
def kraken( def kraken(
@ -200,10 +161,10 @@ def kraken(
# TODO-lev: perform a calculation based on this formula # 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 # https://support.kraken.com/hc/en-us/articles/203325763-Margin-Call-Level-and-Margin-Liquidation-Level
elif trading_mode == TradingMode.FUTURES: elif trading_mode == TradingMode.FUTURES:
exception("kraken", trading_mode, collateral) exception("kraken", trading_mode, collateral)
# If nothing was returned # If nothing was returned
exception("kraken", trading_mode, collateral) exception("kraken", trading_mode, collateral)
def ftx( def ftx(
@ -223,7 +184,7 @@ def ftx(
""" """
if collateral == Collateral.CROSS: if collateral == Collateral.CROSS:
# TODO-lev: Additional arguments, fill in formulas # TODO-lev: Additional arguments, fill in formulas
exception("ftx", trading_mode, collateral) exception("ftx", trading_mode, collateral)
# If nothing was returned # If nothing was returned
exception("ftx", trading_mode, collateral) exception("ftx", trading_mode, collateral)