use more granular msg dict for buy/sell notifications
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from .rpc import RPC, RPCMessageType, RPCException # noqa
|
||||
from .rpc_manager import RPCManager # noqa
|
||||
|
@@ -5,6 +5,7 @@ import logging
|
||||
from abc import abstractmethod
|
||||
from datetime import timedelta, datetime, date
|
||||
from decimal import Decimal
|
||||
from enum import Enum
|
||||
from typing import Dict, Any, List
|
||||
|
||||
import arrow
|
||||
@@ -19,6 +20,15 @@ from freqtrade.state import State
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RPCMessageType(Enum):
|
||||
STATUS_NOTIFICATION = 'status'
|
||||
BUY_NOTIFICATION = 'buy'
|
||||
SELL_NOTIFICATION = 'sell'
|
||||
|
||||
def __repr__(self):
|
||||
return self.value
|
||||
|
||||
|
||||
class RPCException(Exception):
|
||||
"""
|
||||
Should be raised with a rpc-formatted message in an _rpc_* method
|
||||
@@ -33,11 +43,6 @@ class RPCException(Exception):
|
||||
def __str__(self):
|
||||
return self.message
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
'msg': self.message
|
||||
}
|
||||
|
||||
|
||||
class RPC(object):
|
||||
"""
|
||||
|
@@ -2,9 +2,9 @@
|
||||
This module contains class to manage RPC communications (Telegram, Slack, ...)
|
||||
"""
|
||||
import logging
|
||||
from typing import List, Dict
|
||||
from typing import List, Dict, Any
|
||||
|
||||
from freqtrade.rpc.rpc import RPC
|
||||
from freqtrade.rpc import RPC
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -32,7 +32,7 @@ class RPCManager(object):
|
||||
mod.cleanup()
|
||||
del mod
|
||||
|
||||
def send_msg(self, msg: Dict[str, str]) -> None:
|
||||
def send_msg(self, msg: Dict[str, Any]) -> None:
|
||||
"""
|
||||
Send given message to all registered rpc modules.
|
||||
A message consists of one or more key value pairs of strings.
|
||||
|
@@ -12,7 +12,7 @@ from telegram.error import NetworkError, TelegramError
|
||||
from telegram.ext import CommandHandler, Updater
|
||||
|
||||
from freqtrade.__init__ import __version__
|
||||
from freqtrade.rpc.rpc import RPC, RPCException
|
||||
from freqtrade.rpc import RPC, RPCException, RPCMessageType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -110,9 +110,42 @@ class Telegram(RPC):
|
||||
"""
|
||||
self._updater.stop()
|
||||
|
||||
def send_msg(self, msg: Dict[str, str]) -> None:
|
||||
def send_msg(self, msg: Dict[str, Any]) -> None:
|
||||
""" Send a message to telegram channel """
|
||||
self._send_msg('*Status:* `{status}`'.format(**msg))
|
||||
|
||||
if msg['type'] == RPCMessageType.BUY_NOTIFICATION:
|
||||
message = "*{exchange}:* Buying [{pair}]({market_url})\n" \
|
||||
"with limit `{limit:.8f}\n" \
|
||||
"({stake_amount:.6f} {stake_currency}," \
|
||||
"{stake_amount_fiat:.3f} {fiat_currency})`" \
|
||||
.format(**msg)
|
||||
|
||||
elif msg['type'] == RPCMessageType.SELL_NOTIFICATION:
|
||||
msg['amount'] = round(msg['amount'], 8)
|
||||
msg['profit_percent'] = round(msg['profit_percent'] * 100, 2)
|
||||
|
||||
message = "*{exchange}:* Selling [{pair}]({market_url})\n" \
|
||||
"*Limit:* `{limit:.8f}`\n" \
|
||||
"*Amount:* `{amount:.8f}`\n" \
|
||||
"*Open Rate:* `{open_rate:.8f}`\n" \
|
||||
"*Current Rate:* `{current_rate:.8f}`\n" \
|
||||
"*Profit:* `{profit_percent:.2f}%`".format(**msg)
|
||||
|
||||
# Check if all sell properties are available.
|
||||
# This might not be the case if the message origin is triggered by /forcesell
|
||||
if all(prop in msg for prop in ['gain', 'profit_fiat',
|
||||
'fiat_currency', 'stake_currency']):
|
||||
message += '` ({gain}: {profit_amount:.8f} {stake_currency}`' \
|
||||
'` / {profit_fiat:.3f} {fiat_currency})`'.format(**msg)
|
||||
|
||||
self._send_msg(message)
|
||||
elif msg['type'] == RPCMessageType.STATUS_NOTIFICATION:
|
||||
message = '*Status:* `{status}`'.format(**msg)
|
||||
|
||||
else:
|
||||
raise NotImplementedError('Unknown message type: {}'.format(msg['type']))
|
||||
|
||||
self._send_msg(message)
|
||||
|
||||
@authorized_only
|
||||
def _status(self, bot: Bot, update: Update) -> None:
|
||||
|
Reference in New Issue
Block a user