return dict from _rpc_status and handle rendering in module impl

This commit is contained in:
gcarq 2018-06-22 03:54:10 +02:00
parent 29670b9814
commit f1a370b3b9
4 changed files with 62 additions and 49 deletions

View File

@ -64,7 +64,7 @@ class RPC(object):
def send_msg(self, msg: str) -> None: def send_msg(self, msg: str) -> None:
""" Sends a message to all registered rpc modules """ """ Sends a message to all registered rpc modules """
def _rpc_trade_status(self) -> List[str]: def _rpc_trade_status(self) -> List[Dict]:
""" """
Below follows the RPC backend it is prefixed with rpc_ to raise awareness that it is Below follows the RPC backend it is prefixed with rpc_ to raise awareness that it is
a remotely exposed function a remotely exposed function
@ -76,7 +76,7 @@ class RPC(object):
elif not trades: elif not trades:
raise RPCException('no active trade') raise RPCException('no active trade')
else: else:
result = [] results = []
for trade in trades: for trade in trades:
order = None order = None
if trade.open_order_id: if trade.open_order_id:
@ -86,32 +86,22 @@ class RPC(object):
current_profit = trade.calc_profit_percent(current_rate) current_profit = trade.calc_profit_percent(current_rate)
fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%' fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%'
if trade.close_profit else None) if trade.close_profit else None)
market_url = self._freqtrade.exchange.get_pair_detail_url(trade.pair) results.append(dict(
trade_date = arrow.get(trade.open_date).humanize() trade_id=trade.id,
open_rate = trade.open_rate pair=trade.pair,
close_rate = trade.close_rate market_url=self._freqtrade.exchange.get_pair_detail_url(trade.pair),
amount = round(trade.amount, 8) date=arrow.get(trade.open_date).humanize(),
current_profit = round(current_profit * 100, 2) open_rate=trade.open_rate,
open_order = '' close_rate=trade.close_rate,
if order: current_rate=current_rate,
order_type = order['type'] amount=round(trade.amount, 8),
order_side = order['side'] close_profit=fmt_close_profit,
order_rem = order['remaining'] current_profit=round(current_profit * 100, 2),
open_order = f'({order_type} {order_side} rem={order_rem:.8f})' open_order='({} {} rem={:.8f})'.format(
order['type'], order['side'], order['remaining']
message = f"*Trade ID:* `{trade.id}`\n" \ ) if order else None,
f"*Current Pair:* [{trade.pair}]({market_url})\n" \ ))
f"*Open Since:* `{trade_date}`\n" \ return results
f"*Amount:* `{amount}`\n" \
f"*Open Rate:* `{open_rate:.8f}`\n" \
f"*Close Rate:* `{close_rate}`\n" \
f"*Current Rate:* `{current_rate:.8f}`\n" \
f"*Close Profit:* `{fmt_close_profit}`\n" \
f"*Current Profit:* `{current_profit:.2f}%`\n" \
f"*Open Order:* `{open_order}`"\
result.append(message)
return result
def _rpc_status_table(self) -> DataFrame: def _rpc_status_table(self) -> DataFrame:
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.query.filter(Trade.is_open.is_(True)).all()

View File

@ -136,8 +136,22 @@ class Telegram(RPC):
return return
try: try:
for trade_msg in self._rpc_trade_status(): results = self._rpc_trade_status()
self._send_msg(trade_msg, bot=bot) messages = [
"*Trade ID:* `{trade_id}`\n"
"*Current Pair:* [{pair}]({market_url})\n"
"*Open Since:* `{date}`\n"
"*Amount:* `{amount}`\n"
"*Open Rate:* `{open_rate:.8f}`\n"
"*Close Rate:* `{close_rate}`\n"
"*Current Rate:* `{current_rate:.8f}`\n"
"*Close Profit:* `{close_profit}`\n"
"*Current Profit:* `{current_profit:.2f}%`\n"
"*Open Order:* `{open_order}`".format(**result)
for result in results
]
for msg in messages:
self._send_msg(msg, bot=bot)
except RPCException as e: except RPCException as e:
self._send_msg(str(e), bot=bot) self._send_msg(str(e), bot=bot)

View File

@ -53,24 +53,21 @@ def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None:
rpc._rpc_trade_status() rpc._rpc_trade_status()
freqtradebot.create_trade() freqtradebot.create_trade()
trades = rpc._rpc_trade_status() results = rpc._rpc_trade_status()
trade = trades[0]
result_message = [ assert {
'*Trade ID:* `1`\n' 'trade_id': 1,
'*Current Pair:* ' 'pair': 'ETH/BTC',
'[ETH/BTC](https://bittrex.com/Market/Index?MarketName=BTC-ETH)\n' 'market_url': 'https://bittrex.com/Market/Index?MarketName=BTC-ETH',
'*Open Since:* `just now`\n' 'date': 'just now',
'*Amount:* `90.99181074`\n' 'open_rate': 1.099e-05,
'*Open Rate:* `0.00001099`\n' 'close_rate': None,
'*Close Rate:* `None`\n' 'current_rate': 1.098e-05,
'*Current Rate:* `0.00001098`\n' 'amount': 90.99181074,
'*Close Profit:* `None`\n' 'close_profit': None,
'*Current Profit:* `-0.59%`\n' 'current_profit': -0.59,
'*Open Order:* `(limit buy rem=0.00000000)`' 'open_order': '(limit buy rem=0.00000000)'
] } == results[0]
assert trades == result_message
assert trade.find('[ETH/BTC]') >= 0
def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None: def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:

View File

@ -210,7 +210,19 @@ def test_status(default_conf, update, mocker, fee, ticker, markets) -> None:
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.rpc.telegram.Telegram', 'freqtrade.rpc.telegram.Telegram',
_init=MagicMock(), _init=MagicMock(),
_rpc_trade_status=MagicMock(return_value=[1, 2, 3]), _rpc_trade_status=MagicMock(return_value=[{
'trade_id': 1,
'pair': 'ETH/BTC',
'market_url': 'https://bittrex.com/Market/Index?MarketName=BTC-ETH',
'date': 'just now',
'open_rate': 1.099e-05,
'close_rate': None,
'current_rate': 1.098e-05,
'amount': 90.99181074,
'close_profit': None,
'current_profit': -0.59,
'open_order': '(limit buy rem=0.00000000)'
}]),
_status_table=status_table, _status_table=status_table,
_send_msg=msg_mock _send_msg=msg_mock
) )
@ -224,7 +236,7 @@ def test_status(default_conf, update, mocker, fee, ticker, markets) -> None:
freqtradebot.create_trade() freqtradebot.create_trade()
telegram._status(bot=MagicMock(), update=update) telegram._status(bot=MagicMock(), update=update)
assert msg_mock.call_count == 3 assert msg_mock.call_count == 1
update.message.text = MagicMock() update.message.text = MagicMock()
update.message.text.replace = MagicMock(return_value='table 2 3') update.message.text.replace = MagicMock(return_value='table 2 3')