Refactor notify_sell to rpc_manager

* Call sell_notify also when stoploss_on_exchange is hit

fix #1653
This commit is contained in:
Matthias 2019-03-12 21:49:08 +01:00
parent e2bcaa4d75
commit 11cc33a982
2 changed files with 37 additions and 32 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.rpc.notify_sell(trade, self.config, trade.close_rate)
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,35 +848,5 @@ 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
profit_trade = trade.calc_profit(rate=limit)
current_rate = self.exchange.get_ticker(trade.pair)['bid']
profit_percent = trade.calc_profit_percent(limit)
gain = "profit" if profit_percent > 0 else "loss"
msg = {
'type': RPCMessageType.SELL_NOTIFICATION,
'exchange': trade.exchange.capitalize(),
'pair': trade.pair,
'gain': gain,
'limit': limit,
'amount': trade.amount,
'open_rate': trade.open_rate,
'current_rate': current_rate,
'profit_amount': profit_trade,
'profit_percent': profit_percent,
'sell_reason': sell_reason.value
}
# For regular case, when the configuration exists
if 'stake_currency' in self.config and 'fiat_display_currency' in self.config:
stake_currency = self.config['stake_currency']
fiat_currency = self.config['fiat_display_currency']
msg.update({
'stake_currency': stake_currency,
'fiat_currency': fiat_currency,
})
# Send the message
self.rpc.send_msg(msg)
Trade.session.flush() Trade.session.flush()
self.rpc.notify_sell(trade, self.config, self.exchange.get_ticker(trade.pair)['bid'])

View File

@ -2,8 +2,9 @@
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.persistence import Trade
from freqtrade.rpc import RPC, RPCMessageType from freqtrade.rpc import RPC, RPCMessageType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -80,3 +81,35 @@ class RPCManager(object):
'status': f'Searching for {stake_currency} pairs to buy and sell ' 'status': f'Searching for {stake_currency} pairs to buy and sell '
f'based on {pairlist.short_desc()}' f'based on {pairlist.short_desc()}'
}) })
def notify_sell(self, trade: Trade, config, current_rate: float):
profit_trade = trade.calc_profit(rate=trade.close_rate_requested)
profit_percent = trade.calc_profit_percent(trade.close_rate_requested)
gain = "profit" if profit_percent > 0 else "loss"
msg = {
'type': RPCMessageType.SELL_NOTIFICATION,
'exchange': trade.exchange.capitalize(),
'pair': trade.pair,
'gain': gain,
'limit': trade.close_rate_requested,
'amount': trade.amount,
'open_rate': trade.open_rate,
'current_rate': current_rate,
'profit_amount': profit_trade,
'profit_percent': profit_percent,
'sell_reason': trade.sell_reason
}
# For regular case, when the configuration exists
if 'stake_currency' in config and 'fiat_display_currency' in config:
stake_currency = config['stake_currency']
fiat_currency = config['fiat_display_currency']
msg.update({
'stake_currency': stake_currency,
'fiat_currency': fiat_currency,
})
# Send the message
self.send_msg(msg)