Merge pull request #3321 from freqtrade/rpc/improve_daily
Improve daily API format
This commit is contained in:
commit
fd7fa3116a
@ -186,7 +186,7 @@ class RPC:
|
|||||||
|
|
||||||
def _rpc_daily_profit(
|
def _rpc_daily_profit(
|
||||||
self, timescale: int,
|
self, timescale: int,
|
||||||
stake_currency: str, fiat_display_currency: str) -> List[List[Any]]:
|
stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]:
|
||||||
today = datetime.utcnow().date()
|
today = datetime.utcnow().date()
|
||||||
profit_days: Dict[date, Dict] = {}
|
profit_days: Dict[date, Dict] = {}
|
||||||
|
|
||||||
@ -206,28 +206,26 @@ class RPC:
|
|||||||
'trades': len(trades)
|
'trades': len(trades)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
data = [
|
||||||
[
|
{
|
||||||
key,
|
'date': key,
|
||||||
'{value:.8f} {symbol}'.format(
|
'abs_profit': f'{float(value["amount"]):.8f}',
|
||||||
value=float(value['amount']),
|
'fiat_value': '{value:.3f}'.format(
|
||||||
symbol=stake_currency
|
|
||||||
),
|
|
||||||
'{value:.3f} {symbol}'.format(
|
|
||||||
value=self._fiat_converter.convert_amount(
|
value=self._fiat_converter.convert_amount(
|
||||||
value['amount'],
|
value['amount'],
|
||||||
stake_currency,
|
stake_currency,
|
||||||
fiat_display_currency
|
fiat_display_currency
|
||||||
) if self._fiat_converter else 0,
|
) if self._fiat_converter else 0,
|
||||||
symbol=fiat_display_currency
|
|
||||||
),
|
),
|
||||||
'{value} trade{s}'.format(
|
'trade_count': f'{value["trades"]}',
|
||||||
value=value['trades'],
|
}
|
||||||
s='' if value['trades'] < 2 else 's'
|
|
||||||
),
|
|
||||||
]
|
|
||||||
for key, value in profit_days.items()
|
for key, value in profit_days.items()
|
||||||
]
|
]
|
||||||
|
return {
|
||||||
|
'stake_currency': stake_currency,
|
||||||
|
'fiat_display_currency': fiat_display_currency,
|
||||||
|
'data': data
|
||||||
|
}
|
||||||
|
|
||||||
def _rpc_trade_history(self, limit: int) -> Dict:
|
def _rpc_trade_history(self, limit: int) -> Dict:
|
||||||
""" Returns the X last trades """
|
""" Returns the X last trades """
|
||||||
|
@ -280,14 +280,18 @@ class Telegram(RPC):
|
|||||||
stake_cur,
|
stake_cur,
|
||||||
fiat_disp_cur
|
fiat_disp_cur
|
||||||
)
|
)
|
||||||
stats_tab = tabulate(stats,
|
stats_tab = tabulate(
|
||||||
headers=[
|
[[day['date'],
|
||||||
'Day',
|
f"{day['abs_profit']} {stats['stake_currency']}",
|
||||||
f'Profit {stake_cur}',
|
f"{day['fiat_value']} {stats['fiat_display_currency']}",
|
||||||
f'Profit {fiat_disp_cur}',
|
f"{day['trade_count']} trades"] for day in stats['data']],
|
||||||
f'Trades'
|
headers=[
|
||||||
],
|
'Day',
|
||||||
tablefmt='simple')
|
f'Profit {stake_cur}',
|
||||||
|
f'Profit {fiat_disp_cur}',
|
||||||
|
f'Trades',
|
||||||
|
],
|
||||||
|
tablefmt='simple')
|
||||||
message = f'<b>Daily Profit over the last {timescale} days</b>:\n<pre>{stats_tab}</pre>'
|
message = f'<b>Daily Profit over the last {timescale} days</b>:\n<pre>{stats_tab}</pre>'
|
||||||
self._send_msg(message, parse_mode=ParseMode.HTML)
|
self._send_msg(message, parse_mode=ParseMode.HTML)
|
||||||
except RPCException as e:
|
except RPCException as e:
|
||||||
|
@ -205,16 +205,18 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
|
|||||||
# Try valid data
|
# Try valid data
|
||||||
update.message.text = '/daily 2'
|
update.message.text = '/daily 2'
|
||||||
days = rpc._rpc_daily_profit(7, stake_currency, fiat_display_currency)
|
days = rpc._rpc_daily_profit(7, stake_currency, fiat_display_currency)
|
||||||
assert len(days) == 7
|
assert len(days['data']) == 7
|
||||||
for day in days:
|
assert days['stake_currency'] == default_conf['stake_currency']
|
||||||
|
assert days['fiat_display_currency'] == default_conf['fiat_display_currency']
|
||||||
|
for day in days['data']:
|
||||||
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
|
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
|
||||||
assert (day[1] == '0.00000000 BTC' or
|
assert (day['abs_profit'] == '0.00000000' or
|
||||||
day[1] == '0.00006217 BTC')
|
day['abs_profit'] == '0.00006217')
|
||||||
|
|
||||||
assert (day[2] == '0.000 USD' or
|
assert (day['fiat_value'] == '0.000' or
|
||||||
day[2] == '0.767 USD')
|
day['fiat_value'] == '0.767')
|
||||||
# ensure first day is current date
|
# ensure first day is current date
|
||||||
assert str(days[0][0]) == str(datetime.utcnow().date())
|
assert str(days['data'][0]['date']) == str(datetime.utcnow().date())
|
||||||
|
|
||||||
# Try invalid data
|
# Try invalid data
|
||||||
with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'):
|
with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'):
|
||||||
|
@ -333,8 +333,10 @@ def test_api_daily(botclient, mocker, ticker, fee, markets):
|
|||||||
)
|
)
|
||||||
rc = client_get(client, f"{BASE_URI}/daily")
|
rc = client_get(client, f"{BASE_URI}/daily")
|
||||||
assert_response(rc)
|
assert_response(rc)
|
||||||
assert len(rc.json) == 7
|
assert len(rc.json['data']) == 7
|
||||||
assert rc.json[0][0] == str(datetime.utcnow().date())
|
assert rc.json['stake_currency'] == 'BTC'
|
||||||
|
assert rc.json['fiat_display_currency'] == 'USD'
|
||||||
|
assert rc.json['data'][0]['date'] == str(datetime.utcnow().date())
|
||||||
|
|
||||||
|
|
||||||
def test_api_trades(botclient, mocker, ticker, fee, markets):
|
def test_api_trades(botclient, mocker, ticker, fee, markets):
|
||||||
|
Loading…
Reference in New Issue
Block a user