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
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()
if not trades:
raise RPCException('no active trade')
else:
trades_list = []
fiat_profit_sum = NAN
for trade in trades:
# calculate profit and send message to user
try:
@ -243,6 +244,7 @@ class RPC:
)
if fiat_profit and not isnan(fiat_profit):
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([
trade.id,
trade.pair + ('*' if (trade.open_order_id is not None
@ -256,7 +258,7 @@ class RPC:
profitcol += " (" + fiat_display_currency + ")"
columns = ['ID', 'Pair', 'Since', profitcol]
return trades_list, columns
return trades_list, columns, fiat_profit_sum
def _rpc_daily_profit(
self, timescale: int,

View File

@ -5,10 +5,10 @@ This module manage Telegram communication
"""
import json
import logging
import re
from datetime import timedelta
from html import escape
from itertools import chain
from math import isnan
from typing import Any, Callable, Dict, List, Union
import arrow
@ -356,18 +356,10 @@ class Telegram(RPCHandler):
"""
try:
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)
show_total = fiat_currency != ''
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])
show_total = not isnan(fiat_profit_sum) and len(statlist) > 1
max_trades_per_msg = 50
"""
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]
if show_total and i == messages_count - 1:
# 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,
headers=head,

View File

@ -199,28 +199,31 @@ def test_rpc_status_table(default_conf, ticker, fee, mocker) -> None:
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 "Pair" in headers
assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1]
assert '-0.41%' == result[0][3]
assert isnan(fiat_profit_sum)
# Test with fiatconvert
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 "Pair" in headers
assert 'instantly' == result[0][2]
assert 'ETH/BTC' in result[0][1]
assert '-0.41% (-0.06)' == result[0][3]
assert -0.06 == fiat_profit_sum
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_sell_rate',
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 'ETH/BTC' in result[0][1]
assert 'nan%' == result[0][3]
assert isnan(fiat_profit_sum)
def test_rpc_daily_profit(default_conf, update, ticker, fee,