From 3b54e1e746ac75bfdcbb94b04b69d44d50d510a3 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Tue, 9 Jan 2024 14:17:35 +0900 Subject: [PATCH 1/7] add Try block to catch the failure on using FullTradesFilter on non-dry/live run --- freqtrade/plugins/pairlist/FullTradesFilter.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/freqtrade/plugins/pairlist/FullTradesFilter.py b/freqtrade/plugins/pairlist/FullTradesFilter.py index 69779d896..9d435c3a9 100644 --- a/freqtrade/plugins/pairlist/FullTradesFilter.py +++ b/freqtrade/plugins/pairlist/FullTradesFilter.py @@ -48,8 +48,13 @@ class FullTradesFilter(IPairList): :return: new allowlist """ # Get the number of open trades and max open trades config - num_open = Trade.get_open_trade_count() - max_trades = self._config['max_open_trades'] + try: + num_open = Trade.get_open_trade_count() + max_trades = self._config['max_open_trades'] + except AttributeError: + # Performancefilter does not work in backtesting. + self.log_once("FullTradesFilter is not available in this mode.", logger.warning) + return pairlist if (num_open >= max_trades) and (max_trades > 0): return [] From bae4973da518f14cb73feac19329c90f44d844c4 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Mon, 11 Mar 2024 22:01:09 +0900 Subject: [PATCH 2/7] simplify full exit using adjust trade pos --- docs/strategy-callbacks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 048cad04a870ed00f7474776795c7cda37e89ad5 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Mon, 11 Mar 2024 22:07:58 +0900 Subject: [PATCH 3/7] remove unrelated old changes --- freqtrade/plugins/pairlist/FullTradesFilter.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/freqtrade/plugins/pairlist/FullTradesFilter.py b/freqtrade/plugins/pairlist/FullTradesFilter.py index 9d435c3a9..69779d896 100644 --- a/freqtrade/plugins/pairlist/FullTradesFilter.py +++ b/freqtrade/plugins/pairlist/FullTradesFilter.py @@ -48,13 +48,8 @@ class FullTradesFilter(IPairList): :return: new allowlist """ # Get the number of open trades and max open trades config - try: - num_open = Trade.get_open_trade_count() - max_trades = self._config['max_open_trades'] - except AttributeError: - # Performancefilter does not work in backtesting. - self.log_once("FullTradesFilter is not available in this mode.", logger.warning) - return pairlist + num_open = Trade.get_open_trade_count() + max_trades = self._config['max_open_trades'] if (num_open >= max_trades) and (max_trades > 0): return [] From 8000f94295c090fb6b5a3cfd6f05ac0e93c17447 Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Tue, 12 Mar 2024 16:28:40 +0900 Subject: [PATCH 4/7] changing the partial exit logic to be simpler and easier to understand --- freqtrade/freqtradebot.py | 2 +- tests/freqtradebot/test_integration.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 8ad151108..4cb81bded 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -667,7 +667,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_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 From c5201a6476d5c5cd9bcb9337ffffce2a9ea1adcc Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Tue, 12 Mar 2024 16:31:53 +0900 Subject: [PATCH 5/7] fix precommit --- freqtrade/freqtradebot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 4cb81bded..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 From 14c27f2cbee809a6158d88511fdc6dada027f24a Mon Sep 17 00:00:00 2001 From: Stefano Ariestasia Date: Tue, 12 Mar 2024 16:55:33 +0900 Subject: [PATCH 6/7] fix test --- tests/freqtradebot/test_freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/freqtradebot/test_freqtradebot.py b/tests/freqtradebot/test_freqtradebot.py index 1891c2332..fc1fcae9e 100644 --- a/tests/freqtradebot/test_freqtradebot.py +++ b/tests/freqtradebot/test_freqtradebot.py @@ -5460,7 +5460,7 @@ 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.001, '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 From 85c145c77717366f2bffaefaa40a6dff9d16a78b Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 13 Mar 2024 07:07:42 +0100 Subject: [PATCH 7/7] Don't close the test trade ... --- tests/freqtradebot/test_freqtradebot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/freqtradebot/test_freqtradebot.py b/tests/freqtradebot/test_freqtradebot.py index fc1fcae9e..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=(-0.001, '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