From 62c42a73e2cb9d0cdc2375e836cc884da15a8637 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 23 Feb 2022 07:40:15 +0100 Subject: [PATCH] Add initial rpc test --- freqtrade/wallets.py | 1 - tests/rpc/test_rpc.py | 108 ++++++++++++++++++++++++++------------ tests/test_persistence.py | 1 + tests/test_wallets.py | 101 +++++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 34 deletions(-) diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index c1f291731..8d8216cf6 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -141,7 +141,6 @@ class Wallets: if currency not in balances: del self._wallets[currency] - # TODO-lev: Implement dry-run/backtest counterpart positions = self._exchange.get_positions() self._positions = {} for position in positions: diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 6a02d6489..09428b8b4 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -603,6 +603,30 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): 'used': 5.0, } } + mock_pos = [ + { + "symbol": "ETH/USDT:USDT", + "timestamp": None, + "datetime": None, + "initialMargin": 0.0, + "initialMarginPercentage": None, + "maintenanceMargin": 0.0, + "maintenanceMarginPercentage": 0.005, + "entryPrice": 0.0, + "notional": 100.0, + "leverage": 5.0, + "unrealizedPnl": 0.0, + "contracts": 100.0, + "contractSize": 1, + "marginRatio": None, + "liquidationPrice": 0.0, + "markPrice": 2896.41, + "collateral": 20, + "marginType": "isolated", + "side": 'short', + "percentage": None + } + ] mocker.patch.multiple( 'freqtrade.rpc.fiat_convert.CoinGeckoAPI', @@ -612,12 +636,15 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch.multiple( 'freqtrade.exchange.Exchange', + validate_trading_mode_and_margin_mode=MagicMock(), get_balances=MagicMock(return_value=mock_balance), + get_positions=MagicMock(return_value=mock_pos), get_tickers=tickers, get_valid_pair_combination=MagicMock( side_effect=lambda a, b: f"{b}/{a}" if a == "USDT" else f"{a}/{b}") ) default_conf['dry_run'] = False + default_conf['trading_mode'] = 'futures' freqtradebot = get_patched_freqtradebot(mocker, default_conf) patch_get_signal(freqtradebot) rpc = RPC(freqtradebot) @@ -630,40 +657,55 @@ def test_rpc_balance_handle(default_conf, mocker, tickers): assert tickers.call_args_list[0][1]['cached'] is True assert 'USD' == result['symbol'] assert result['currencies'] == [ - {'currency': 'BTC', - 'free': 10.0, - 'balance': 12.0, - 'used': 2.0, - 'est_stake': 12.0, - 'stake': 'BTC', - 'is_position': False, - 'leverage': 1.0, - 'position': 0.0, - 'side': 'long', - }, - {'free': 1.0, - 'balance': 5.0, - 'currency': 'ETH', - 'est_stake': 0.30794, - 'used': 4.0, - 'stake': 'BTC', - 'is_position': False, - 'leverage': 1.0, - 'position': 0.0, - 'side': 'long', + { + 'currency': 'BTC', + 'free': 10.0, + 'balance': 12.0, + 'used': 2.0, + 'est_stake': 12.0, + 'stake': 'BTC', + 'is_position': False, + 'leverage': 1.0, + 'position': 0.0, + 'side': 'long', + }, + { + 'free': 1.0, + 'balance': 5.0, + 'currency': 'ETH', + 'est_stake': 0.30794, + 'used': 4.0, + 'stake': 'BTC', + 'is_position': False, + 'leverage': 1.0, + 'position': 0.0, + 'side': 'long', - }, - {'free': 5.0, - 'balance': 10.0, - 'currency': 'USDT', - 'est_stake': 0.0011563153318162476, - 'used': 5.0, - 'stake': 'BTC', - 'is_position': False, - 'leverage': 1.0, - 'position': 0.0, - 'side': 'long', - } + }, + { + 'free': 5.0, + 'balance': 10.0, + 'currency': 'USDT', + 'est_stake': 0.0011563153318162476, + 'used': 5.0, + 'stake': 'BTC', + 'is_position': False, + 'leverage': 1.0, + 'position': 0.0, + 'side': 'long', + }, + { + 'free': 0.0, + 'balance': 0.0, + 'currency': 'ETH/USDT:USDT', + 'est_stake': 20, + 'used': 0, + 'stake': 'BTC', + 'is_position': True, + 'leverage': 5.0, + 'position': 1000.0, + 'side': 'short', + } ] assert result['total'] == 12.309096315331816 diff --git a/tests/test_persistence.py b/tests/test_persistence.py index 484c6e8a8..f7273950a 100644 --- a/tests/test_persistence.py +++ b/tests/test_persistence.py @@ -95,6 +95,7 @@ def test_enter_exit_side(fee, is_short): ) assert trade.enter_side == enter_side assert trade.exit_side == exit_side + assert trade.trade_direction == 'short' if is_short else 'long' @pytest.mark.usefixtures("init_persistence") diff --git a/tests/test_wallets.py b/tests/test_wallets.py index 4dd1c925f..359d63bca 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -234,3 +234,104 @@ def test_get_starting_balance(mocker, default_conf, available_capital, closed_pr freqtrade = get_patched_freqtradebot(mocker, default_conf) assert freqtrade.wallets.get_starting_balance() == expected + + +def test_sync_wallet_futures_live(mocker, default_conf): + default_conf['dry_run'] = False + default_conf['trading_mode'] = 'futures' + default_conf['margin_mode'] = 'isolated' + mock_result = [ + { + "symbol": "ETH/USDT:USDT", + "timestamp": None, + "datetime": None, + "initialMargin": 0.0, + "initialMarginPercentage": None, + "maintenanceMargin": 0.0, + "maintenanceMarginPercentage": 0.005, + "entryPrice": 0.0, + "notional": 100.0, + "leverage": 5.0, + "unrealizedPnl": 0.0, + "contracts": 100.0, + "contractSize": 1, + "marginRatio": None, + "liquidationPrice": 0.0, + "markPrice": 2896.41, + "collateral": 20, + "marginType": "isolated", + "side": 'short', + "percentage": None + }, + { + "symbol": "ADA/USDT:USDT", + "timestamp": None, + "datetime": None, + "initialMargin": 0.0, + "initialMarginPercentage": None, + "maintenanceMargin": 0.0, + "maintenanceMarginPercentage": 0.005, + "entryPrice": 0.0, + "notional": 100.0, + "leverage": 5.0, + "unrealizedPnl": 0.0, + "contracts": 100.0, + "contractSize": 1, + "marginRatio": None, + "liquidationPrice": 0.0, + "markPrice": 0.91, + "collateral": 20, + "marginType": "isolated", + "side": 'short', + "percentage": None + }, + { + # Closed position + "symbol": "SOL/BUSD:BUSD", + "timestamp": None, + "datetime": None, + "initialMargin": 0.0, + "initialMarginPercentage": None, + "maintenanceMargin": 0.0, + "maintenanceMarginPercentage": 0.005, + "entryPrice": 0.0, + "notional": 0.0, + "leverage": 5.0, + "unrealizedPnl": 0.0, + "contracts": 0.0, + "contractSize": 1, + "marginRatio": None, + "liquidationPrice": 0.0, + "markPrice": 15.41, + "collateral": 0.0, + "marginType": "isolated", + "side": 'short', + "percentage": None + } + ] + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_balances=MagicMock(return_value={ + "USDT": { + "free": 900, + "used": 100, + "total": 1000 + }, + }), + get_positions=MagicMock(return_value=mock_result) + ) + + freqtrade = get_patched_freqtradebot(mocker, default_conf) + + assert len(freqtrade.wallets._wallets) == 1 + assert len(freqtrade.wallets._positions) == 2 + + assert 'USDT' in freqtrade.wallets._wallets + assert 'ETH/USDT:USDT' in freqtrade.wallets._positions + assert freqtrade.wallets._last_wallet_refresh > 0 + + # Remove ETH/USDT:USDT position + del mock_result[0] + freqtrade.wallets.update() + assert len(freqtrade.wallets._positions) == 1 + assert 'ETH/USDT:USDT' not in freqtrade.wallets._positions