rpc refactor 8/

This commit is contained in:
kryofly 2018-01-26 13:17:00 +01:00
parent 64bbf3f9fb
commit c99ec4d567
2 changed files with 31 additions and 16 deletions

View File

@ -380,6 +380,7 @@ def rpc_stop():
return (True, '*Status:* `already stopped`') return (True, '*Status:* `already stopped`')
# FIX: no test for this!!!!
def rpc_forcesell(trade_id) -> None: def rpc_forcesell(trade_id) -> None:
""" """
Handler for forcesell <id>. Handler for forcesell <id>.
@ -405,3 +406,25 @@ def rpc_forcesell(trade_id) -> None:
return (True, 'Invalid argument. See `/help` to view usage') return (True, 'Invalid argument. See `/help` to view usage')
_exec_forcesell(trade) _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)

View File

@ -1,7 +1,6 @@
import logging import logging
from typing import Any, Callable from typing import Any, Callable
from sqlalchemy import func, text
from tabulate import tabulate from tabulate import tabulate
from telegram import Bot, ParseMode, ReplyKeyboardMarkup, Update from telegram import Bot, ParseMode, ReplyKeyboardMarkup, Update
from telegram.error import NetworkError, TelegramError from telegram.error import NetworkError, TelegramError
@ -15,6 +14,7 @@ from freqtrade.rpc.__init__ import (rpc_status_table,
rpc_start, rpc_start,
rpc_stop, rpc_stop,
rpc_forcesell, rpc_forcesell,
rpc_performance,
) )
from freqtrade import __version__, exchange from freqtrade import __version__, exchange
@ -324,26 +324,18 @@ def _performance(bot: Bot, update: Update) -> None:
:param update: message update :param update: message update
:return: None :return: None
""" """
if get_state() != State.RUNNING: (error, trades) = rpc_performance()
send_msg('`trader is not running`', bot=bot) if error:
send_msg(trades, bot=bot)
return 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<code>{pair}\t{profit:.2f}% ({count})</code>'.format( stats = '\n'.join('{index}.\t<code>{pair}\t{profit:.2f}% ({count})</code>'.format(
index=i + 1, index=i + 1,
pair=pair, pair=trade['pair'],
profit=round(rate * 100, 2), profit=trade['profit'],
count=count count=trade['count']
) for i, (pair, rate, count) in enumerate(pair_rates)) ) for i, trade in enumerate(trades))
message = '<b>Performance:</b>\n{}'.format(stats) message = '<b>Performance:</b>\n{}'.format(stats)
logger.debug(message)
send_msg(message, parse_mode=ParseMode.HTML) send_msg(message, parse_mode=ParseMode.HTML)