From 1f1ac8ce9dae0e1a14435069fb110c9d55aad5c1 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Fri, 14 Jan 2022 06:58:32 -0600 Subject: [PATCH] test_get_liquidation_price/test_get_maintenance_ratio_and_amt_binance/fill_leverage_brackets/test_validate_trading_mode_and_collateral TODO comments --- freqtrade/exchange/binance.py | 2 +- tests/exchange/test_binance.py | 55 ++++++++++++++------------- tests/exchange/test_exchange.py | 67 +++++++++++++++++++-------------- tests/test_freqtradebot.py | 37 +++++++++--------- 4 files changed, 87 insertions(+), 74 deletions(-) diff --git a/freqtrade/exchange/binance.py b/freqtrade/exchange/binance.py index 0b434d9d3..e1623025a 100644 --- a/freqtrade/exchange/binance.py +++ b/freqtrade/exchange/binance.py @@ -156,7 +156,7 @@ class Binance(Exchange): for [notional_floor, mm_ratio] in brkts: amt = ( ( - (float(notional_floor) * (float(mm_ratio)) - float(old_ratio)) + (float(notional_floor) * (float(mm_ratio) - float(old_ratio))) ) + amt ) if old_ratio else 0.0 old_ratio = mm_ratio diff --git a/tests/exchange/test_binance.py b/tests/exchange/test_binance.py index 15ee56013..886c11980 100644 --- a/tests/exchange/test_binance.py +++ b/tests/exchange/test_binance.py @@ -397,9 +397,9 @@ def test__ccxt_config(default_conf, mocker, trading_mode, collateral, config): ("BNB/BUSD", 0.0, 0.025, 0), ("BNB/USDT", 100.0, 0.0065, 0), ("BTC/USDT", 170.30, 0.004, 0), - ("BNB/BUSD", 999999.9, 0.1, 0), - ("BNB/USDT", 5000000.0, 0.5, 0), - ("BTC/USDT", 300000000.1, 0.5, 0), + ("BNB/BUSD", 999999.9, 0.1, 27500.0), + ("BNB/USDT", 5000000.0, 0.15, 233034.99999999994), + ("BTC/USDT", 300000000.1, 0.5, 99891300.0), ]) def test_get_maintenance_ratio_and_amt_binance( default_conf, @@ -411,29 +411,30 @@ def test_get_maintenance_ratio_and_amt_binance( ): exchange = get_patched_exchange(mocker, default_conf, id="binance") exchange._leverage_brackets = { - 'BNB/BUSD': [[0.0, 0.025], - [100000.0, 0.05], - [500000.0, 0.1], - [1000000.0, 0.15], - [2000000.0, 0.25], - [5000000.0, 0.5]], - 'BNB/USDT': [[0.0, 0.0065], - [10000.0, 0.01], - [50000.0, 0.02], - [250000.0, 0.05], - [1000000.0, 0.1], - [2000000.0, 0.125], - [5000000.0, 0.15], - [10000000.0, 0.25]], - 'BTC/USDT': [[0.0, 0.004], - [50000.0, 0.005], - [250000.0, 0.01], - [1000000.0, 0.025], - [5000000.0, 0.05], - [20000000.0, 0.1], - [50000000.0, 0.125], - [100000000.0, 0.15], - [200000000.0, 0.25], - [300000000.0, 0.5]], + 'BNB/BUSD': [[0.0, 0.025, 0.0], + [100000.0, 0.05, 2500.0], + [500000.0, 0.1, 27500.0], + [1000000.0, 0.15, 77499.99999999999], + [2000000.0, 0.25, 277500.0], + [5000000.0, 0.5, 1527500.0]], + 'BNB/USDT': [[0.0, 0.0065, 0.0], + [10000.0, 0.01, 35.00000000000001], + [50000.0, 0.02, 535.0], + [250000.0, 0.05, 8035.000000000001], + [1000000.0, 0.1, 58035.0], + [2000000.0, 0.125, 108034.99999999999], + [5000000.0, 0.15, 233034.99999999994], + [10000000.0, 0.25, 1233035.0]], + 'BTC/USDT': [[0.0, 0.004, 0.0], + [50000.0, 0.005, 50.0], + [250000.0, 0.01, 1300.0], + [1000000.0, 0.025, 16300.000000000002], + [5000000.0, 0.05, 141300.0], + [20000000.0, 0.1, 1141300.0], + [50000000.0, 0.125, 2391300.0], + [100000000.0, 0.15, 4891300.0], + [200000000.0, 0.25, 24891300.0], + [300000000.0, 0.5, 99891300.0] + ] } assert exchange.get_maintenance_ratio_and_amt(pair, nominal_value) == (mm_ratio, amt) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 95400dd3d..6e0c02e7c 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -3438,8 +3438,18 @@ def test_set_margin_mode(mocker, default_conf, collateral): ("bittrex", TradingMode.FUTURES, Collateral.CROSS, True), ("bittrex", TradingMode.FUTURES, 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.FUTURES, 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.FUTURES, Collateral.CROSS, True), - # TODO-lev: Uncomment once implemented + # * Uncomment once implemented # ("binance", TradingMode.MARGIN, Collateral.CROSS, False), # ("binance", TradingMode.FUTURES, Collateral.CROSS, False), - ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False), # ("kraken", TradingMode.MARGIN, Collateral.CROSS, False), # ("kraken", TradingMode.FUTURES, Collateral.CROSS, False), # ("ftx", TradingMode.MARGIN, Collateral.CROSS, False), # ("ftx", TradingMode.FUTURES, Collateral.CROSS, False), # ("gateio", TradingMode.MARGIN, Collateral.CROSS, False), # ("gateio", TradingMode.FUTURES, Collateral.CROSS, False), - ("gateio", TradingMode.FUTURES, Collateral.ISOLATED, False), ]) def test_validate_trading_mode_and_collateral( default_conf, @@ -3583,36 +3591,39 @@ def test_calculate_funding_fees( def test_get_liquidation_price(mocker, default_conf): api_mock = MagicMock() - api_mock.fetch_positions = MagicMock(return_value=[{ - 'info': {}, - 'symbol': 'NEAR/USDT:USDT', - 'timestamp': 1642164737148, - 'datetime': '2022-01-14T12:52:17.148Z', - 'initialMargin': 1.51072, - 'initialMarginPercentage': 0.1, - 'maintenanceMargin': 0.38916147, - 'maintenanceMarginPercentage': 0.025, - 'entryPrice': 18.884, - 'notional': 15.1072, - 'leverage': 9.97, - 'unrealizedPnl': 0.0048, - 'contracts': 8, - 'contractSize': 0.1, - 'marginRatio': None, - 'liquidationPrice': 17.47, - 'markPrice': 18.89, - 'collateral': 1.52549075, - 'marginType': 'isolated', - 'side': 'buy', - 'percentage': 0.003177292946409658 - }]) + positions = [ + { + 'info': {}, + 'symbol': 'NEAR/USDT:USDT', + 'timestamp': 1642164737148, + 'datetime': '2022-01-14T12:52:17.148Z', + 'initialMargin': 1.51072, + 'initialMarginPercentage': 0.1, + 'maintenanceMargin': 0.38916147, + 'maintenanceMarginPercentage': 0.025, + 'entryPrice': 18.884, + 'notional': 15.1072, + 'leverage': 9.97, + 'unrealizedPnl': 0.0048, + 'contracts': 8, + 'contractSize': 0.1, + 'marginRatio': None, + 'liquidationPrice': 17.47, + 'markPrice': 18.89, + 'collateral': 1.52549075, + 'marginType': 'isolated', + 'side': 'buy', + 'percentage': 0.003177292946409658 + } + ] + api_mock.fetch_positions = MagicMock(return_value=positions) mocker.patch.multiple( 'freqtrade.exchange.Exchange', exchange_has=MagicMock(return_value=True), ) 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') assert liq_price == 17.47 diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 6f47a62aa..7d859f955 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -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", [ - (False, 'spot', 'binance', '', None), - (True, 'spot', 'binance', '', None), - (False, 'spot', 'gateio', '', None), - (True, 'spot', 'gateio', '', None), + (False, 'spot', 'binance', None, None), + (True, 'spot', 'binance', None, None), + (False, 'spot', 'gateio', None, None), + (True, 'spot', 'gateio', None, None), (True, 'futures', 'binance', 'isolated', 13.217821782178218), (False, 'futures', 'binance', 'isolated', 6.717171717171718), (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)] default_conf_usdt['trading_mode'] = trading_mode 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_exchange(mocker) freqtrade = FreqtradeBot(default_conf_usdt) @@ -4832,16 +4833,16 @@ def test_update_funding_fees( nominal_value = mark_price * size funding_fee = nominal_value * funding_rate size = 123 - "LTC/BTC" + "LTC/USDT" 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 - "ETH/BTC" + "ETH/USDT" 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 - "ETC/BTC" + "ETC/USDT" 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 - "XRP/BTC" + "XRP/USDT" 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 """ @@ -4865,19 +4866,19 @@ def test_update_funding_fees( # 16:00 entry is actually never used # But should be kept in the test to ensure we're filtering correctly. funding_rates = { - "LTC/BTC": + "LTC/USDT": DataFrame([ [date_midnight, 0.00032583, 0, 0, 0, 0], [date_eight, 0.00024472, 0, 0, 0, 0], [date_sixteen, 0.00024472, 0, 0, 0, 0], ], columns=columns), - "ETH/BTC": + "ETH/USDT": DataFrame([ [date_midnight, 0.0001, 0, 0, 0, 0], [date_eight, 0.0001, 0, 0, 0, 0], [date_sixteen, 0.0001, 0, 0, 0, 0], ], columns=columns), - "XRP/BTC": + "XRP/USDT": DataFrame([ [date_midnight, 0.00049426, 0, 0, 0, 0], [date_eight, 0.00032715, 0, 0, 0, 0], @@ -4886,19 +4887,19 @@ def test_update_funding_fees( } mark_prices = { - "LTC/BTC": + "LTC/USDT": DataFrame([ [date_midnight, 3.3, 0, 0, 0, 0], [date_eight, 3.2, 0, 0, 0, 0], [date_sixteen, 3.2, 0, 0, 0, 0], ], columns=columns), - "ETH/BTC": + "ETH/USDT": DataFrame([ [date_midnight, 2.4, 0, 0, 0, 0], [date_eight, 2.5, 0, 0, 0, 0], [date_sixteen, 2.5, 0, 0, 0, 0], ], columns=columns), - "XRP/BTC": + "XRP/USDT": DataFrame([ [date_midnight, 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) # initial funding fees, - freqtrade.execute_entry('ETH/BTC', 123, is_short=is_short) - freqtrade.execute_entry('LTC/BTC', 2.0, is_short=is_short) - freqtrade.execute_entry('XRP/BTC', 123, is_short=is_short) + freqtrade.execute_entry('ETH/USDT', 123, is_short=is_short) + freqtrade.execute_entry('LTC/USDT', 2.0, is_short=is_short) + freqtrade.execute_entry('XRP/USDT', 123, is_short=is_short) multipl = 1 if is_short else -1 trades = Trade.get_open_trades() assert len(trades) == 3