From f8da1bf5a6c2bdf56bd10199649779a32693057f Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Sun, 13 May 2018 15:17:50 -0700 Subject: [PATCH 1/2] added support for the adjusteable stop loss, if we made profit. Implementation based on the cocnept: use positive stop loss, if made profit is > 0 --- config.json.example | 4 +++- freqtrade/analyze.py | 15 +++++++++++++-- freqtrade/persistence.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/config.json.example b/config.json.example index 5b98a505a..63530d936 100644 --- a/config.json.example +++ b/config.json.example @@ -4,7 +4,9 @@ "stake_amount": 0.05, "fiat_display_currency": "USD", "dry_run": false, - "trailing_stop": false, + "trailing_stop": { + "positive" : 0.01 + }, "unfilledtimeout": 600, "bid_strategy": { "ask_last_balance": 0.0 diff --git a/freqtrade/analyze.py b/freqtrade/analyze.py index c1bae8303..4a073c3bd 100644 --- a/freqtrade/analyze.py +++ b/freqtrade/analyze.py @@ -203,7 +203,6 @@ class Analyze(object): # evaluate if the stoploss was hit if self.strategy.stoploss is not None and trade.stop_loss >= current_rate: - # just for debugging if 'trailing_stop' in self.config and self.config['trailing_stop']: logger.warning( "HIT STOP: current price at {:.6f}, stop loss is {:.6f}, " @@ -216,7 +215,19 @@ class Analyze(object): # update the stop loss afterwards, after all by definition it's supposed to be hanging if 'trailing_stop' in self.config and self.config['trailing_stop']: - trade.adjust_stop_loss(current_rate, self.strategy.stoploss) + + # check if we have a special stop loss for positive condition + # and if profit is positive + stop_loss_value = self.strategy.stoploss + if isinstance(self.config['trailing_stop'], dict) and \ + 'positive' in self.config['trailing_stop'] and \ + current_profit > 0: + + print("using positive stop loss mode: {} since we have profit {}".format( + self.config['trailing_stop']['positive'], current_profit)) + stop_loss_value = self.config['trailing_stop']['positive'] + + trade.adjust_stop_loss(current_rate, stop_loss_value) # 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 52aa17038..a34b1b6c9 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -141,7 +141,7 @@ class Trade(_DECL_BASE): else: logger.debug("keeping current stop loss") - print( + logger.debug( "{} - current price {:.8f}, bought at {:.8f} and calculated " "stop loss is at: {:.8f} initial stop at {:.8f}. trailing stop loss saved us: {:.8f} " "and max observed rate was {:.8f}".format( From 40630875ab4d5e7e9966d37b9b43a2e0e651a2cd Mon Sep 17 00:00:00 2001 From: Gert Wohlgemuth Date: Sun, 13 May 2018 15:19:27 -0700 Subject: [PATCH 2/2] fixed flake error, they are really to stringent! --- freqtrade/analyze.py | 3 ++- freqtrade/persistence.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/freqtrade/analyze.py b/freqtrade/analyze.py index 4a073c3bd..7de3e95b2 100644 --- a/freqtrade/analyze.py +++ b/freqtrade/analyze.py @@ -208,7 +208,8 @@ class Analyze(object): "HIT STOP: current price at {:.6f}, stop loss is {:.6f}, " "initial stop loss was at {:.6f}, trade opened at {:.6f}".format( current_rate, trade.stop_loss, trade.initial_stop_loss, trade.open_rate)) - logger.debug("trailing stop saved us: {:.6f}".format(trade.stop_loss - trade.initial_stop_loss)) + logger.debug("trailing stop saved us: {:.6f}" + .format(trade.stop_loss - trade.initial_stop_loss)) logger.debug('Stop loss hit.') return True diff --git a/freqtrade/persistence.py b/freqtrade/persistence.py index a34b1b6c9..67cae492c 100644 --- a/freqtrade/persistence.py +++ b/freqtrade/persistence.py @@ -95,9 +95,12 @@ class Trade(_DECL_BASE): open_date = Column(DateTime, nullable=False, default=datetime.utcnow) close_date = Column(DateTime) open_order_id = Column(String) - stop_loss = Column(Float, nullable=False, default=0.0) # absolute value of the stop loss - initial_stop_loss = Column(Float, nullable=False, default=0.0) # absolute value of the initial stop loss - max_rate = Column(Float, nullable=False, default=0.0) # absolute value of the highest reached price + # absolute value of the stop loss + stop_loss = Column(Float, nullable=False, default=0.0) + # absolute value of the initial stop loss + initial_stop_loss = Column(Float, nullable=False, default=0.0) + # absolute value of the highest reached price + max_rate = Column(Float, nullable=False, default=0.0) def __repr__(self): return 'Trade(id={}, pair={}, amount={:.8f}, open_rate={:.8f}, open_since={})'.format(