exchange.liquidation_price methods combined, dry_run check on exchange for liquidation price

This commit is contained in:
Sam Germain
2022-01-29 18:47:17 -06:00
parent 143c37d36f
commit b8f4cebce7
8 changed files with 140 additions and 124 deletions

View File

@@ -817,29 +817,42 @@ def get_markets():
'symbol': 'ETH/USDT',
'base': 'ETH',
'quote': 'USDT',
'spot': True,
'future': True,
'swap': True,
'margin': True,
'settle': 'USDT',
'baseId': 'ETH',
'quoteId': 'USDT',
'settleId': 'USDT',
'type': 'spot',
'contractSize': None,
'spot': True,
'margin': True,
'swap': True,
'future': True,
'option': False,
'active': True,
'contract': True,
'linear': True,
'inverse': False,
'taker': 0.0006,
'maker': 0.0002,
'contractSize': 1,
'expiry': 1680220800000,
'expiryDateTime': '2023-03-31T00:00:00.000Z',
'strike': None,
'optionType': None,
'precision': {
'amount': 8,
'price': 8
'price': 8,
},
'limits': {
'leverage': {
'min': 1,
'max': 100,
},
'amount': {
'min': 0.02214286,
'max': None
'max': None,
},
'price': {
'min': 1e-08,
'max': None
},
'leverage': {
'min': None,
'max': None,
},
'cost': {
@@ -847,8 +860,9 @@ def get_markets():
'max': None,
},
},
'active': True,
'info': {},
'info': {
'maintenance_rate': '0.005',
},
},
'LTC/USDT': {
'id': 'USDT-LTC',
@@ -1110,7 +1124,6 @@ def get_markets():
'swap': True,
'futures': False,
'option': False,
'derivative': True,
'contract': True,
'linear': True,
'inverse': False,

View File

@@ -3975,13 +3975,13 @@ def test__amount_to_contracts(
@pytest.mark.parametrize('exchange_name,open_rate,is_short,leverage,trading_mode,collateral', [
# Bittrex
('bittrex', 2.0, False, 3.0, spot, None),
('bittrex', 2.0, False, 1.0, spot, cross),
('bittrex', 2.0, True, 3.0, spot, isolated),
('bittrex', 2.0, False, 3.0, 'spot', None),
('bittrex', 2.0, False, 1.0, 'spot', 'cross'),
('bittrex', 2.0, True, 3.0, 'spot', 'isolated'),
# Binance
('binance', 2.0, False, 3.0, spot, None),
('binance', 2.0, False, 1.0, spot, cross),
('binance', 2.0, True, 3.0, spot, isolated),
('binance', 2.0, False, 3.0, 'spot', None),
('binance', 2.0, False, 1.0, 'spot', 'cross'),
('binance', 2.0, True, 3.0, 'spot', 'isolated'),
])
def test_liquidation_price_is_none(
mocker,
@@ -3996,14 +3996,12 @@ def test_liquidation_price_is_none(
default_conf['trading_mode'] = trading_mode
default_conf['collateral'] = collateral
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
assert exchange.liquidation_price(
assert exchange.get_liquidation_price(
pair='DOGE/USDT',
open_rate=open_rate,
is_short=is_short,
mm_ratio=1535443.01,
position=71200.81144,
wallet_balance=-56354.57,
taker_fee_rate=0.01,
maintenance_amt=3683.979,
mm_ex_1=0.10,
upnl_ex_1=0.0
) is None
@@ -4014,13 +4012,13 @@ def test_liquidation_price_is_none(
'mm_ex_1, upnl_ex_1, maintenance_amt, position, open_rate, '
'mm_ratio, expected',
[
("binance", False, 1, futures, isolated, 1535443.01, 0.0,
("binance", False, 1, 'futures', 'isolated', 1535443.01, 0.0,
0.0, 135365.00, 3683.979, 1456.84, 0.10, 1114.78),
("binance", False, 1, futures, isolated, 1535443.01, 0.0,
("binance", False, 1, 'futures', 'isolated', 1535443.01, 0.0,
0.0, 16300.000, 109.488, 32481.980, 0.025, 18778.73),
("binance", False, 1, futures, cross, 1535443.01, 71200.81144,
("binance", False, 1, 'futures', 'cross', 1535443.01, 71200.81144,
-56354.57, 135365.00, 3683.979, 1456.84, 0.10, 1153.26),
("binance", False, 1, futures, cross, 1535443.01, 356512.508,
("binance", False, 1, 'futures', 'cross', 1535443.01, 356512.508,
-448192.89, 16300.000, 109.488, 32481.980, 0.025, 26316.89)
])
def test_liquidation_price(
@@ -4030,13 +4028,13 @@ def test_liquidation_price(
default_conf['trading_mode'] = trading_mode
default_conf['collateral'] = collateral
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
assert isclose(round(exchange.liquidation_price(
exchange.get_maintenance_ratio_and_amt = MagicMock(return_value=(mm_ratio, maintenance_amt))
assert isclose(round(exchange.get_liquidation_price(
pair='DOGE/USDT',
open_rate=open_rate,
is_short=is_short,
wallet_balance=wallet_balance,
mm_ex_1=mm_ex_1,
upnl_ex_1=upnl_ex_1,
maintenance_amt=maintenance_amt,
position=position,
mm_ratio=mm_ratio
), 2), expected)

View File

@@ -735,11 +735,11 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
((wb + cum_b) - (side_1 * position * ep1)) / ((position * mmr_b) - (side_1 * position))
((2 + 0.01) - (1 * 1 * 10)) / ((1 * 0.01) - (1 * 1)) = 8.070707070707071
exchange_name = gateio, is_short = true
exchange_name = gateio/okex, is_short = true
(open_rate + (wallet_balance / position)) / (1 + (mm_ratio + taker_fee_rate))
(10 + (2 / 1)) / (1 + (0.01 + 0.0006)) = 11.87413417771621
exchange_name = gateio, is_short = false
exchange_name = gateio/okex, is_short = false
(open_rate - (wallet_balance / position)) / (1 - (mm_ratio + taker_fee_rate))
(10 - (2 / 1)) / (1 - (0.01 + 0.0006)) = 8.085708510208207
"""
@@ -747,10 +747,12 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order = limit_order[enter_side(is_short)]
default_conf_usdt['trading_mode'] = trading_mode
leverage = 1.0 if trading_mode == 'spot' else 5.0
default_conf_usdt['exchange']['name'] = exchange_name
if margin_mode:
default_conf_usdt['collateral'] = margin_mode
mocker.patch('freqtrade.exchange.Gateio.validate_ordertypes')
patch_RPCManager(mocker)
patch_exchange(mocker)
patch_exchange(mocker, id=exchange_name)
freqtrade = FreqtradeBot(default_conf_usdt)
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=False)
freqtrade.strategy.leverage = MagicMock(return_value=leverage)