fixed liq-buffer tests

This commit is contained in:
Sam Germain 2022-02-11 03:48:09 -06:00 committed by Matthias
parent fb3a6e2ce8
commit 6ae85f9be1
3 changed files with 33 additions and 15 deletions

View File

@ -2072,7 +2072,7 @@ class Exchange:
mm_ex_1=mm_ex_1, mm_ex_1=mm_ex_1,
upnl_ex_1=upnl_ex_1 upnl_ex_1=upnl_ex_1
) )
else:
try: try:
positions = self._api.fetch_positions([pair]) positions = self._api.fetch_positions([pair])
if len(positions) > 0: if len(positions) > 0:

View File

@ -3664,6 +3664,7 @@ def test_get_liquidation_price(mocker, default_conf):
default_conf['dry_run'] = False default_conf['dry_run'] = False
default_conf['trading_mode'] = 'futures' default_conf['trading_mode'] = 'futures'
default_conf['margin_mode'] = 'isolated' default_conf['margin_mode'] = 'isolated'
default_conf['liquidation_buffer'] = 0.0
exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange = get_patched_exchange(mocker, default_conf, api_mock)
liq_price = exchange.get_liquidation_price( liq_price = exchange.get_liquidation_price(
@ -3675,6 +3676,17 @@ def test_get_liquidation_price(mocker, default_conf):
) )
assert liq_price == 17.47 assert liq_price == 17.47
default_conf['liquidation_buffer'] = 0.05
exchange = get_patched_exchange(mocker, default_conf, api_mock)
liq_price = exchange.get_liquidation_price(
pair='NEAR/USDT:USDT',
open_rate=0.0,
is_short=False,
position=0.0,
wallet_balance=0.0,
)
assert liq_price == 18.8133
ccxt_exceptionhandlers( ccxt_exceptionhandlers(
mocker, mocker,
default_conf, default_conf,
@ -4073,6 +4085,7 @@ def test_liquidation_price(
): ):
default_conf['trading_mode'] = trading_mode default_conf['trading_mode'] = trading_mode
default_conf['margin_mode'] = margin_mode default_conf['margin_mode'] = margin_mode
default_conf['liquidation_buffer'] = 0.0
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(mm_ratio, maintenance_amt)) exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(mm_ratio, maintenance_amt))
assert isclose(round(exchange.get_liquidation_price( assert isclose(round(exchange.get_liquidation_price(

View File

@ -4813,6 +4813,7 @@ def test_get_valid_price(mocker, default_conf_usdt) -> None:
assert valid_price_at_min_alwd < proposed_price assert valid_price_at_min_alwd < proposed_price
@pytest.mark.parametrize('liquidation_buffer', [0.0, 0.05])
@pytest.mark.parametrize( @pytest.mark.parametrize(
"is_short,trading_mode,exchange_name,margin_mode,leverage,open_rate,amount,expected_liq", [ "is_short,trading_mode,exchange_name,margin_mode,leverage,open_rate,amount,expected_liq", [
(False, 'spot', 'binance', '', 5.0, 10.0, 1.0, None), (False, 'spot', 'binance', '', 5.0, 10.0, 1.0, None),
@ -4854,6 +4855,7 @@ def test_leverage_prep(
open_rate, open_rate,
amount, amount,
expected_liq, expected_liq,
liquidation_buffer,
): ):
""" """
position = 0.2 * 5 position = 0.2 * 5
@ -4907,6 +4909,7 @@ def test_leverage_prep(
leverage = 5, open_rate = 8, amount = 1.0 leverage = 5, open_rate = 8, amount = 1.0
(8 - (1.6 / 1.0)) / (1 + (0.01 + 0.0006)) = 6.332871561448645 (8 - (1.6 / 1.0)) / (1 + (0.01 + 0.0006)) = 6.332871561448645
""" """
default_conf_usdt['liquidation_buffer'] = liquidation_buffer
default_conf_usdt['trading_mode'] = trading_mode default_conf_usdt['trading_mode'] = trading_mode
default_conf_usdt['exchange']['name'] = exchange_name default_conf_usdt['exchange']['name'] = exchange_name
default_conf_usdt['margin_mode'] = margin_mode default_conf_usdt['margin_mode'] = margin_mode
@ -4931,6 +4934,8 @@ def test_leverage_prep(
if expected_liq is None: if expected_liq is None:
assert liq is None assert liq is None
else: else:
buffer_amount = liquidation_buffer * abs(open_rate - expected_liq)
expected_liq = expected_liq - buffer_amount if is_short else expected_liq + buffer_amount
isclose(expected_liq, liq) isclose(expected_liq, liq)