From ae11be39706b37eb6733b12cee814cdf351b53d9 Mon Sep 17 00:00:00 2001 From: axel Date: Thu, 12 Aug 2021 14:47:01 -0400 Subject: [PATCH] manage None or string value returned by custom_entry_price and add unit test for those cases --- freqtrade/freqtradebot.py | 12 ++++++++---- tests/test_freqtradebot.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 99f5d2894..99fe1c5a3 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -479,13 +479,17 @@ class FreqtradeBot(LoggingMixin): buy_limit_requested = price else: # Calculate price - buy_limit_requested = self.exchange.get_rate(pair, refresh=True, side="buy") + proposed_buy_rate = self.exchange.get_rate(pair, refresh=True, side="buy") custom_entry_price = strategy_safe_wrapper(self.strategy.custom_entry_price, - default_retval=buy_limit_requested)( + default_retval=proposed_buy_rate)( pair=pair, current_time=datetime.now(timezone.utc), - proposed_rate=buy_limit_requested) + proposed_rate=proposed_buy_rate) - buy_limit_requested = custom_entry_price + if custom_entry_price and (isinstance(custom_entry_price, int) + or isinstance(custom_entry_price, float)): + buy_limit_requested = custom_entry_price + else: + buy_limit_requested = proposed_buy_rate if not buy_limit_requested: raise PricingError('Could not determine buy price.') diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index c73e51dec..69a4fa530 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -913,6 +913,30 @@ def test_execute_buy(mocker, default_conf, fee, limit_buy_order, limit_buy_order assert trade assert trade.open_rate_requested == 0.77 + # In case of custom entry price set to None + limit_buy_order['status'] = 'open' + limit_buy_order['id'] = '5567' + freqtrade.strategy.custom_entry_price = lambda **kwargs: None + + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_rate=MagicMock(return_value=10), + ) + + assert freqtrade.execute_buy(pair, stake_amount) + trade = Trade.query.all()[7] + assert trade + assert trade.open_rate_requested == 10 + + # In case of custom entry price not float type + limit_buy_order['status'] = 'open' + limit_buy_order['id'] = '5568' + freqtrade.strategy.custom_entry_price = lambda **kwargs: "string price" + assert freqtrade.execute_buy(pair, stake_amount) + trade = Trade.query.all()[8] + assert trade + assert trade.open_rate_requested == 10 + def test_execute_buy_confirm_error(mocker, default_conf, fee, limit_buy_order) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf)