diff --git a/docs/strategy-callbacks.md b/docs/strategy-callbacks.md index 2f04e906e..c49324be7 100644 --- a/docs/strategy-callbacks.md +++ b/docs/strategy-callbacks.md @@ -783,7 +783,7 @@ Additional entries are ignored once you have reached the maximum amount of extra ### Decrease position The strategy is expected to return a negative stake_amount (in stake currency) for a partial exit. -Returning the full owned stake at that point (based on the current price) (`-(trade.amount / trade.leverage) * current_exit_rate`) results in a full exit. +Returning the full owned stake at that point (`-trade.stake_amount`) results in a full exit. Returning a value more than the above (so remaining stake_amount would become negative) will result in the bot ignoring the signal. !!! Note "About stake size" diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 8ad151108..898f36eb1 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -37,7 +37,6 @@ from freqtrade.rpc.rpc_types import (ProfitLossStr, RPCCancelMsg, RPCEntryMsg, R RPCExitMsg, RPCProtectionMsg) from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper -from freqtrade.util import FtPrecise from freqtrade.util.migrations import migrate_binance_futures_names from freqtrade.wallets import Wallets @@ -667,7 +666,7 @@ class FreqtradeBot(LoggingMixin): # We should decrease our position amount = self.exchange.amount_to_contract_precision( trade.pair, - abs(float(FtPrecise(stake_amount * trade.leverage) / FtPrecise(current_exit_rate)))) + abs(float(stake_amount * trade.amount / trade.stake_amount))) if amount == 0.0: logger.info("Amount to exit is 0.0 due to exchange limits - not exiting.") diff --git a/tests/freqtradebot/test_freqtradebot.py b/tests/freqtradebot/test_freqtradebot.py index 1891c2332..27a18c0df 100644 --- a/tests/freqtradebot/test_freqtradebot.py +++ b/tests/freqtradebot/test_freqtradebot.py @@ -5460,9 +5460,10 @@ def test_check_and_call_adjust_trade_position(mocker, default_conf_usdt, fee, ca assert freqtrade.strategy.adjust_trade_position.call_count == 1 caplog.clear() - freqtrade.strategy.adjust_trade_position = MagicMock(return_value=(-10, 'partial_exit_c')) + freqtrade.strategy.adjust_trade_position = MagicMock(return_value=(-0.0005, 'partial_exit_c')) freqtrade.process_open_trade_positions() assert log_has_re(r"LIMIT_SELL has been fulfilled.*", caplog) assert freqtrade.strategy.adjust_trade_position.call_count == 1 trade = Trade.get_trades(trade_filter=[Trade.id == 5]).first() assert trade.orders[-1].ft_order_tag == 'partial_exit_c' + assert trade.is_open diff --git a/tests/freqtradebot/test_integration.py b/tests/freqtradebot/test_integration.py index 522693131..3384ae49f 100644 --- a/tests/freqtradebot/test_integration.py +++ b/tests/freqtradebot/test_integration.py @@ -636,12 +636,12 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera assert len(trade.orders) == 2 assert trade.orders[-1].ft_order_side == 'sell' assert trade.orders[-1].ft_order_tag == 'PES' - assert pytest.approx(trade.stake_amount) == 40.198 - assert pytest.approx(trade.amount) == 20.099 * leverage + assert pytest.approx(trade.stake_amount) == 40 + assert pytest.approx(trade.amount) == 20 * leverage assert trade.open_rate == 2.0 assert trade.is_open assert trade.realized_profit > 0.098 * leverage - expected_profit = starting_amount - 40.1980 + trade.realized_profit + expected_profit = starting_amount - 40 + trade.realized_profit assert pytest.approx(freqtrade.wallets.get_free('USDT')) == expected_profit if spot: @@ -667,14 +667,14 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera # Amount exactly comes out as exactly 0 freqtrade.strategy.adjust_trade_position = MagicMock( - return_value=-(trade.amount / trade.leverage * 2.02)) + return_value=-trade.stake_amount) freqtrade.process() trade = Trade.get_trades().first() assert len(trade.orders) == 3 assert trade.orders[-1].ft_order_side == 'sell' - assert pytest.approx(trade.stake_amount) == 40.198 + assert pytest.approx(trade.stake_amount) == 40 assert trade.is_open is False # use amount that would trunc to 0.0 once selling @@ -684,7 +684,7 @@ def test_dca_exiting(default_conf_usdt, ticker_usdt, fee, mocker, caplog, levera trade = Trade.get_trades().first() assert len(trade.orders) == 3 assert trade.orders[-1].ft_order_side == 'sell' - assert pytest.approx(trade.stake_amount) == 40.198 + assert pytest.approx(trade.stake_amount) == 40 assert trade.is_open is False assert log_has_re('Amount to exit is 0.0 due to exchange limits - not exiting.', caplog) expected_profit = starting_amount - 60 + trade.realized_profit