From 6ded2d5b7c442a5fd69d56ee63361df4f4a3db7e Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 4 Aug 2022 16:28:36 +0200 Subject: [PATCH] Improve forceexit API test --- freqtrade/rpc/rpc.py | 2 +- tests/conftest_trades.py | 6 ++++-- tests/rpc/test_rpc_apiserver.py | 23 +++++++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 7694b2414..cda4fdc22 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -682,7 +682,7 @@ class RPC: exit_check = ExitCheckTuple(exit_type=ExitType.FORCE_EXIT) order_type = ordertype or self._freqtrade.strategy.order_types.get( "force_exit", self._freqtrade.strategy.order_types["exit"]) - sub_amount: float = None + sub_amount: Optional[float] = None if _amount and _amount < trade.amount: # Partial exit ... min_exit_stake = self._freqtrade.exchange.get_min_pair_stake_amount( diff --git a/tests/conftest_trades.py b/tests/conftest_trades.py index 1a8cf3183..9642435e5 100644 --- a/tests/conftest_trades.py +++ b/tests/conftest_trades.py @@ -214,7 +214,8 @@ def mock_trade_4(fee, is_short: bool): open_order_id=f'prod_buy_{direc(is_short)}_12345', strategy='StrategyTestV3', timeframe=5, - is_short=is_short + is_short=is_short, + stop_loss_pct=0.10 ) o = Order.parse_from_ccxt_object(mock_order_4(is_short), 'ETC/BTC', entry_side(is_short)) trade.orders.append(o) @@ -270,7 +271,8 @@ def mock_trade_5(fee, is_short: bool): enter_tag='TEST1', stoploss_order_id=f'prod_stoploss_{direc(is_short)}_3455', timeframe=5, - is_short=is_short + is_short=is_short, + stop_loss_pct=0.10, ) o = Order.parse_from_ccxt_object(mock_order_5(is_short), 'XRP/BTC', entry_side(is_short)) trade.orders.append(o) diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index b7161e680..d2d9a73d5 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1202,7 +1202,7 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets): fetch_ticker=ticker, get_fee=fee, markets=PropertyMock(return_value=markets), - _is_dry_limit_order_filled=MagicMock(return_value=False), + _is_dry_limit_order_filled=MagicMock(return_value=True), ) patch_get_signal(ftbot) @@ -1212,12 +1212,27 @@ def test_api_forceexit(botclient, mocker, ticker, fee, markets): assert rc.json() == {"error": "Error querying /api/v1/forceexit: invalid argument"} Trade.query.session.rollback() - ftbot.enter_positions() + create_mock_trades(fee) + trade = Trade.get_trades([Trade.id == 5]).first() + assert pytest.approx(trade.amount) == 123 + rc = client_post(client, f"{BASE_URI}/forceexit", + data='{"tradeid": "5", "ordertype": "market", "amount": 23}') + assert_response(rc) + assert rc.json() == {'result': 'Created sell order for trade 5.'} + Trade.query.session.rollback() + + trade = Trade.get_trades([Trade.id == 5]).first() + assert pytest.approx(trade.amount) == 100 + assert trade.is_open is True rc = client_post(client, f"{BASE_URI}/forceexit", - data='{"tradeid": "1"}') + data='{"tradeid": "5"}') assert_response(rc) - assert rc.json() == {'result': 'Created sell order for trade 1.'} + assert rc.json() == {'result': 'Created sell order for trade 5.'} + Trade.query.session.rollback() + + trade = Trade.get_trades([Trade.id == 5]).first() + assert trade.is_open is False def test_api_pair_candles(botclient, ohlcv_history):