From c99ec4d5677db54ff70d5bb7e8912abe5ea081e1 Mon Sep 17 00:00:00 2001 From: kryofly Date: Fri, 26 Jan 2018 13:17:00 +0100 Subject: [PATCH] rpc refactor 8/ --- freqtrade/rpc/__init__.py | 23 +++++++++++++++++++++++ freqtrade/rpc/telegram.py | 24 ++++++++---------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/freqtrade/rpc/__init__.py b/freqtrade/rpc/__init__.py index 4d68b80d0..b0670969c 100644 --- a/freqtrade/rpc/__init__.py +++ b/freqtrade/rpc/__init__.py @@ -380,6 +380,7 @@ def rpc_stop(): return (True, '*Status:* `already stopped`') +# FIX: no test for this!!!! def rpc_forcesell(trade_id) -> None: """ Handler for forcesell . @@ -405,3 +406,25 @@ def rpc_forcesell(trade_id) -> None: return (True, 'Invalid argument. See `/help` to view usage') _exec_forcesell(trade) + + +def rpc_performance() -> None: + """ + Handler for performance. + Shows a performance statistic from finished trades + """ + if get_state() != State.RUNNING: + return (True, '`trader is not running`') + + pair_rates = Trade.session.query(Trade.pair, + sql.func.sum(Trade.close_profit).label('profit_sum'), + sql.func.count(Trade.pair).label('count')) \ + .filter(Trade.is_open.is_(False)) \ + .group_by(Trade.pair) \ + .order_by(sql.text('profit_sum DESC')) \ + .all() + trades = [] + for (pair, rate, count) in pair_rates: + trades.append({'pair': pair, 'profit': round(rate * 100, 2), 'count': count}) + + return (False, trades) diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index 130b30d41..dec630ea2 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -1,7 +1,6 @@ import logging from typing import Any, Callable -from sqlalchemy import func, text from tabulate import tabulate from telegram import Bot, ParseMode, ReplyKeyboardMarkup, Update from telegram.error import NetworkError, TelegramError @@ -15,6 +14,7 @@ from freqtrade.rpc.__init__ import (rpc_status_table, rpc_start, rpc_stop, rpc_forcesell, + rpc_performance, ) from freqtrade import __version__, exchange @@ -324,26 +324,18 @@ def _performance(bot: Bot, update: Update) -> None: :param update: message update :return: None """ - if get_state() != State.RUNNING: - send_msg('`trader is not running`', bot=bot) + (error, trades) = rpc_performance() + if error: + send_msg(trades, bot=bot) return - pair_rates = Trade.session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum'), - func.count(Trade.pair).label('count')) \ - .filter(Trade.is_open.is_(False)) \ - .group_by(Trade.pair) \ - .order_by(text('profit_sum DESC')) \ - .all() - stats = '\n'.join('{index}.\t{pair}\t{profit:.2f}% ({count})'.format( index=i + 1, - pair=pair, - profit=round(rate * 100, 2), - count=count - ) for i, (pair, rate, count) in enumerate(pair_rates)) - + pair=trade['pair'], + profit=trade['profit'], + count=trade['count'] + ) for i, trade in enumerate(trades)) message = 'Performance:\n{}'.format(stats) - logger.debug(message) send_msg(message, parse_mode=ParseMode.HTML)