merged with aranova4/feat/binance_liq
This commit is contained in:
commit
1c0c7d802f
@ -14,10 +14,10 @@ def liquidation_price(
|
|||||||
wallet_balance: Optional[float],
|
wallet_balance: Optional[float],
|
||||||
maintenance_margin_ex_1: Optional[float],
|
maintenance_margin_ex_1: Optional[float],
|
||||||
unrealized_pnl_ex_1: Optional[float],
|
unrealized_pnl_ex_1: Optional[float],
|
||||||
maintenance_amount_both: Optional[float],
|
maintenance_amount: Optional[float],
|
||||||
position_1_both: Optional[float],
|
position_1: Optional[float],
|
||||||
entry_price_1_both: Optional[float],
|
entry_price_1: Optional[float],
|
||||||
maintenance_margin_rate_both: Optional[float]
|
maintenance_margin_rate: Optional[float]
|
||||||
) -> Optional[float]:
|
) -> Optional[float]:
|
||||||
|
|
||||||
if trading_mode == TradingMode.SPOT:
|
if trading_mode == TradingMode.SPOT:
|
||||||
@ -30,34 +30,16 @@ def liquidation_price(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if exchange_name.lower() == "binance":
|
if exchange_name.lower() == "binance":
|
||||||
if (
|
if not wallet_balance or not maintenance_margin_ex_1 or not unrealized_pnl_ex_1 or not maintenance_amount \
|
||||||
not wallet_balance or
|
or not position_1 or not entry_price_1 or not maintenance_margin_rate:
|
||||||
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"Parameters wallet_balance, maintenance_margin_ex_1, unrealized_pnl_ex_1, maintenance_amount_both, "
|
f"Parameters wallet_balance, maintenance_margin_ex_1, unrealized_pnl_ex_1, maintenance_amount, "
|
||||||
f"position_1_both, entry_price_1_both, maintenance_margin_rate_both is required by liquidation_price "
|
f"position_1, entry_price_1, maintenance_margin_rate is required by liquidation_price "
|
||||||
f"when exchange is {exchange_name.lower()}")
|
f"when exchange is {exchange_name.lower()}")
|
||||||
|
|
||||||
return binance(
|
return binance(open_rate, is_short, leverage, trading_mode, collateral, wallet_balance, maintenance_margin_ex_1,
|
||||||
open_rate,
|
unrealized_pnl_ex_1, maintenance_amount, position_1, entry_price_1,
|
||||||
is_short,
|
maintenance_margin_rate)
|
||||||
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":
|
||||||
@ -92,10 +74,10 @@ def binance(
|
|||||||
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: float,
|
||||||
position_1_both: float,
|
position_1: float,
|
||||||
entry_price_1_both: float,
|
entry_price_1: float,
|
||||||
maintenance_margin_rate_both: float,
|
maintenance_margin_rate: float,
|
||||||
):
|
):
|
||||||
r"""
|
r"""
|
||||||
Calculates the liquidation price on Binance
|
Calculates the liquidation price on Binance
|
||||||
@ -114,24 +96,24 @@ def binance(
|
|||||||
:param unrealized_pnl_ex_1: Unrealized PNL of all other contracts, excluding Contract 1.
|
:param unrealized_pnl_ex_1: Unrealized PNL of all other contracts, excluding Contract 1.
|
||||||
If it is an isolated margin mode, then UPNL=0
|
If it is an isolated margin mode, then UPNL=0
|
||||||
|
|
||||||
:param maintenance_amount_both: Maintenance Amount of BOTH position (one-way mode)
|
:param maintenance_amount: Maintenance Amount of position (one-way mode)
|
||||||
|
|
||||||
:param position_1_both: Absolute value of BOTH position size (one-way mode)
|
:param position_1: Absolute value of position size (one-way mode)
|
||||||
|
|
||||||
:param entry_price_1_both: Entry Price of BOTH position (one-way mode)
|
:param entry_price_1: Entry Price of position (one-way mode)
|
||||||
|
|
||||||
:param maintenance_margin_rate_both: Maintenance margin rate of BOTH position (one-way mode)
|
:param maintenance_margin_rate: Maintenance margin rate of position (one-way 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
|
||||||
side_1_both = -1 if is_short else 1
|
side_1 = -1 if is_short else 1
|
||||||
position_1_both = abs(position_1_both)
|
position_1 = abs(position_1)
|
||||||
ep1_both = entry_price_1_both
|
ep1 = entry_price_1
|
||||||
mmr_b = maintenance_margin_rate_both
|
mmr_b = maintenance_margin_rate
|
||||||
|
|
||||||
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
|
||||||
@ -142,16 +124,16 @@ def binance(
|
|||||||
# Liquidation Price of USDⓈ-M Futures Contracts Isolated
|
# Liquidation Price of USDⓈ-M Futures Contracts Isolated
|
||||||
|
|
||||||
# Isolated margin mode, then TMM=0,UPNL=0
|
# Isolated margin mode, then TMM=0,UPNL=0
|
||||||
return (wb + cum_b - (side_1_both * position_1_both * ep1_both)) / (
|
return (wb + cum_b - side_1 * position_1 * ep1) / (
|
||||||
position_1_both * mmr_b - side_1_both * position_1_both)
|
position_1 * mmr_b - side_1 * position_1)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
# Isolated margin mode, then TMM=0,UPNL=0
|
# Isolated margin mode, then TMM=0,UPNL=0
|
||||||
return (wb - tmm_1 + upnl_1 + cum_b - (side_1_both * position_1_both * ep1_both)) / (
|
return (wb - tmm_1 + upnl_1 + cum_b - side_1 * position_1 * ep1) / (
|
||||||
position_1_both * mmr_b - side_1_both * position_1_both)
|
position_1 * mmr_b - side_1 * position_1)
|
||||||
|
|
||||||
# If nothing was returned
|
# If nothing was returned
|
||||||
exception("binance", trading_mode, collateral)
|
exception("binance", trading_mode, collateral)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
from math import isclose
|
||||||
|
|
||||||
from freqtrade.enums import Collateral, TradingMode
|
from freqtrade.enums import Collateral, TradingMode
|
||||||
from freqtrade.leverage import liquidation_price
|
from freqtrade.leverage import liquidation_price
|
||||||
@ -87,44 +88,34 @@ def test_liquidation_price_exception_thrown(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('exchange_name,open_rate,is_short,leverage,trading_mode,collateral,wallet_balance,'
|
'exchange_name, open_rate, is_short, leverage, trading_mode, collateral, wallet_balance, maintenance_margin_ex_1, '
|
||||||
'maintenance_margin_ex_1,unrealized_pnl_ex_1,maintenance_amount_both,'
|
'unrealized_pnl_ex_1, maintenance_amount, position_1, entry_price_1, maintenance_margin_rate, '
|
||||||
'position_1_both,entry_price_1_both,maintenance_margin_rate_both,liq_price'), [
|
'expected',
|
||||||
# Binance
|
[
|
||||||
("binance", 0.0, False, 1, futures, cross, 1535443.01,
|
("binance", 0.0, False, 1, TradingMode.FUTURES, Collateral.ISOLATED, 1535443.01, 71200.81144, -56354.57,
|
||||||
71200.81144, -56354.57, 135365.00, 3683.979, 1456.84, 0.10, 1153.26)
|
135365.00, 3683.979, 1456.84, 0.10, 1114.78),
|
||||||
# Kraken
|
("binance", 0.0, False, 1, TradingMode.FUTURES, Collateral.ISOLATED, 1535443.01, 356512.508, -448192.89,
|
||||||
# FTX
|
16300.000, 109.488, 32481.980, 0.025, 18778.73),
|
||||||
]
|
("binance", 0.0, False, 1, TradingMode.FUTURES, Collateral.CROSS, 1535443.01, 71200.81144, -56354.57, 135365.00,
|
||||||
)
|
3683.979, 1456.84, 0.10, 1153.26),
|
||||||
def test_liquidation_price(
|
("binance", 0.0, False, 1, TradingMode.FUTURES, Collateral.CROSS, 1535443.01, 356512.508, -448192.89, 16300.000,
|
||||||
exchange_name,
|
109.488, 32481.980, 0.025, 26316.89)
|
||||||
open_rate,
|
])
|
||||||
is_short,
|
def test_liquidation_price(exchange_name, open_rate, is_short, leverage, trading_mode, collateral, wallet_balance,
|
||||||
leverage,
|
maintenance_margin_ex_1, unrealized_pnl_ex_1, maintenance_amount, position_1,
|
||||||
trading_mode,
|
entry_price_1, maintenance_margin_rate, expected):
|
||||||
collateral,
|
assert isclose(round(liquidation_price(
|
||||||
wallet_balance,
|
exchange_name=exchange_name,
|
||||||
maintenance_margin_ex_1,
|
open_rate=open_rate,
|
||||||
unrealized_pnl_ex_1,
|
is_short=is_short,
|
||||||
maintenance_amount_both,
|
leverage=leverage,
|
||||||
position_1_both,
|
trading_mode=trading_mode,
|
||||||
entry_price_1_both,
|
collateral=collateral,
|
||||||
maintenance_margin_rate_both,
|
wallet_balance=wallet_balance,
|
||||||
liq_price
|
maintenance_margin_ex_1=maintenance_margin_ex_1,
|
||||||
):
|
unrealized_pnl_ex_1=unrealized_pnl_ex_1,
|
||||||
assert liquidation_price(
|
maintenance_amount=maintenance_amount,
|
||||||
exchange_name,
|
position_1=position_1,
|
||||||
open_rate,
|
entry_price_1=entry_price_1,
|
||||||
is_short,
|
maintenance_margin_rate=maintenance_margin_rate
|
||||||
leverage,
|
), 2), expected)
|
||||||
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
|
|
||||||
) == liq_price
|
|
||||||
|
Loading…
Reference in New Issue
Block a user