From 5d3261e92fad6e4b7d5a843877ea3db2d646866a Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Tue, 7 Sep 2021 12:24:39 +0530 Subject: [PATCH 1/6] Added Ftx interest rate calculation --- freqtrade/leverage/interest.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/freqtrade/leverage/interest.py b/freqtrade/leverage/interest.py index aacbb3532..c687c8b5b 100644 --- a/freqtrade/leverage/interest.py +++ b/freqtrade/leverage/interest.py @@ -20,7 +20,7 @@ def interest( :param exchange_name: The exchanged being trading on :param borrowed: The amount of currency being borrowed - :param rate: The rate of interest + :param rate: The rate of interest (i.e daily interest rate) :param hours: The time in hours that the currency has been borrowed for Raises: @@ -36,7 +36,8 @@ def interest( # Rounded based on https://kraken-fees-calculator.github.io/ return borrowed * rate * (one+ceil(hours/four)) elif exchange_name == "ftx": - # TODO-lev: Add FTX interest formula - raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") + # As Explained under #Interest rates section in + # https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer + return borrowed * rate * ceil(hours)/twenty_four else: - raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") + raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") \ No newline at end of file From d07c7f7f275af5169b4ba3ac6ca25974c687998f Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Tue, 7 Sep 2021 12:28:23 +0530 Subject: [PATCH 2/6] Added Ftx interest rate calculation --- freqtrade/leverage/interest.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/freqtrade/leverage/interest.py b/freqtrade/leverage/interest.py index aacbb3532..c687c8b5b 100644 --- a/freqtrade/leverage/interest.py +++ b/freqtrade/leverage/interest.py @@ -20,7 +20,7 @@ def interest( :param exchange_name: The exchanged being trading on :param borrowed: The amount of currency being borrowed - :param rate: The rate of interest + :param rate: The rate of interest (i.e daily interest rate) :param hours: The time in hours that the currency has been borrowed for Raises: @@ -36,7 +36,8 @@ def interest( # Rounded based on https://kraken-fees-calculator.github.io/ return borrowed * rate * (one+ceil(hours/four)) elif exchange_name == "ftx": - # TODO-lev: Add FTX interest formula - raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") + # As Explained under #Interest rates section in + # https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer + return borrowed * rate * ceil(hours)/twenty_four else: - raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") + raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") \ No newline at end of file From b343d38112b86585ce3e98d30fe7a120cae78564 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Wed, 15 Sep 2021 06:55:44 +0530 Subject: [PATCH 3/6] =?UTF-8?q?Added=20Formulas=20to=20Calculate=20Liquida?= =?UTF-8?q?tion=20Price=20of=20Binance=20USD=E2=93=88-M=20Futures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freqtrade/enums/__init__.py | 1 + freqtrade/enums/marginmode.py | 10 + freqtrade/leverage/liquidation_price.py | 236 ++++++++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 freqtrade/enums/marginmode.py create mode 100644 freqtrade/leverage/liquidation_price.py diff --git a/freqtrade/enums/__init__.py b/freqtrade/enums/__init__.py index 692a7fcb6..610b5cf43 100644 --- a/freqtrade/enums/__init__.py +++ b/freqtrade/enums/__init__.py @@ -7,3 +7,4 @@ from freqtrade.enums.selltype import SellType from freqtrade.enums.signaltype import SignalTagType, SignalType from freqtrade.enums.state import State from freqtrade.enums.tradingmode import TradingMode +from freqtrade.enums.marginmode import MarginMode diff --git a/freqtrade/enums/marginmode.py b/freqtrade/enums/marginmode.py new file mode 100644 index 000000000..80df6e6fa --- /dev/null +++ b/freqtrade/enums/marginmode.py @@ -0,0 +1,10 @@ +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" diff --git a/freqtrade/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py new file mode 100644 index 000000000..00e6c904c --- /dev/null +++ b/freqtrade/leverage/liquidation_price.py @@ -0,0 +1,236 @@ +from typing import Optional + +from freqtrade.enums import Collateral, TradingMode, MarginMode +from freqtrade.exceptions import OperationalException + + +def liquidation_price( + exchange_name: str, + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Optional[Collateral], + margin_mode: Optional[MarginMode] +) -> Optional[float]: + if trading_mode == TradingMode.SPOT: + 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": + if not margin_mode: + raise OperationalException( + f"Parameter margin_mode is required by liquidation_price when exchange is {trading_mode}") + + return binance(open_rate, is_short, leverage, margin_mode, trading_mode, collateral) + 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( + f"liquidation_price is not implemented for {exchange_name}" + ) + + +def exception( + exchange: str, + trading_mode: TradingMode, + collateral: Collateral, + margin_mode: Optional[MarginMode] = None +): + """ + Raises an exception if exchange used doesn't support desired leverage mode + :param exchange: Name of the exchange + :param margin_mode: one-way or hedge + :param trading_mode: spot, margin, futures + :param collateral: cross, isolated + """ + if not margin_mode: + raise OperationalException( + f"{exchange} does not support {collateral.value} {trading_mode.value} trading ") + + raise OperationalException( + f"{exchange} does not support {collateral.value} {margin_mode.value} Mode {trading_mode.value} trading ") + + +def binance( + open_rate: float, + is_short: bool, + leverage: float, + margin_mode: MarginMode, + trading_mode: TradingMode, + collateral: Collateral, + **kwargs +): + r""" + Calculates the liquidation price on Binance + :param open_rate: open_rate + :param is_short: true or false + :param leverage: leverage in float + :param margin_mode: one-way or hedge + :param trading_mode: spot, margin, futures + :param collateral: cross, isolated + + :param \**kwargs: + See below + + :Keyword Arguments: + * *wallet_balance* (``float``) -- + Wallet Balance is crossWalletBalance in Cross-Margin Mode + Wallet Balance is isolatedWalletBalance in Isolated Margin Mode + + * *maintenance_margin_ex_1* (``float``) -- + Maintenance Margin of all other contracts, excluding Contract 1. + If it is an isolated margin mode, then TMM=0 + + * *unrealized_pnl_ex_1* (``float``) -- + Unrealized PNL of all other contracts, excluding Contract 1. + If it is an isolated margin mode, then UPNL=0 + + * *maintenance_amount_both* (``float``) -- + Maintenance Amount of BOTH position (one-way mode) + + * *maintenance_amount_long* (``float``) -- + Maintenance Amount of LONG position (hedge mode) + + * *maintenance_amount_short* (``float``) -- + Maintenance Amount of SHORT position (hedge mode) + + * *side_1_both* (``int``) -- + Direction of BOTH position, 1 as long position, -1 as short position + Derived from is_short + + * *position_1_both* (``float``) -- + Absolute value of BOTH position size (one-way mode) + + * *entry_price_1_both* (``float``) -- + Entry Price of BOTH position (one-way mode) + + * *position_1_long* (``float``) -- + Absolute value of LONG position size (hedge mode) + + * *entry_price_1_long* (``float``) -- + Entry Price of LONG position (hedge mode) + + * *position_1_short* (``float``) -- + Absolute value of SHORT position size (hedge mode) + + * *entry_price_1_short* (``float``) -- + Entry Price of SHORT position (hedge mode) + + * *maintenance_margin_rate_both* (``float``) -- + Maintenance margin rate of BOTH position (one-way mode) + + * *maintenance_margin_rate_long* (``float``) -- + Maintenance margin rate of LONG position (hedge mode) + + * *maintenance_margin_rate_short* (``float``) -- + Maintenance margin rate of SHORT position (hedge mode) + """ + # TODO-lev: Additional arguments, fill in formulas + wb = kwargs.get("wallet_balance") + tmm_1 = 0.0 if collateral == Collateral.ISOLATED else kwargs.get("maintenance_margin_ex_1") + upnl_1 = 0.0 if collateral == Collateral.ISOLATED else kwargs.get("unrealized_pnl_ex_1") + cum_b = kwargs.get("maintenance_amount_both") + cum_l = kwargs.get("maintenance_amount_long") + cum_s = kwargs.get("maintenance_amount_short") + side_1_both = -1 if is_short else 1 + position_1_both = abs(kwargs.get("position_1_both")) + ep1_both = kwargs.get("entry_price_1_both") + position_1_long = abs(kwargs.get("position_1_long")) + ep1_long = kwargs.get("entry_price_1_long") + position_1_short = abs(kwargs.get("position_1_short")) + ep1_short = kwargs.get("entry_price_1_short") + mmr_b = kwargs.get("maintenance_margin_rate_both") + mmr_l = kwargs.get("maintenance_margin_rate_long") + mmr_s = kwargs.get("maintenance_margin_rate_short") + + 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, margin_mode) + elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED: + # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 + # Liquidation Price of USDⓈ-M Futures Contracts Isolated + + if margin_mode == MarginMode.HEDGE: + exception("binance", trading_mode, collateral, margin_mode) + + elif margin_mode == MarginMode.ONE_WAY: + # 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: + # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 + # Liquidation Price of USDⓈ-M Futures Contracts Cross + + if margin_mode == MarginMode.HEDGE: + return (wb - tmm_1 + upnl_1 + cum_l + cum_s - (position_1_long * ep1_long) + ( + position_1_short * ep1_short)) / ( + 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=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) + + # If nothing was returned + exception("binance", trading_mode, collateral, margin_mode) + + +def kraken( + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Collateral +): + """ + Calculates the liquidation price on Kraken + :param open_rate: open_rate + :param is_short: true or false + :param leverage: leverage in float + :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( + open_rate: float, + is_short: bool, + leverage: float, + trading_mode: TradingMode, + collateral: Collateral +): + """ + Calculates the liquidation price on FTX + :param open_rate: open_rate + :param is_short: true or false + :param leverage: leverage in float + :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) \ No newline at end of file From f0aa76d799d0b650d5ea909e634c0a446ffe3d2b Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Wed, 15 Sep 2021 07:06:05 +0530 Subject: [PATCH 4/6] Revert "Added Ftx interest rate calculation" This reverts commit d07c7f7f275af5169b4ba3ac6ca25974c687998f. --- freqtrade/leverage/interest.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/freqtrade/leverage/interest.py b/freqtrade/leverage/interest.py index c687c8b5b..aacbb3532 100644 --- a/freqtrade/leverage/interest.py +++ b/freqtrade/leverage/interest.py @@ -20,7 +20,7 @@ def interest( :param exchange_name: The exchanged being trading on :param borrowed: The amount of currency being borrowed - :param rate: The rate of interest (i.e daily interest rate) + :param rate: The rate of interest :param hours: The time in hours that the currency has been borrowed for Raises: @@ -36,8 +36,7 @@ def interest( # Rounded based on https://kraken-fees-calculator.github.io/ return borrowed * rate * (one+ceil(hours/four)) elif exchange_name == "ftx": - # As Explained under #Interest rates section in - # https://help.ftx.com/hc/en-us/articles/360053007671-Spot-Margin-Trading-Explainer - return borrowed * rate * ceil(hours)/twenty_four + # TODO-lev: Add FTX interest formula + raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") else: - raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") \ No newline at end of file + raise OperationalException(f"Leverage not available on {exchange_name} with freqtrade") From 13331e4dc7afe0c52860efb9541d469993e02f6c Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Fri, 17 Sep 2021 08:17:15 +0530 Subject: [PATCH 5/6] Converted kwargs to params --- freqtrade/leverage/liquidation_price.py | 105 +++++++++++------------- 1 file changed, 49 insertions(+), 56 deletions(-) diff --git a/freqtrade/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py index 00e6c904c..2379875ac 100644 --- a/freqtrade/leverage/liquidation_price.py +++ b/freqtrade/leverage/liquidation_price.py @@ -65,7 +65,21 @@ def binance( margin_mode: MarginMode, trading_mode: TradingMode, collateral: Collateral, - **kwargs + wallet_balance: float, + maintenance_margin_ex_1: float, + unrealized_pnl_ex_1: float, + maintenance_amount_both: float, + maintenance_amount_long: float, + maintenance_amount_short: float, + position_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_long: float, + maintenance_margin_rate_short: float, ): r""" Calculates the liquidation price on Binance @@ -76,79 +90,58 @@ def binance( :param trading_mode: spot, margin, futures :param collateral: cross, isolated - :param \**kwargs: - See below + :param wallet_balance: Wallet Balance is crossWalletBalance in Cross-Margin Mode. + Wallet Balance is isolatedWalletBalance in Isolated Margin Mode - :Keyword Arguments: - * *wallet_balance* (``float``) -- - 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 - * *maintenance_margin_ex_1* (``float``) -- - 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 - * *unrealized_pnl_ex_1* (``float``) -- - 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) - * *maintenance_amount_both* (``float``) -- - Maintenance Amount of BOTH position (one-way mode) + :param maintenance_amount_long: Maintenance Amount of LONG position (hedge mode) - * *maintenance_amount_long* (``float``) -- - Maintenance Amount of LONG position (hedge mode) + :param maintenance_amount_short: Maintenance Amount of SHORT position (hedge mode) - * *maintenance_amount_short* (``float``) -- - 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 - * *side_1_both* (``int``) -- - 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) - * *position_1_both* (``float``) -- - Absolute value of BOTH position size (one-way mode) + :param entry_price_1_both: Entry Price of BOTH position (one-way mode) - * *entry_price_1_both* (``float``) -- - Entry Price of BOTH position (one-way mode) + :param position_1_long: Absolute value of LONG position size (hedge mode) - * *position_1_long* (``float``) -- - Absolute value of LONG position size (hedge mode) + :param entry_price_1_long: Entry Price of LONG position (hedge mode) - * *entry_price_1_long* (``float``) -- - Entry Price of LONG position (hedge mode) + :param position_1_short: Absolute value of SHORT position size (hedge mode) - * *position_1_short* (``float``) -- - Absolute value of SHORT position size (hedge mode) + :param entry_price_1_short: Entry Price of SHORT position (hedge mode) - * *entry_price_1_short* (``float``) -- - Entry Price of SHORT position (hedge mode) + :param maintenance_margin_rate_both: Maintenance margin rate of BOTH position (one-way mode) - * *maintenance_margin_rate_both* (``float``) -- - Maintenance margin rate of BOTH position (one-way mode) + :param maintenance_margin_rate_long: Maintenance margin rate of LONG position (hedge mode) - * *maintenance_margin_rate_long* (``float``) -- - Maintenance margin rate of LONG position (hedge mode) - - * *maintenance_margin_rate_short* (``float``) -- - Maintenance margin rate of SHORT position (hedge mode) + :param maintenance_margin_rate_short: Maintenance margin rate of SHORT position (hedge mode) """ # TODO-lev: Additional arguments, fill in formulas - wb = kwargs.get("wallet_balance") - tmm_1 = 0.0 if collateral == Collateral.ISOLATED else kwargs.get("maintenance_margin_ex_1") - upnl_1 = 0.0 if collateral == Collateral.ISOLATED else kwargs.get("unrealized_pnl_ex_1") - cum_b = kwargs.get("maintenance_amount_both") - cum_l = kwargs.get("maintenance_amount_long") - cum_s = kwargs.get("maintenance_amount_short") + 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 + cum_l = maintenance_amount_long + cum_s = maintenance_amount_short side_1_both = -1 if is_short else 1 - position_1_both = abs(kwargs.get("position_1_both")) - ep1_both = kwargs.get("entry_price_1_both") - position_1_long = abs(kwargs.get("position_1_long")) - ep1_long = kwargs.get("entry_price_1_long") - position_1_short = abs(kwargs.get("position_1_short")) - ep1_short = kwargs.get("entry_price_1_short") - mmr_b = kwargs.get("maintenance_margin_rate_both") - mmr_l = kwargs.get("maintenance_margin_rate_long") - mmr_s = kwargs.get("maintenance_margin_rate_short") + position_1_both = abs(position_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_l = maintenance_margin_rate_long + mmr_s = maintenance_margin_rate_short if trading_mode == TradingMode.MARGIN and collateral == Collateral.CROSS: # TODO-lev: perform a calculation based on this formula From 6a81730a74847edd6e431fe6ede299ac0639174e Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Sun, 19 Sep 2021 10:18:28 +0530 Subject: [PATCH 6/6] Binance Liquidation Price Hedge-Mode Removed --- freqtrade/enums/__init__.py | 1 - freqtrade/enums/marginmode.py | 10 --- freqtrade/leverage/liquidation_price.py | 97 ++++++++----------------- 3 files changed, 29 insertions(+), 79 deletions(-) delete mode 100644 freqtrade/enums/marginmode.py diff --git a/freqtrade/enums/__init__.py b/freqtrade/enums/__init__.py index 610b5cf43..692a7fcb6 100644 --- a/freqtrade/enums/__init__.py +++ b/freqtrade/enums/__init__.py @@ -7,4 +7,3 @@ from freqtrade.enums.selltype import SellType from freqtrade.enums.signaltype import SignalTagType, SignalType from freqtrade.enums.state import State from freqtrade.enums.tradingmode import TradingMode -from freqtrade.enums.marginmode import MarginMode diff --git a/freqtrade/enums/marginmode.py b/freqtrade/enums/marginmode.py deleted file mode 100644 index 80df6e6fa..000000000 --- a/freqtrade/enums/marginmode.py +++ /dev/null @@ -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" diff --git a/freqtrade/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py index 2379875ac..8a9063a81 100644 --- a/freqtrade/leverage/liquidation_price.py +++ b/freqtrade/leverage/liquidation_price.py @@ -1,6 +1,6 @@ from typing import Optional -from freqtrade.enums import Collateral, TradingMode, MarginMode +from freqtrade.enums import Collateral, TradingMode from freqtrade.exceptions import OperationalException @@ -11,7 +11,13 @@ def liquidation_price( leverage: float, trading_mode: TradingMode, 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]: if trading_mode == TradingMode.SPOT: return None @@ -23,11 +29,16 @@ def liquidation_price( ) 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( - 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": return kraken(open_rate, is_short, leverage, trading_mode, collateral) elif exchange_name.lower() == "ftx": @@ -41,52 +52,37 @@ def exception( exchange: str, trading_mode: TradingMode, collateral: Collateral, - margin_mode: Optional[MarginMode] = None ): """ Raises an exception if exchange used doesn't support desired leverage mode :param exchange: Name of the exchange - :param margin_mode: one-way or hedge :param trading_mode: spot, margin, futures :param collateral: cross, isolated """ - if not margin_mode: - raise OperationalException( - f"{exchange} does not support {collateral.value} {trading_mode.value} trading ") 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( open_rate: float, is_short: bool, leverage: float, - margin_mode: MarginMode, trading_mode: TradingMode, collateral: Collateral, wallet_balance: float, maintenance_margin_ex_1: float, unrealized_pnl_ex_1: float, maintenance_amount_both: float, - maintenance_amount_long: float, - maintenance_amount_short: float, position_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_long: float, - maintenance_margin_rate_short: float, ): r""" Calculates the liquidation price on Binance :param open_rate: open_rate :param is_short: true or false :param leverage: leverage in float - :param margin_mode: one-way or hedge :param trading_mode: spot, margin, futures :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_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 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_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 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 - cum_l = maintenance_amount_long - cum_s = maintenance_amount_short side_1_both = -1 if is_short else 1 position_1_both = abs(position_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_l = maintenance_margin_rate_long - mmr_s = maintenance_margin_rate_short 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, margin_mode) + exception("binance", trading_mode, collateral) elif trading_mode == TradingMode.FUTURES and collateral == Collateral.ISOLATED: # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 # Liquidation Price of USDⓈ-M Futures Contracts Isolated - if margin_mode == MarginMode.HEDGE: - exception("binance", trading_mode, collateral, margin_mode) - - elif margin_mode == MarginMode.ONE_WAY: - # 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) + # 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: # https://www.binance.com/en/support/faq/b3c689c1f50a44cabb3a84e663b81d93 # Liquidation Price of USDⓈ-M Futures Contracts Cross - if margin_mode == MarginMode.HEDGE: - return (wb - tmm_1 + upnl_1 + cum_l + cum_s - (position_1_long * ep1_long) + ( - position_1_short * ep1_short)) / ( - 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=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) + # 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) # If nothing was returned - exception("binance", trading_mode, collateral, margin_mode) + exception("binance", trading_mode, collateral) def kraken( @@ -200,10 +161,10 @@ def kraken( # 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) + exception("kraken", trading_mode, collateral) # If nothing was returned - exception("kraken", trading_mode, collateral) + exception("kraken", trading_mode, collateral) def ftx( @@ -223,7 +184,7 @@ def ftx( """ if collateral == Collateral.CROSS: # TODO-lev: Additional arguments, fill in formulas - exception("ftx", trading_mode, collateral) + exception("ftx", trading_mode, collateral) # If nothing was returned - exception("ftx", trading_mode, collateral) \ No newline at end of file + exception("ftx", trading_mode, collateral)