From 36dae7cc6c513e590ff8cbeb36be5b82b8393664 Mon Sep 17 00:00:00 2001 From: Misagh Date: Sun, 2 Jun 2019 13:27:31 +0200 Subject: [PATCH] trailing stoploss reason fixed --- freqtrade/strategy/interface.py | 5 +++-- freqtrade/tests/test_freqtradebot.py | 32 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index caf56f13e..db266d95f 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -329,8 +329,9 @@ class IStrategy(ABC): (not self.order_types.get('stoploss_on_exchange'))): selltype = SellType.STOP_LOSS - # If Trailing stop (and max-rate did move above open rate) - if trailing_stop and trade.open_rate != trade.max_rate: + + # If initial stoploss is not the same as current one then it is trailing. + if trade.initial_stop_loss != trade.stop_loss: selltype = SellType.TRAILING_STOP_LOSS logger.debug( f"HIT STOP: current price at {current_rate:.6f}, " diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 67b05ac3e..790e90641 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -2493,9 +2493,9 @@ def test_trailing_stop_loss(default_conf, limit_buy_order, fee, markets, caplog, mocker.patch.multiple( 'freqtrade.exchange.Exchange', get_ticker=MagicMock(return_value={ - 'bid': 0.00000102, - 'ask': 0.00000103, - 'last': 0.00000102 + 'bid': 0.00001099, + 'ask': 0.00001099, + 'last': 0.00001099 }), buy=MagicMock(return_value={'id': limit_buy_order['id']}), get_fee=fee, @@ -2507,15 +2507,33 @@ def test_trailing_stop_loss(default_conf, limit_buy_order, fee, markets, caplog, freqtrade.strategy.min_roi_reached = MagicMock(return_value=False) freqtrade.create_trade() - trade = Trade.query.first() - trade.update(limit_buy_order) - trade.max_rate = trade.open_rate * 1.003 + assert freqtrade.handle_trade(trade) is False + + # Raise ticker above buy price + mocker.patch('freqtrade.exchange.Exchange.get_ticker', + MagicMock(return_value={ + 'bid': 0.00001099 * 1.5, + 'ask': 0.00001099 * 1.5, + 'last': 0.00001099 * 1.5 + })) + + # Stoploss should be adjusted + assert freqtrade.handle_trade(trade) is False + + # Price fell + mocker.patch('freqtrade.exchange.Exchange.get_ticker', + MagicMock(return_value={ + 'bid': 0.00001099 * 1.1, + 'ask': 0.00001099 * 1.1, + 'last': 0.00001099 * 1.1 + })) + caplog.set_level(logging.DEBUG) # Sell as trailing-stop is reached assert freqtrade.handle_trade(trade) is True assert log_has( - f'HIT STOP: current price at 0.000001, stop loss is {trade.stop_loss:.6f}, ' + f'HIT STOP: current price at 0.000012, stop loss is 0.000015, ' f'initial stop loss was at 0.000010, trade opened at 0.000011', caplog.record_tuples) assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value