test_get_liquidation_price/test_get_maintenance_ratio_and_amt_binance/fill_leverage_brackets/test_validate_trading_mode_and_collateral TODO comments

This commit is contained in:
Sam Germain 2022-01-14 06:58:32 -06:00
parent c2f9201512
commit 1f1ac8ce9d
4 changed files with 87 additions and 74 deletions

View File

@ -156,7 +156,7 @@ class Binance(Exchange):
for [notional_floor, mm_ratio] in brkts: for [notional_floor, mm_ratio] in brkts:
amt = ( amt = (
( (
(float(notional_floor) * (float(mm_ratio)) - float(old_ratio)) (float(notional_floor) * (float(mm_ratio) - float(old_ratio)))
) + amt ) + amt
) if old_ratio else 0.0 ) if old_ratio else 0.0
old_ratio = mm_ratio old_ratio = mm_ratio

View File

@ -397,9 +397,9 @@ def test__ccxt_config(default_conf, mocker, trading_mode, collateral, config):
("BNB/BUSD", 0.0, 0.025, 0), ("BNB/BUSD", 0.0, 0.025, 0),
("BNB/USDT", 100.0, 0.0065, 0), ("BNB/USDT", 100.0, 0.0065, 0),
("BTC/USDT", 170.30, 0.004, 0), ("BTC/USDT", 170.30, 0.004, 0),
("BNB/BUSD", 999999.9, 0.1, 0), ("BNB/BUSD", 999999.9, 0.1, 27500.0),
("BNB/USDT", 5000000.0, 0.5, 0), ("BNB/USDT", 5000000.0, 0.15, 233034.99999999994),
("BTC/USDT", 300000000.1, 0.5, 0), ("BTC/USDT", 300000000.1, 0.5, 99891300.0),
]) ])
def test_get_maintenance_ratio_and_amt_binance( def test_get_maintenance_ratio_and_amt_binance(
default_conf, default_conf,
@ -411,29 +411,30 @@ def test_get_maintenance_ratio_and_amt_binance(
): ):
exchange = get_patched_exchange(mocker, default_conf, id="binance") exchange = get_patched_exchange(mocker, default_conf, id="binance")
exchange._leverage_brackets = { exchange._leverage_brackets = {
'BNB/BUSD': [[0.0, 0.025], 'BNB/BUSD': [[0.0, 0.025, 0.0],
[100000.0, 0.05], [100000.0, 0.05, 2500.0],
[500000.0, 0.1], [500000.0, 0.1, 27500.0],
[1000000.0, 0.15], [1000000.0, 0.15, 77499.99999999999],
[2000000.0, 0.25], [2000000.0, 0.25, 277500.0],
[5000000.0, 0.5]], [5000000.0, 0.5, 1527500.0]],
'BNB/USDT': [[0.0, 0.0065], 'BNB/USDT': [[0.0, 0.0065, 0.0],
[10000.0, 0.01], [10000.0, 0.01, 35.00000000000001],
[50000.0, 0.02], [50000.0, 0.02, 535.0],
[250000.0, 0.05], [250000.0, 0.05, 8035.000000000001],
[1000000.0, 0.1], [1000000.0, 0.1, 58035.0],
[2000000.0, 0.125], [2000000.0, 0.125, 108034.99999999999],
[5000000.0, 0.15], [5000000.0, 0.15, 233034.99999999994],
[10000000.0, 0.25]], [10000000.0, 0.25, 1233035.0]],
'BTC/USDT': [[0.0, 0.004], 'BTC/USDT': [[0.0, 0.004, 0.0],
[50000.0, 0.005], [50000.0, 0.005, 50.0],
[250000.0, 0.01], [250000.0, 0.01, 1300.0],
[1000000.0, 0.025], [1000000.0, 0.025, 16300.000000000002],
[5000000.0, 0.05], [5000000.0, 0.05, 141300.0],
[20000000.0, 0.1], [20000000.0, 0.1, 1141300.0],
[50000000.0, 0.125], [50000000.0, 0.125, 2391300.0],
[100000000.0, 0.15], [100000000.0, 0.15, 4891300.0],
[200000000.0, 0.25], [200000000.0, 0.25, 24891300.0],
[300000000.0, 0.5]], [300000000.0, 0.5, 99891300.0]
]
} }
assert exchange.get_maintenance_ratio_and_amt(pair, nominal_value) == (mm_ratio, amt) assert exchange.get_maintenance_ratio_and_amt(pair, nominal_value) == (mm_ratio, amt)

View File

@ -3438,8 +3438,18 @@ def test_set_margin_mode(mocker, default_conf, collateral):
("bittrex", TradingMode.FUTURES, Collateral.CROSS, True), ("bittrex", TradingMode.FUTURES, Collateral.CROSS, True),
("bittrex", TradingMode.FUTURES, Collateral.ISOLATED, True), ("bittrex", TradingMode.FUTURES, Collateral.ISOLATED, True),
("gateio", TradingMode.MARGIN, Collateral.ISOLATED, True), ("gateio", TradingMode.MARGIN, Collateral.ISOLATED, True),
("okex", TradingMode.SPOT, None, False),
("okex", TradingMode.MARGIN, Collateral.CROSS, True),
("okex", TradingMode.MARGIN, Collateral.ISOLATED, True),
("okex", TradingMode.FUTURES, Collateral.CROSS, True),
# TODO-lev: Remove once implemented ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
("gateio", TradingMode.FUTURES, Collateral.ISOLATED, False),
# ("okex", TradingMode.FUTURES, Collateral.ISOLATED, False), # TODO-lev: uncomment once impleme
("okex", TradingMode.FUTURES, Collateral.ISOLATED, True), # TODO-lev: remove once implemented
# * Remove once implemented
("binance", TradingMode.MARGIN, Collateral.CROSS, True), ("binance", TradingMode.MARGIN, Collateral.CROSS, True),
("binance", TradingMode.FUTURES, Collateral.CROSS, True), ("binance", TradingMode.FUTURES, Collateral.CROSS, True),
("kraken", TradingMode.MARGIN, Collateral.CROSS, True), ("kraken", TradingMode.MARGIN, Collateral.CROSS, True),
@ -3449,17 +3459,15 @@ def test_set_margin_mode(mocker, default_conf, collateral):
("gateio", TradingMode.MARGIN, Collateral.CROSS, True), ("gateio", TradingMode.MARGIN, Collateral.CROSS, True),
("gateio", TradingMode.FUTURES, Collateral.CROSS, True), ("gateio", TradingMode.FUTURES, Collateral.CROSS, True),
# TODO-lev: Uncomment once implemented # * Uncomment once implemented
# ("binance", TradingMode.MARGIN, Collateral.CROSS, False), # ("binance", TradingMode.MARGIN, Collateral.CROSS, False),
# ("binance", TradingMode.FUTURES, Collateral.CROSS, False), # ("binance", TradingMode.FUTURES, Collateral.CROSS, False),
("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
# ("kraken", TradingMode.MARGIN, Collateral.CROSS, False), # ("kraken", TradingMode.MARGIN, Collateral.CROSS, False),
# ("kraken", TradingMode.FUTURES, Collateral.CROSS, False), # ("kraken", TradingMode.FUTURES, Collateral.CROSS, False),
# ("ftx", TradingMode.MARGIN, Collateral.CROSS, False), # ("ftx", TradingMode.MARGIN, Collateral.CROSS, False),
# ("ftx", TradingMode.FUTURES, Collateral.CROSS, False), # ("ftx", TradingMode.FUTURES, Collateral.CROSS, False),
# ("gateio", TradingMode.MARGIN, Collateral.CROSS, False), # ("gateio", TradingMode.MARGIN, Collateral.CROSS, False),
# ("gateio", TradingMode.FUTURES, Collateral.CROSS, False), # ("gateio", TradingMode.FUTURES, Collateral.CROSS, False),
("gateio", TradingMode.FUTURES, Collateral.ISOLATED, False),
]) ])
def test_validate_trading_mode_and_collateral( def test_validate_trading_mode_and_collateral(
default_conf, default_conf,
@ -3583,36 +3591,39 @@ def test_calculate_funding_fees(
def test_get_liquidation_price(mocker, default_conf): def test_get_liquidation_price(mocker, default_conf):
api_mock = MagicMock() api_mock = MagicMock()
api_mock.fetch_positions = MagicMock(return_value=[{ positions = [
'info': {}, {
'symbol': 'NEAR/USDT:USDT', 'info': {},
'timestamp': 1642164737148, 'symbol': 'NEAR/USDT:USDT',
'datetime': '2022-01-14T12:52:17.148Z', 'timestamp': 1642164737148,
'initialMargin': 1.51072, 'datetime': '2022-01-14T12:52:17.148Z',
'initialMarginPercentage': 0.1, 'initialMargin': 1.51072,
'maintenanceMargin': 0.38916147, 'initialMarginPercentage': 0.1,
'maintenanceMarginPercentage': 0.025, 'maintenanceMargin': 0.38916147,
'entryPrice': 18.884, 'maintenanceMarginPercentage': 0.025,
'notional': 15.1072, 'entryPrice': 18.884,
'leverage': 9.97, 'notional': 15.1072,
'unrealizedPnl': 0.0048, 'leverage': 9.97,
'contracts': 8, 'unrealizedPnl': 0.0048,
'contractSize': 0.1, 'contracts': 8,
'marginRatio': None, 'contractSize': 0.1,
'liquidationPrice': 17.47, 'marginRatio': None,
'markPrice': 18.89, 'liquidationPrice': 17.47,
'collateral': 1.52549075, 'markPrice': 18.89,
'marginType': 'isolated', 'collateral': 1.52549075,
'side': 'buy', 'marginType': 'isolated',
'percentage': 0.003177292946409658 'side': 'buy',
}]) 'percentage': 0.003177292946409658
}
]
api_mock.fetch_positions = MagicMock(return_value=positions)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
exchange_has=MagicMock(return_value=True), exchange_has=MagicMock(return_value=True),
) )
default_conf['dry_run'] = False default_conf['dry_run'] = False
exchange = get_patched_exchange(mocker, default_conf) exchange = get_patched_exchange(mocker, default_conf, api_mock)
liq_price = exchange.get_liquidation_price('NEAR/USDT:USDT') liq_price = exchange.get_liquidation_price('NEAR/USDT:USDT')
assert liq_price == 17.47 assert liq_price == 17.47

View File

@ -708,10 +708,10 @@ def test_process_informative_pairs_added(default_conf_usdt, ticker_usdt, mocker)
@pytest.mark.parametrize("is_short,trading_mode,exchange_name,margin_mode,liq_price", [ @pytest.mark.parametrize("is_short,trading_mode,exchange_name,margin_mode,liq_price", [
(False, 'spot', 'binance', '', None), (False, 'spot', 'binance', None, None),
(True, 'spot', 'binance', '', None), (True, 'spot', 'binance', None, None),
(False, 'spot', 'gateio', '', None), (False, 'spot', 'gateio', None, None),
(True, 'spot', 'gateio', '', None), (True, 'spot', 'gateio', None, None),
(True, 'futures', 'binance', 'isolated', 13.217821782178218), (True, 'futures', 'binance', 'isolated', 13.217821782178218),
(False, 'futures', 'binance', 'isolated', 6.717171717171718), (False, 'futures', 'binance', 'isolated', 6.717171717171718),
(True, 'futures', 'gateio', 'isolated', 13.198706526760379), (True, 'futures', 'gateio', 'isolated', 13.198706526760379),
@ -745,7 +745,8 @@ def test_execute_entry(mocker, default_conf_usdt, fee, limit_order,
order = limit_order[enter_side(is_short)] order = limit_order[enter_side(is_short)]
default_conf_usdt['trading_mode'] = trading_mode default_conf_usdt['trading_mode'] = trading_mode
leverage = 1.0 if trading_mode == 'spot' else 5.0 leverage = 1.0 if trading_mode == 'spot' else 5.0
default_conf_usdt['collateral'] = margin_mode if margin_mode:
default_conf_usdt['collateral'] = margin_mode
patch_RPCManager(mocker) patch_RPCManager(mocker)
patch_exchange(mocker) patch_exchange(mocker)
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
@ -4832,16 +4833,16 @@ def test_update_funding_fees(
nominal_value = mark_price * size nominal_value = mark_price * size
funding_fee = nominal_value * funding_rate funding_fee = nominal_value * funding_rate
size = 123 size = 123
"LTC/BTC" "LTC/USDT"
time: 0, mark: 3.3, fundRate: 0.00032583, nominal_value: 405.9, fundFee: 0.132254397 time: 0, mark: 3.3, fundRate: 0.00032583, nominal_value: 405.9, fundFee: 0.132254397
time: 8, mark: 3.2, fundRate: 0.00024472, nominal_value: 393.6, fundFee: 0.096321792 time: 8, mark: 3.2, fundRate: 0.00024472, nominal_value: 393.6, fundFee: 0.096321792
"ETH/BTC" "ETH/USDT"
time: 0, mark: 2.4, fundRate: 0.0001, nominal_value: 295.2, fundFee: 0.02952 time: 0, mark: 2.4, fundRate: 0.0001, nominal_value: 295.2, fundFee: 0.02952
time: 8, mark: 2.5, fundRate: 0.0001, nominal_value: 307.5, fundFee: 0.03075 time: 8, mark: 2.5, fundRate: 0.0001, nominal_value: 307.5, fundFee: 0.03075
"ETC/BTC" "ETC/USDT"
time: 0, mark: 4.3, fundRate: 0.00031077, nominal_value: 528.9, fundFee: 0.164366253 time: 0, mark: 4.3, fundRate: 0.00031077, nominal_value: 528.9, fundFee: 0.164366253
time: 8, mark: 4.1, fundRate: 0.00022655, nominal_value: 504.3, fundFee: 0.114249165 time: 8, mark: 4.1, fundRate: 0.00022655, nominal_value: 504.3, fundFee: 0.114249165
"XRP/BTC" "XRP/USDT"
time: 0, mark: 1.2, fundRate: 0.00049426, nominal_value: 147.6, fundFee: 0.072952776 time: 0, mark: 1.2, fundRate: 0.00049426, nominal_value: 147.6, fundFee: 0.072952776
time: 8, mark: 1.2, fundRate: 0.00032715, nominal_value: 147.6, fundFee: 0.04828734 time: 8, mark: 1.2, fundRate: 0.00032715, nominal_value: 147.6, fundFee: 0.04828734
""" """
@ -4865,19 +4866,19 @@ def test_update_funding_fees(
# 16:00 entry is actually never used # 16:00 entry is actually never used
# But should be kept in the test to ensure we're filtering correctly. # But should be kept in the test to ensure we're filtering correctly.
funding_rates = { funding_rates = {
"LTC/BTC": "LTC/USDT":
DataFrame([ DataFrame([
[date_midnight, 0.00032583, 0, 0, 0, 0], [date_midnight, 0.00032583, 0, 0, 0, 0],
[date_eight, 0.00024472, 0, 0, 0, 0], [date_eight, 0.00024472, 0, 0, 0, 0],
[date_sixteen, 0.00024472, 0, 0, 0, 0], [date_sixteen, 0.00024472, 0, 0, 0, 0],
], columns=columns), ], columns=columns),
"ETH/BTC": "ETH/USDT":
DataFrame([ DataFrame([
[date_midnight, 0.0001, 0, 0, 0, 0], [date_midnight, 0.0001, 0, 0, 0, 0],
[date_eight, 0.0001, 0, 0, 0, 0], [date_eight, 0.0001, 0, 0, 0, 0],
[date_sixteen, 0.0001, 0, 0, 0, 0], [date_sixteen, 0.0001, 0, 0, 0, 0],
], columns=columns), ], columns=columns),
"XRP/BTC": "XRP/USDT":
DataFrame([ DataFrame([
[date_midnight, 0.00049426, 0, 0, 0, 0], [date_midnight, 0.00049426, 0, 0, 0, 0],
[date_eight, 0.00032715, 0, 0, 0, 0], [date_eight, 0.00032715, 0, 0, 0, 0],
@ -4886,19 +4887,19 @@ def test_update_funding_fees(
} }
mark_prices = { mark_prices = {
"LTC/BTC": "LTC/USDT":
DataFrame([ DataFrame([
[date_midnight, 3.3, 0, 0, 0, 0], [date_midnight, 3.3, 0, 0, 0, 0],
[date_eight, 3.2, 0, 0, 0, 0], [date_eight, 3.2, 0, 0, 0, 0],
[date_sixteen, 3.2, 0, 0, 0, 0], [date_sixteen, 3.2, 0, 0, 0, 0],
], columns=columns), ], columns=columns),
"ETH/BTC": "ETH/USDT":
DataFrame([ DataFrame([
[date_midnight, 2.4, 0, 0, 0, 0], [date_midnight, 2.4, 0, 0, 0, 0],
[date_eight, 2.5, 0, 0, 0, 0], [date_eight, 2.5, 0, 0, 0, 0],
[date_sixteen, 2.5, 0, 0, 0, 0], [date_sixteen, 2.5, 0, 0, 0, 0],
], columns=columns), ], columns=columns),
"XRP/BTC": "XRP/USDT":
DataFrame([ DataFrame([
[date_midnight, 1.2, 0, 0, 0, 0], [date_midnight, 1.2, 0, 0, 0, 0],
[date_eight, 1.2, 0, 0, 0, 0], [date_eight, 1.2, 0, 0, 0, 0],
@ -4935,9 +4936,9 @@ def test_update_funding_fees(
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
# initial funding fees, # initial funding fees,
freqtrade.execute_entry('ETH/BTC', 123, is_short=is_short) freqtrade.execute_entry('ETH/USDT', 123, is_short=is_short)
freqtrade.execute_entry('LTC/BTC', 2.0, is_short=is_short) freqtrade.execute_entry('LTC/USDT', 2.0, is_short=is_short)
freqtrade.execute_entry('XRP/BTC', 123, is_short=is_short) freqtrade.execute_entry('XRP/USDT', 123, is_short=is_short)
multipl = 1 if is_short else -1 multipl = 1 if is_short else -1
trades = Trade.get_open_trades() trades = Trade.get_open_trades()
assert len(trades) == 3 assert len(trades) == 3