Merge pull request #1657 from freqtrade/fix/1653

send notification when stoploss_on_exchange is hit
This commit is contained in:
Misagh 2019-03-13 19:52:32 +01:00 committed by GitHub
commit ee613b564c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 7 deletions

View File

@ -512,6 +512,7 @@ class FreqtradeBot(object):
except OperationalException as exception: except OperationalException as exception:
logger.warning("Could not update trade amount: %s", exception) logger.warning("Could not update trade amount: %s", exception)
# This handles both buy and sell orders!
trade.update(order) trade.update(order)
if self.strategy.order_types.get('stoploss_on_exchange') and trade.is_open: if self.strategy.order_types.get('stoploss_on_exchange') and trade.is_open:
@ -657,6 +658,7 @@ class FreqtradeBot(object):
if order['status'] == 'closed': if order['status'] == 'closed':
trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value trade.sell_reason = SellType.STOPLOSS_ON_EXCHANGE.value
trade.update(order) trade.update(order)
self.notify_sell(trade)
result = True result = True
elif self.config.get('trailing_stop', False): elif self.config.get('trailing_stop', False):
# if trailing stoploss is enabled we check if stoploss value has changed # if trailing stoploss is enabled we check if stoploss value has changed
@ -846,10 +848,17 @@ class FreqtradeBot(object):
trade.open_order_id = order_id trade.open_order_id = order_id
trade.close_rate_requested = limit trade.close_rate_requested = limit
trade.sell_reason = sell_reason.value trade.sell_reason = sell_reason.value
Trade.session.flush()
self.notify_sell(trade)
profit_trade = trade.calc_profit(rate=limit) def notify_sell(self, trade: Trade):
"""
Sends rpc notification when a sell occured.
"""
profit_rate = trade.close_rate if trade.close_rate else trade.close_rate_requested
profit_trade = trade.calc_profit(rate=profit_rate)
current_rate = self.exchange.get_ticker(trade.pair)['bid'] current_rate = self.exchange.get_ticker(trade.pair)['bid']
profit_percent = trade.calc_profit_percent(limit) profit_percent = trade.calc_profit_percent(profit_rate)
gain = "profit" if profit_percent > 0 else "loss" gain = "profit" if profit_percent > 0 else "loss"
msg = { msg = {
@ -857,13 +866,13 @@ class FreqtradeBot(object):
'exchange': trade.exchange.capitalize(), 'exchange': trade.exchange.capitalize(),
'pair': trade.pair, 'pair': trade.pair,
'gain': gain, 'gain': gain,
'limit': limit, 'limit': trade.close_rate_requested,
'amount': trade.amount, 'amount': trade.amount,
'open_rate': trade.open_rate, 'open_rate': trade.open_rate,
'current_rate': current_rate, 'current_rate': current_rate,
'profit_amount': profit_trade, 'profit_amount': profit_trade,
'profit_percent': profit_percent, 'profit_percent': profit_percent,
'sell_reason': sell_reason.value 'sell_reason': trade.sell_reason
} }
# For regular case, when the configuration exists # For regular case, when the configuration exists
@ -877,4 +886,3 @@ class FreqtradeBot(object):
# Send the message # Send the message
self.rpc.send_msg(msg) self.rpc.send_msg(msg)
Trade.session.flush()

View File

@ -266,6 +266,7 @@ class Trade(_DECL_BASE):
logger.info('%s_SELL has been fulfilled for %s.', order_type.upper(), self) logger.info('%s_SELL has been fulfilled for %s.', order_type.upper(), self)
elif order_type == 'stop_loss_limit': elif order_type == 'stop_loss_limit':
self.stoploss_order_id = None self.stoploss_order_id = None
self.close_rate_requested = self.stop_loss
logger.info('STOP_LOSS_LIMIT is hit for %s.', self) logger.info('STOP_LOSS_LIMIT is hit for %s.', self)
self.close(order['average']) self.close(order['average'])
else: else:

View File

@ -2,7 +2,7 @@
This module contains class to manage RPC communications (Telegram, Slack, ...) This module contains class to manage RPC communications (Telegram, Slack, ...)
""" """
import logging import logging
from typing import List, Dict, Any from typing import Any, Dict, List
from freqtrade.rpc import RPC, RPCMessageType from freqtrade.rpc import RPC, RPCMessageType

View File

@ -2105,7 +2105,7 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf,
assert trade.is_open is False assert trade.is_open is False
print(trade.sell_reason) print(trade.sell_reason)
assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value
assert rpc_mock.call_count == 1 assert rpc_mock.call_count == 2
def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee,