From 250e84a42ab8b4b8868b0ad9f0801051552b91ec Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Thu, 10 May 2018 16:50:04 -0700 Subject: [PATCH] more work on stop losses --- freqtrade/analyze.py | 9 +++++---- freqtrade/persistence.py | 19 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/freqtrade/analyze.py b/freqtrade/analyze.py index e72978e9c..3ed2181cf 100644 --- a/freqtrade/analyze.py +++ b/freqtrade/analyze.py @@ -197,16 +197,17 @@ class Analyze(object): current_profit = trade.calc_profit_percent(current_rate) if trade.stop_loss is None: - # initially adjust the stop loss to it's default value - trade.adjust_stop_loss(current_rate, self.strategy.stoploss) + # initially adjust the stop loss to the base value + trade.adjust_stop_loss(trade.open_rate, self.strategy.stoploss) - # evaluate stop loss, before we continue + # evaluate if the stoploss was hit if self.strategy.stoploss is not None and trade.stop_loss >= current_rate: logger.debug('Stop loss hit.') return True # update the stop loss afterwards, after all by definition it's supposed to be hanging - trade.adjust_stop_loss(current_rate, self.strategy.stoploss) + if 'trailing_stop' in self.config and self.config['trailing_stop']: + trade.adjust_stop_loss(current_rate, self.strategy.stoploss) # Check if time matches and current rate is above threshold time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60 diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index f3c16a709..638ab1595 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -117,21 +117,20 @@ class Trade(_DECL_BASE): """ new_loss = Decimal(current_price * (1 - abs(stoploss))) - logger.debug("calculated stop loss at: {:.6f}".format(new_loss)) + if self.stop_loss is None: logger.debug("assigning new stop loss") self.stop_loss = new_loss # no stop loss assigned yet else: - if _CONF.get('trailing_stop', True): - if new_loss > self.stop_loss: # stop losses only walk up, never down! - self.stop_loss = new_loss - logger.debug("adjusted stop loss for {:.6f} and {:.6f} to {:.6f}".format( - current_price, stoploss, self.stop_loss) - ) - else: - logger.debug("keeping current stop loss of {:.6f}".format(self.stop_loss)) + if new_loss > self.stop_loss: # stop losses only walk up, never down! + self.stop_loss = new_loss + logger.debug("adjusted stop loss") else: - print("utilizing fixed stop") + logger.debug("keeping current stop loss") + + print("{} - current price {:.6f}, calculated stop loss at: {:.6f} old loss at {:.6f}".format(self.id, current_price, + new_loss, + self.stop_loss)) def update(self, order: Dict) -> None: """