From 5fbe76cd7ed01ca5eeee26dc93649cbe4415d305 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Thu, 14 Oct 2021 05:10:28 -0600 Subject: [PATCH] isolated conditionals in interface stoploss method --- freqtrade/strategy/interface.py | 15 +++++++-------- tests/test_freqtradebot.py | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index f4784133a..05df0c6fb 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -840,10 +840,9 @@ class IStrategy(ABC, HyperStrategyMixin): else: logger.warning("CustomStoploss function did not return valid stoploss") - if self.trailing_stop and ( - (trade.stop_loss < (low or current_rate) and not trade.is_short) or - (trade.stop_loss > (high or current_rate) and trade.is_short) - ): + sl_lower_short = (trade.stop_loss < (low or current_rate) and not trade.is_short) + sl_higher_long = (trade.stop_loss > (high or current_rate) and trade.is_short) + if self.trailing_stop and (sl_lower_short or sl_higher_long): # trailing stoploss handling sl_offset = self.trailing_stop_positive_offset @@ -867,13 +866,13 @@ class IStrategy(ABC, HyperStrategyMixin): trade.adjust_stop_loss(bound or current_rate, stop_loss_value) + sl_higher_short = (trade.stop_loss >= (low or current_rate) and not trade.is_short) + sl_lower_long = ((trade.stop_loss <= (high or current_rate) and trade.is_short)) # evaluate if the stoploss was hit if stoploss is not on exchange # in Dry-Run, this handles stoploss logic as well, as the logic will not be different to # regular stoploss handling. - if (( - (trade.stop_loss >= (low or current_rate) and not trade.is_short) or - ((trade.stop_loss <= (high or current_rate) and trade.is_short)) - ) and (not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])): + if ((sl_higher_short or sl_lower_long) and + (not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])): sell_type = SellType.STOP_LOSS diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index d5b820d2b..9d817bc91 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -3238,11 +3238,11 @@ def test_execute_trade_exit_market_order( """ amount long: 60 / 2.0 = 30 - short: 60 / 2.02 = 29.70297029 - open_value + short: 60 / 2.02 = 29.70297029 + open_value long: (30 * 2.0) + (30 * 2.0 * 0.0025) = 60.15 short: (29.702970297029704 * 2.02) - (29.702970297029704 * 2.02 * 0.0025) = 59.85 - close_value + close_value long: (30 * 2.2) - (30 * 2.2 * 0.0025) = 65.835 short: (29.702970297029704 * 2.3) + (29.702970297029704 * 2.3 * 0.0025) = 68.48762376237624 profit