From 544b88f026490af59594b5713d96cad9d784e188 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sun, 19 Sep 2021 00:05:39 -0600 Subject: [PATCH] added one test for binance isolated liq --- freqtrade/enums/__init__.py | 1 - freqtrade/leverage/liquidation_price.py | 29 ++++++++++--- tests/leverage/test_liquidation_price.py | 55 ++++++++++++++++-------- 3 files changed, 60 insertions(+), 25 deletions(-) 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/leverage/liquidation_price.py b/freqtrade/leverage/liquidation_price.py index 8a9063a81..2eb660394 100644 --- a/freqtrade/leverage/liquidation_price.py +++ b/freqtrade/leverage/liquidation_price.py @@ -19,6 +19,7 @@ def liquidation_price( entry_price_1_both: Optional[float], maintenance_margin_rate_both: Optional[float] ) -> Optional[float]: + if trading_mode == TradingMode.SPOT: return None @@ -29,16 +30,34 @@ def liquidation_price( ) if exchange_name.lower() == "binance": - 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: + 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) + 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": diff --git a/tests/leverage/test_liquidation_price.py b/tests/leverage/test_liquidation_price.py index 687dd57f4..ade1d83ea 100644 --- a/tests/leverage/test_liquidation_price.py +++ b/tests/leverage/test_liquidation_price.py @@ -46,7 +46,14 @@ def test_liquidation_price_is_none( is_short, leverage, trading_mode, - collateral + collateral, + 1535443.01, + 71200.81144, + -56354.57, + 135365.00, + 3683.979, + 1456.84, + 0.10, ) is None @@ -80,17 +87,14 @@ def test_liquidation_price_exception_thrown( @pytest.mark.parametrize( - 'exchange_name,open_rate,is_short,leverage,trading_mode,collateral,result', [ + ('exchange_name,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,liq_price'), [ # Binance - ('binance', "2.0", False, "1.0", margin, cross, 1.0), - ('binance', "2.0", False, "1.0", futures, cross, 1.0), - ('binance', "2.0", False, "1.0", futures, isolated, 1.0), + ("binance", 0.0, False, 1, futures, cross, 1535443.01, + 71200.81144, -56354.57, 135365.00, 3683.979, 1456.84, 0.10, 1153.26) # Kraken - ('kraken', "2.0", True, "3.0", margin, cross, 1.0), - ('kraken', "2.0", True, "3.0", futures, cross, 1.0), # FTX - ('ftx', "2.0", False, "3.0", margin, cross, 1.0), - ('ftx', "2.0", False, "3.0", futures, cross, 1.0), ] ) def test_liquidation_price( @@ -100,14 +104,27 @@ def test_liquidation_price( leverage, trading_mode, collateral, - result + 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 ): - # assert liquidation_price( - # exchange_name, - # open_rate, - # is_short, - # leverage, - # trading_mode, - # collateral - # ) == result - return # Here to avoid indent error + assert liquidation_price( + exchange_name, + 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 + ) == liq_price