Total row for telegram "/status table" command

moved sum calculation to API
This commit is contained in:
Eugene Schava 2021-05-17 15:59:03 +03:00
parent 196fde44e0
commit 3ad8fa2f38
3 changed files with 14 additions and 17 deletions

View File

@ -220,12 +220,13 @@ class RPC:
return results return results
def _rpc_status_table(self, stake_currency: str, def _rpc_status_table(self, stake_currency: str,
fiat_display_currency: str) -> Tuple[List, List]: fiat_display_currency: str) -> Tuple[List, List, float]:
trades = Trade.get_open_trades() trades = Trade.get_open_trades()
if not trades: if not trades:
raise RPCException('no active trade') raise RPCException('no active trade')
else: else:
trades_list = [] trades_list = []
fiat_profit_sum = NAN
for trade in trades: for trade in trades:
# calculate profit and send message to user # calculate profit and send message to user
try: try:
@ -243,6 +244,7 @@ class RPC:
) )
if fiat_profit and not isnan(fiat_profit): if fiat_profit and not isnan(fiat_profit):
profit_str += f" ({fiat_profit:.2f})" profit_str += f" ({fiat_profit:.2f})"
fiat_profit_sum = fiat_profit if isnan(fiat_profit_sum) else fiat_profit_sum + fiat_profit
trades_list.append([ trades_list.append([
trade.id, trade.id,
trade.pair + ('*' if (trade.open_order_id is not None trade.pair + ('*' if (trade.open_order_id is not None
@ -256,7 +258,7 @@ class RPC:
profitcol += " (" + fiat_display_currency + ")" profitcol += " (" + fiat_display_currency + ")"
columns = ['ID', 'Pair', 'Since', profitcol] columns = ['ID', 'Pair', 'Since', profitcol]
return trades_list, columns return trades_list, columns, fiat_profit_sum
def _rpc_daily_profit( def _rpc_daily_profit(
self, timescale: int, self, timescale: int,

View File

@ -5,10 +5,10 @@ This module manage Telegram communication
""" """
import json import json
import logging import logging
import re
from datetime import timedelta from datetime import timedelta
from html import escape from html import escape
from itertools import chain from itertools import chain
from math import isnan
from typing import Any, Callable, Dict, List, Union from typing import Any, Callable, Dict, List, Union
import arrow import arrow
@ -356,18 +356,10 @@ class Telegram(RPCHandler):
""" """
try: try:
fiat_currency = self._config.get('fiat_display_currency', '') fiat_currency = self._config.get('fiat_display_currency', '')
statlist, head = self._rpc._rpc_status_table( statlist, head, fiat_profit_sum = self._rpc._rpc_status_table(
self._config['stake_currency'], fiat_currency) self._config['stake_currency'], fiat_currency)
show_total = fiat_currency != '' show_total = not isnan(fiat_profit_sum) and len(statlist) > 1
total_sum = 0.0
if show_total:
r = re.compile(".*\\((-?\\d*\\.\\d*)\\)")
for trade in statlist:
m = r.match(trade[-1])
if m is not None:
total_sum += float(m[1])
max_trades_per_msg = 50 max_trades_per_msg = 50
""" """
Calculate the number of messages of 50 trades per message Calculate the number of messages of 50 trades per message
@ -379,7 +371,7 @@ class Telegram(RPCHandler):
trades = statlist[i * max_trades_per_msg:(i + 1) * max_trades_per_msg] trades = statlist[i * max_trades_per_msg:(i + 1) * max_trades_per_msg]
if show_total and i == messages_count - 1: if show_total and i == messages_count - 1:
# append total line # append total line
trades.append(["Total", "", "", f"{total_sum:.2f} {fiat_currency}"]) trades.append(["Total", "", "", f"{fiat_profit_sum:.2f} {fiat_currency}"])
message = tabulate(trades, message = tabulate(trades,
headers=head, headers=head,

View File

@ -199,28 +199,31 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
freqtradebot.enter_positions() freqtradebot.enter_positions()
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
assert "Since" in headers assert "Since" in headers
assert "Pair" in headers assert "Pair" in headers
assert 'instantly' == result[0][2] assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1] assert 'ETH/BTC' in result[0][1]
assert '-0.41%' == result[0][3] assert '-0.41%' == result[0][3]
assert isnan(fiat_profit_sum)
# Test with fiatconvert # Test with fiatconvert
rpc._fiat_converter = CryptoToFiatConverter() rpc._fiat_converter = CryptoToFiatConverter()
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
assert "Since" in headers assert "Since" in headers
assert "Pair" in headers assert "Pair" in headers
assert 'instantly' == result[0][2] assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1] assert 'ETH/BTC' in result[0][1]
assert '-0.41% (-0.06)' == result[0][3] assert '-0.41% (-0.06)' == result[0][3]
assert -0.06 == fiat_profit_sum
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate', mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available"))) MagicMock(side_effect=ExchangeError("Pair 'ETH/BTC' not available")))
result, headers = rpc._rpc_status_table(default_conf['stake_currency'], 'USD') result, headers, fiat_profit_sum = rpc._rpc_status_table(default_conf['stake_currency'], 'USD')
assert 'instantly' == result[0][2] assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1] assert 'ETH/BTC' in result[0][1]
assert 'nan%' == result[0][3] assert 'nan%' == result[0][3]
assert isnan(fiat_profit_sum)
def test_rpc_daily_profit(default_conf, update, ticker, fee, def test_rpc_daily_profit(default_conf, update, ticker, fee,