2017-05-14 12:14:16 +00:00
|
|
|
import logging
|
2017-11-02 17:25:19 +00:00
|
|
|
import re
|
2018-01-10 07:51:36 +00:00
|
|
|
from datetime import datetime, timedelta
|
2017-12-15 22:58:21 +00:00
|
|
|
from decimal import Decimal
|
2018-01-10 07:51:36 +00:00
|
|
|
from typing import Any, Callable
|
2017-05-12 17:11:56 +00:00
|
|
|
|
2017-05-14 12:14:16 +00:00
|
|
|
import arrow
|
2017-11-20 21:26:32 +00:00
|
|
|
from pandas import DataFrame
|
2017-12-27 20:33:42 +00:00
|
|
|
from sqlalchemy import and_, func, text
|
2017-11-20 21:26:32 +00:00
|
|
|
from tabulate import tabulate
|
2018-01-10 07:51:36 +00:00
|
|
|
from telegram import Bot, ParseMode, ReplyKeyboardMarkup, Update
|
2017-11-17 18:47:29 +00:00
|
|
|
from telegram.error import NetworkError, TelegramError
|
2017-05-12 17:11:56 +00:00
|
|
|
from telegram.ext import CommandHandler, Updater
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2018-01-10 07:51:36 +00:00
|
|
|
from freqtrade import __version__, exchange
|
2017-12-25 07:51:41 +00:00
|
|
|
from freqtrade.fiat_convert import CryptoToFiatConverter
|
2018-01-10 07:51:36 +00:00
|
|
|
from freqtrade.misc import State, get_state, update_state
|
|
|
|
from freqtrade.persistence import Trade
|
2017-05-12 17:11:56 +00:00
|
|
|
|
2017-05-14 12:14:16 +00:00
|
|
|
# Remove noisy log messages
|
|
|
|
logging.getLogger('requests.packages.urllib3').setLevel(logging.INFO)
|
|
|
|
logging.getLogger('telegram').setLevel(logging.INFO)
|
2017-05-12 17:11:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-11-07 19:13:36 +00:00
|
|
|
_UPDATER: Updater = None
|
2017-09-08 21:10:22 +00:00
|
|
|
_CONF = {}
|
2017-12-25 07:51:41 +00:00
|
|
|
_FIAT_CONVERT = CryptoToFiatConverter()
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init(config: dict) -> None:
|
|
|
|
"""
|
|
|
|
Initializes this module with the given config,
|
|
|
|
registers all known command handlers
|
|
|
|
and starts polling for message updates
|
|
|
|
:param config: config to use
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-11-07 19:13:36 +00:00
|
|
|
global _UPDATER
|
2017-09-08 19:17:13 +00:00
|
|
|
|
2017-09-08 21:10:22 +00:00
|
|
|
_CONF.update(config)
|
2017-11-02 17:56:57 +00:00
|
|
|
if not is_enabled():
|
2017-09-28 22:15:38 +00:00
|
|
|
return
|
|
|
|
|
2017-11-07 19:13:36 +00:00
|
|
|
_UPDATER = Updater(token=config['telegram']['token'], workers=0)
|
2017-05-12 17:11:56 +00:00
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
# Register command handler and start telegram message polling
|
|
|
|
handles = [
|
|
|
|
CommandHandler('status', _status),
|
|
|
|
CommandHandler('profit', _profit),
|
2017-10-28 05:44:49 +00:00
|
|
|
CommandHandler('balance', _balance),
|
2017-09-08 13:51:00 +00:00
|
|
|
CommandHandler('start', _start),
|
|
|
|
CommandHandler('stop', _stop),
|
|
|
|
CommandHandler('forcesell', _forcesell),
|
|
|
|
CommandHandler('performance', _performance),
|
2017-12-10 06:32:40 +00:00
|
|
|
CommandHandler('daily', _daily),
|
2017-10-23 20:43:05 +00:00
|
|
|
CommandHandler('count', _count),
|
2017-10-21 08:08:08 +00:00
|
|
|
CommandHandler('help', _help),
|
2017-11-09 22:51:32 +00:00
|
|
|
CommandHandler('version', _version),
|
2017-09-08 13:51:00 +00:00
|
|
|
]
|
|
|
|
for handle in handles:
|
2017-11-07 19:13:36 +00:00
|
|
|
_UPDATER.dispatcher.add_handler(handle)
|
|
|
|
_UPDATER.start_polling(
|
2017-09-08 13:51:00 +00:00
|
|
|
clean=True,
|
2017-11-18 21:23:05 +00:00
|
|
|
bootstrap_retries=-1,
|
2017-09-08 13:51:00 +00:00
|
|
|
timeout=30,
|
|
|
|
read_latency=60,
|
|
|
|
)
|
2017-09-08 21:10:22 +00:00
|
|
|
logger.info(
|
|
|
|
'rpc.telegram is listening for following commands: %s',
|
|
|
|
[h.command for h in handles]
|
|
|
|
)
|
2017-05-12 17:11:56 +00:00
|
|
|
|
|
|
|
|
2017-10-27 13:52:14 +00:00
|
|
|
def cleanup() -> None:
|
|
|
|
"""
|
|
|
|
Stops all running telegram threads.
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-11-02 17:56:57 +00:00
|
|
|
if not is_enabled():
|
|
|
|
return
|
2017-11-07 19:13:36 +00:00
|
|
|
_UPDATER.stop()
|
2017-10-27 13:52:14 +00:00
|
|
|
|
|
|
|
|
2017-11-02 17:56:57 +00:00
|
|
|
def is_enabled() -> bool:
|
|
|
|
"""
|
|
|
|
Returns True if the telegram module is activated, False otherwise
|
|
|
|
"""
|
|
|
|
return bool(_CONF['telegram'].get('enabled', False))
|
|
|
|
|
|
|
|
|
2017-09-01 23:22:20 +00:00
|
|
|
def authorized_only(command_handler: Callable[[Bot, Update], None]) -> Callable[..., Any]:
|
2017-05-14 12:14:16 +00:00
|
|
|
"""
|
|
|
|
Decorator to check if the message comes from the correct chat_id
|
|
|
|
:param command_handler: Telegram CommandHandler
|
|
|
|
:return: decorated function
|
|
|
|
"""
|
|
|
|
def wrapper(*args, **kwargs):
|
2017-12-16 02:39:47 +00:00
|
|
|
update = kwargs.get('update') or args[1]
|
2017-09-08 17:25:39 +00:00
|
|
|
|
2017-11-07 21:27:16 +00:00
|
|
|
# Reject unauthorized messages
|
2017-09-08 21:10:22 +00:00
|
|
|
chat_id = int(_CONF['telegram']['chat_id'])
|
2017-11-07 21:27:16 +00:00
|
|
|
if int(update.message.chat_id) != chat_id:
|
2017-08-27 13:50:59 +00:00
|
|
|
logger.info('Rejected unauthorized message from: %s', update.message.chat_id)
|
2017-11-07 21:27:16 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
logger.info('Executing handler: %s for chat_id: %s', command_handler.__name__, chat_id)
|
|
|
|
try:
|
|
|
|
return command_handler(*args, **kwargs)
|
|
|
|
except BaseException:
|
|
|
|
logger.exception('Exception occurred within Telegram module')
|
2017-05-14 12:14:16 +00:00
|
|
|
return wrapper
|
2017-05-12 17:11:56 +00:00
|
|
|
|
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
@authorized_only
|
|
|
|
def _status(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /status.
|
|
|
|
Returns the current TradeThread status
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-10-29 22:57:48 +00:00
|
|
|
|
|
|
|
# Check if additional parameters are passed
|
|
|
|
params = update.message.text.replace('/status', '').split(' ') \
|
|
|
|
if update.message.text else []
|
|
|
|
if 'table' in params:
|
|
|
|
_status_table(bot, update)
|
|
|
|
return
|
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
# Fetch open trade
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
2017-09-08 21:10:22 +00:00
|
|
|
if get_state() != State.RUNNING:
|
2017-09-08 13:51:00 +00:00
|
|
|
send_msg('*Status:* `trader is not running`', bot=bot)
|
|
|
|
elif not trades:
|
2017-10-31 23:22:38 +00:00
|
|
|
send_msg('*Status:* `no active trade`', bot=bot)
|
2017-09-08 13:51:00 +00:00
|
|
|
else:
|
|
|
|
for trade in trades:
|
2017-11-07 16:39:57 +00:00
|
|
|
order = None
|
|
|
|
if trade.open_order_id:
|
|
|
|
order = exchange.get_order(trade.open_order_id)
|
2017-09-08 13:51:00 +00:00
|
|
|
# calculate profit and send message to user
|
2018-01-02 09:56:42 +00:00
|
|
|
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
2017-12-17 21:07:56 +00:00
|
|
|
current_profit = trade.calc_profit_percent(current_rate)
|
2017-09-08 21:10:22 +00:00
|
|
|
fmt_close_profit = '{:.2f}%'.format(
|
2017-10-31 23:54:16 +00:00
|
|
|
round(trade.close_profit * 100, 2)
|
2017-09-08 21:10:22 +00:00
|
|
|
) if trade.close_profit else None
|
2017-09-08 13:51:00 +00:00
|
|
|
message = """
|
2017-05-17 21:36:08 +00:00
|
|
|
*Trade ID:* `{trade_id}`
|
2017-06-05 19:17:10 +00:00
|
|
|
*Current Pair:* [{pair}]({market_url})
|
2017-05-12 17:11:56 +00:00
|
|
|
*Open Since:* `{date}`
|
|
|
|
*Amount:* `{amount}`
|
2017-11-05 15:13:55 +00:00
|
|
|
*Open Rate:* `{open_rate:.8f}`
|
2017-05-12 17:11:56 +00:00
|
|
|
*Close Rate:* `{close_rate}`
|
2017-11-05 15:13:55 +00:00
|
|
|
*Current Rate:* `{current_rate:.8f}`
|
2017-05-12 22:30:08 +00:00
|
|
|
*Close Profit:* `{close_profit}`
|
2017-09-08 17:45:54 +00:00
|
|
|
*Current Profit:* `{current_profit:.2f}%`
|
2017-05-12 17:11:56 +00:00
|
|
|
*Open Order:* `{open_order}`
|
2017-09-08 13:51:00 +00:00
|
|
|
""".format(
|
|
|
|
trade_id=trade.id,
|
|
|
|
pair=trade.pair,
|
|
|
|
market_url=exchange.get_pair_detail_url(trade.pair),
|
|
|
|
date=arrow.get(trade.open_date).humanize(),
|
|
|
|
open_rate=trade.open_rate,
|
|
|
|
close_rate=trade.close_rate,
|
|
|
|
current_rate=current_rate,
|
|
|
|
amount=round(trade.amount, 8),
|
2017-09-08 21:10:22 +00:00
|
|
|
close_profit=fmt_close_profit,
|
2017-10-31 23:54:16 +00:00
|
|
|
current_profit=round(current_profit * 100, 2),
|
2017-11-20 22:33:52 +00:00
|
|
|
open_order='({} rem={:.8f})'.format(
|
|
|
|
order['type'], order['remaining']
|
2017-10-31 23:54:16 +00:00
|
|
|
) if order else None,
|
2017-09-08 13:51:00 +00:00
|
|
|
)
|
|
|
|
send_msg(message, bot=bot)
|
|
|
|
|
|
|
|
|
2017-10-29 22:57:48 +00:00
|
|
|
@authorized_only
|
|
|
|
def _status_table(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /status table.
|
|
|
|
Returns the current TradeThread status in table format
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
# Fetch open trade
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
if get_state() != State.RUNNING:
|
|
|
|
send_msg('*Status:* `trader is not running`', bot=bot)
|
|
|
|
elif not trades:
|
|
|
|
send_msg('*Status:* `no active order`', bot=bot)
|
|
|
|
else:
|
|
|
|
trades_list = []
|
|
|
|
for trade in trades:
|
|
|
|
# calculate profit and send message to user
|
2018-01-02 09:56:42 +00:00
|
|
|
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
2017-11-05 17:35:32 +00:00
|
|
|
trades_list.append([
|
2017-10-29 22:57:48 +00:00
|
|
|
trade.id,
|
|
|
|
trade.pair,
|
2017-11-02 17:25:19 +00:00
|
|
|
shorten_date(arrow.get(trade.open_date).humanize(only_distance=True)),
|
2017-12-22 11:58:13 +00:00
|
|
|
'{:.2f}%'.format(100 * trade.calc_profit_percent(current_rate))
|
2017-11-05 17:35:32 +00:00
|
|
|
])
|
2017-10-29 22:57:48 +00:00
|
|
|
|
2017-11-02 17:25:19 +00:00
|
|
|
columns = ['ID', 'Pair', 'Since', 'Profit']
|
|
|
|
df_statuses = DataFrame.from_records(trades_list, columns=columns)
|
|
|
|
df_statuses = df_statuses.set_index(columns[0])
|
2017-10-29 22:57:48 +00:00
|
|
|
|
2017-11-02 17:25:19 +00:00
|
|
|
message = tabulate(df_statuses, headers='keys', tablefmt='simple')
|
2017-10-29 22:57:48 +00:00
|
|
|
message = "<pre>{}</pre>".format(message)
|
|
|
|
|
|
|
|
send_msg(message, parse_mode=ParseMode.HTML)
|
|
|
|
|
2017-12-15 22:56:02 +00:00
|
|
|
|
2017-12-10 06:32:40 +00:00
|
|
|
@authorized_only
|
|
|
|
def _daily(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /daily <n>
|
|
|
|
Returns a daily profit (in BTC) over the last n days.
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-12-27 20:06:05 +00:00
|
|
|
today = datetime.utcnow().date()
|
2017-12-10 06:32:40 +00:00
|
|
|
profit_days = {}
|
2017-12-15 22:56:02 +00:00
|
|
|
|
2017-12-10 06:32:40 +00:00
|
|
|
try:
|
2017-12-12 08:38:18 +00:00
|
|
|
timescale = int(update.message.text.replace('/daily', '').strip())
|
2017-12-15 22:56:02 +00:00
|
|
|
except (TypeError, ValueError):
|
2017-12-27 20:06:05 +00:00
|
|
|
timescale = 7
|
2017-12-15 22:56:02 +00:00
|
|
|
|
2017-12-12 08:38:18 +00:00
|
|
|
if not (isinstance(timescale, int) and timescale > 0):
|
2017-12-15 04:59:38 +00:00
|
|
|
send_msg('*Daily [n]:* `must be an integer greater than 0`', bot=bot)
|
2017-12-12 08:38:18 +00:00
|
|
|
return
|
2017-12-10 06:32:40 +00:00
|
|
|
|
|
|
|
for day in range(0, timescale):
|
2017-12-27 20:06:05 +00:00
|
|
|
profitday = today - timedelta(days=day)
|
2017-12-17 21:07:56 +00:00
|
|
|
trades = Trade.query \
|
|
|
|
.filter(Trade.is_open.is_(False)) \
|
2017-12-27 20:06:05 +00:00
|
|
|
.filter(Trade.close_date >= profitday)\
|
|
|
|
.filter(Trade.close_date < (profitday + timedelta(days=1)))\
|
2017-12-17 21:07:56 +00:00
|
|
|
.order_by(Trade.close_date)\
|
|
|
|
.all()
|
|
|
|
curdayprofit = sum(trade.calc_profit() for trade in trades)
|
2018-01-20 06:09:28 +00:00
|
|
|
profit_days[profitday] = {
|
|
|
|
'amount': format(curdayprofit, '.8f'),
|
|
|
|
'trades': len(trades)
|
|
|
|
}
|
2017-12-15 22:56:02 +00:00
|
|
|
|
2017-12-25 07:51:41 +00:00
|
|
|
stats = [
|
|
|
|
[
|
|
|
|
key,
|
2018-01-20 06:09:28 +00:00
|
|
|
'{value:.8f} {symbol}'.format(
|
|
|
|
value=float(value['amount']),
|
|
|
|
symbol=_CONF['stake_currency']
|
|
|
|
),
|
2017-12-25 07:51:41 +00:00
|
|
|
'{value:.3f} {symbol}'.format(
|
|
|
|
value=_FIAT_CONVERT.convert_amount(
|
2018-01-20 06:09:28 +00:00
|
|
|
value['amount'],
|
2017-12-25 07:51:41 +00:00
|
|
|
_CONF['stake_currency'],
|
|
|
|
_CONF['fiat_display_currency']
|
|
|
|
),
|
|
|
|
symbol=_CONF['fiat_display_currency']
|
2018-01-20 06:09:28 +00:00
|
|
|
),
|
|
|
|
'{value} trade{s}'.format(value=value['trades'], s='' if value['trades'] < 2 else 's'),
|
2018-01-09 10:59:06 +00:00
|
|
|
]
|
2017-12-25 07:51:41 +00:00
|
|
|
for key, value in profit_days.items()
|
|
|
|
]
|
|
|
|
stats = tabulate(stats,
|
|
|
|
headers=[
|
|
|
|
'Day',
|
|
|
|
'Profit {}'.format(_CONF['stake_currency']),
|
2018-01-20 06:09:28 +00:00
|
|
|
'Profit {}'.format(_CONF['fiat_display_currency']),
|
|
|
|
'# Trades'
|
2017-12-25 07:51:41 +00:00
|
|
|
],
|
|
|
|
tablefmt='simple')
|
2017-12-12 08:38:18 +00:00
|
|
|
|
|
|
|
message = '<b>Daily Profit over the last {} days</b>:\n<pre>{}</pre>'.format(timescale, stats)
|
|
|
|
send_msg(message, bot=bot, parse_mode=ParseMode.HTML)
|
2017-12-15 22:56:02 +00:00
|
|
|
|
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
@authorized_only
|
|
|
|
def _profit(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /profit.
|
|
|
|
Returns a cumulative profit statistics.
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
trades = Trade.query.order_by(Trade.id).all()
|
|
|
|
|
2017-12-25 07:51:41 +00:00
|
|
|
profit_all_coin = []
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_all_percent = []
|
2017-12-25 07:51:41 +00:00
|
|
|
profit_closed_coin = []
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_closed_percent = []
|
2017-09-08 13:51:00 +00:00
|
|
|
durations = []
|
2017-12-15 16:19:00 +00:00
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
for trade in trades:
|
2017-12-15 16:19:00 +00:00
|
|
|
current_rate = None
|
|
|
|
|
2017-10-31 23:22:38 +00:00
|
|
|
if not trade.open_rate:
|
|
|
|
continue
|
2017-09-08 13:51:00 +00:00
|
|
|
if trade.close_date:
|
|
|
|
durations.append((trade.close_date - trade.open_date).total_seconds())
|
2017-12-15 16:19:00 +00:00
|
|
|
|
|
|
|
if not trade.is_open:
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_percent = trade.calc_profit_percent()
|
2017-12-25 07:51:41 +00:00
|
|
|
profit_closed_coin.append(trade.calc_profit())
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_closed_percent.append(profit_percent)
|
2017-09-08 13:51:00 +00:00
|
|
|
else:
|
|
|
|
# Get current rate
|
2018-01-02 09:56:42 +00:00
|
|
|
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_percent = trade.calc_profit_percent(rate=current_rate)
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2017-12-25 07:51:41 +00:00
|
|
|
profit_all_coin.append(trade.calc_profit(rate=Decimal(trade.close_rate or current_rate)))
|
2017-12-17 21:07:56 +00:00
|
|
|
profit_all_percent.append(profit_percent)
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2017-09-08 17:25:39 +00:00
|
|
|
best_pair = Trade.session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum')) \
|
2017-09-08 13:51:00 +00:00
|
|
|
.filter(Trade.is_open.is_(False)) \
|
|
|
|
.group_by(Trade.pair) \
|
2017-09-08 17:25:39 +00:00
|
|
|
.order_by(text('profit_sum DESC')) \
|
2017-09-08 13:51:00 +00:00
|
|
|
.first()
|
|
|
|
|
2017-09-08 17:25:39 +00:00
|
|
|
if not best_pair:
|
|
|
|
send_msg('*Status:* `no closed trade`', bot=bot)
|
|
|
|
return
|
|
|
|
|
|
|
|
bp_pair, bp_rate = best_pair
|
2017-12-25 07:51:41 +00:00
|
|
|
|
|
|
|
# Prepare data to display
|
|
|
|
profit_closed_coin = round(sum(profit_closed_coin), 8)
|
|
|
|
profit_closed_percent = round(sum(profit_closed_percent) * 100, 2)
|
|
|
|
profit_closed_fiat = _FIAT_CONVERT.convert_amount(
|
|
|
|
profit_closed_coin,
|
|
|
|
_CONF['stake_currency'],
|
|
|
|
_CONF['fiat_display_currency']
|
|
|
|
)
|
|
|
|
profit_all_coin = round(sum(profit_all_coin), 8)
|
|
|
|
profit_all_percent = round(sum(profit_all_percent) * 100, 2)
|
|
|
|
profit_all_fiat = _FIAT_CONVERT.convert_amount(
|
|
|
|
profit_all_coin,
|
|
|
|
_CONF['stake_currency'],
|
|
|
|
_CONF['fiat_display_currency']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Message to display
|
2017-09-08 13:51:00 +00:00
|
|
|
markdown_msg = """
|
2017-12-25 07:51:41 +00:00
|
|
|
*ROI:* Close trades
|
|
|
|
∙ `{profit_closed_coin:.8f} {coin} ({profit_closed_percent:.2f}%)`
|
|
|
|
∙ `{profit_closed_fiat:.3f} {fiat}`
|
|
|
|
*ROI:* All trades
|
|
|
|
∙ `{profit_all_coin:.8f} {coin} ({profit_all_percent:.2f}%)`
|
|
|
|
∙ `{profit_all_fiat:.3f} {fiat}`
|
|
|
|
|
2017-12-15 16:19:00 +00:00
|
|
|
*Total Trade Count:* `{trade_count}`
|
2017-06-08 20:52:38 +00:00
|
|
|
*First Trade opened:* `{first_trade_date}`
|
|
|
|
*Latest Trade opened:* `{latest_trade_date}`
|
|
|
|
*Avg. Duration:* `{avg_duration}`
|
2017-09-08 17:45:54 +00:00
|
|
|
*Best Performing:* `{best_pair}: {best_rate:.2f}%`
|
2017-09-08 13:51:00 +00:00
|
|
|
""".format(
|
2017-12-25 07:51:41 +00:00
|
|
|
coin=_CONF['stake_currency'],
|
|
|
|
fiat=_CONF['fiat_display_currency'],
|
|
|
|
profit_closed_coin=profit_closed_coin,
|
|
|
|
profit_closed_percent=profit_closed_percent,
|
|
|
|
profit_closed_fiat=profit_closed_fiat,
|
|
|
|
profit_all_coin=profit_all_coin,
|
|
|
|
profit_all_percent=profit_all_percent,
|
|
|
|
profit_all_fiat=profit_all_fiat,
|
2017-09-08 13:51:00 +00:00
|
|
|
trade_count=len(trades),
|
|
|
|
first_trade_date=arrow.get(trades[0].open_date).humanize(),
|
|
|
|
latest_trade_date=arrow.get(trades[-1].open_date).humanize(),
|
|
|
|
avg_duration=str(timedelta(seconds=sum(durations) / float(len(durations)))).split('.')[0],
|
|
|
|
best_pair=bp_pair,
|
2017-10-31 23:22:38 +00:00
|
|
|
best_rate=round(bp_rate * 100, 2),
|
2017-09-08 13:51:00 +00:00
|
|
|
)
|
|
|
|
send_msg(markdown_msg, bot=bot)
|
2017-06-08 18:01:01 +00:00
|
|
|
|
2017-06-08 20:52:38 +00:00
|
|
|
|
2017-10-28 05:44:49 +00:00
|
|
|
@authorized_only
|
|
|
|
def _balance(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
2017-10-30 23:29:22 +00:00
|
|
|
Handler for /balance
|
2017-10-28 05:44:49 +00:00
|
|
|
Returns current account balance per crypto
|
|
|
|
"""
|
2017-11-15 23:01:47 +00:00
|
|
|
output = ''
|
|
|
|
balances = [
|
|
|
|
c for c in exchange.get_balances()
|
|
|
|
if c['Balance'] or c['Available'] or c['Pending']
|
|
|
|
]
|
|
|
|
if not balances:
|
|
|
|
output = '`All balances are zero.`'
|
|
|
|
|
2018-01-13 07:58:23 +00:00
|
|
|
total = 0.0
|
2017-10-29 08:10:00 +00:00
|
|
|
for currency in balances:
|
2018-01-13 07:58:23 +00:00
|
|
|
coin = currency['Currency']
|
|
|
|
if coin == 'BTC':
|
|
|
|
currency["Rate"] = 1.0
|
|
|
|
else:
|
|
|
|
currency["Rate"] = exchange.get_ticker('BTC_' + coin, False)['bid']
|
|
|
|
currency['BTC'] = currency["Rate"] * currency["Balance"]
|
|
|
|
total = total + currency['BTC']
|
2018-01-15 05:07:01 +00:00
|
|
|
output += """*Currency*: {Currency}
|
2017-10-28 05:44:49 +00:00
|
|
|
*Available*: {Available}
|
|
|
|
*Balance*: {Balance}
|
|
|
|
*Pending*: {Pending}
|
2018-01-13 07:58:23 +00:00
|
|
|
*Est. BTC*: {BTC: .8f}
|
2017-10-28 05:44:49 +00:00
|
|
|
|
2017-10-29 08:10:00 +00:00
|
|
|
""".format(**currency)
|
2018-01-13 07:58:23 +00:00
|
|
|
|
|
|
|
symbol = _CONF['fiat_display_currency']
|
|
|
|
value = _FIAT_CONVERT.convert_amount(
|
|
|
|
total, 'BTC', symbol
|
|
|
|
)
|
|
|
|
output += """*Estimated Value*:
|
|
|
|
*BTC*: {0: .8f}
|
|
|
|
*{1}*: {2: .2f}
|
|
|
|
""".format(total, symbol, value)
|
2017-10-28 05:44:49 +00:00
|
|
|
send_msg(output)
|
2017-06-08 20:52:38 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-09-08 13:51:00 +00:00
|
|
|
@authorized_only
|
|
|
|
def _start(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /start.
|
|
|
|
Starts TradeThread
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if get_state() == State.RUNNING:
|
|
|
|
send_msg('*Status:* `already running`', bot=bot)
|
|
|
|
else:
|
|
|
|
update_state(State.RUNNING)
|
|
|
|
|
|
|
|
|
|
|
|
@authorized_only
|
|
|
|
def _stop(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /stop.
|
|
|
|
Stops TradeThread
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if get_state() == State.RUNNING:
|
|
|
|
send_msg('`Stopping trader ...`', bot=bot)
|
2017-09-08 22:31:40 +00:00
|
|
|
update_state(State.STOPPED)
|
2017-09-08 13:51:00 +00:00
|
|
|
else:
|
|
|
|
send_msg('*Status:* `already stopped`', bot=bot)
|
|
|
|
|
|
|
|
|
|
|
|
@authorized_only
|
|
|
|
def _forcesell(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /forcesell <id>.
|
|
|
|
Sells the given trade at current price
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if get_state() != State.RUNNING:
|
|
|
|
send_msg('`trader is not running`', bot=bot)
|
|
|
|
return
|
|
|
|
|
2017-11-09 21:07:51 +00:00
|
|
|
trade_id = update.message.text.replace('/forcesell', '').strip()
|
|
|
|
if trade_id == 'all':
|
|
|
|
# Execute sell for all open orders
|
|
|
|
for trade in Trade.query.filter(Trade.is_open.is_(True)).all():
|
2017-12-16 00:09:07 +00:00
|
|
|
_exec_forcesell(trade)
|
2017-11-09 21:07:51 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Query for trade
|
|
|
|
trade = Trade.query.filter(and_(
|
|
|
|
Trade.id == trade_id,
|
|
|
|
Trade.is_open.is_(True)
|
|
|
|
)).first()
|
|
|
|
if not trade:
|
2017-11-09 22:45:03 +00:00
|
|
|
send_msg('Invalid argument. See `/help` to view usage')
|
2017-09-08 13:51:00 +00:00
|
|
|
logger.warning('/forcesell: Invalid argument received')
|
2017-11-09 21:07:51 +00:00
|
|
|
return
|
2017-12-16 00:09:07 +00:00
|
|
|
|
|
|
|
_exec_forcesell(trade)
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@authorized_only
|
|
|
|
def _performance(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /performance.
|
|
|
|
Shows a performance statistic from finished trades
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if get_state() != State.RUNNING:
|
|
|
|
send_msg('`trader is not running`', bot=bot)
|
|
|
|
return
|
|
|
|
|
2018-01-02 13:32:13 +00:00
|
|
|
pair_rates = Trade.session.query(Trade.pair, func.sum(Trade.close_profit).label('profit_sum'),
|
|
|
|
func.count(Trade.pair).label('count')) \
|
2017-09-08 13:51:00 +00:00
|
|
|
.filter(Trade.is_open.is_(False)) \
|
|
|
|
.group_by(Trade.pair) \
|
2017-09-08 17:52:20 +00:00
|
|
|
.order_by(text('profit_sum DESC')) \
|
2017-09-08 13:51:00 +00:00
|
|
|
.all()
|
|
|
|
|
2018-01-02 13:55:31 +00:00
|
|
|
stats = '\n'.join('{index}.\t<code>{pair}\t{profit:.2f}% ({count})</code>'.format(
|
2017-09-08 17:50:48 +00:00
|
|
|
index=i + 1,
|
|
|
|
pair=pair,
|
2018-01-02 13:32:13 +00:00
|
|
|
profit=round(rate * 100, 2),
|
|
|
|
count=count
|
|
|
|
) for i, (pair, rate, count) in enumerate(pair_rates))
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2017-11-05 14:35:15 +00:00
|
|
|
message = '<b>Performance:</b>\n{}'.format(stats)
|
2017-09-08 13:51:00 +00:00
|
|
|
logger.debug(message)
|
|
|
|
send_msg(message, parse_mode=ParseMode.HTML)
|
|
|
|
|
|
|
|
|
2017-10-23 20:43:05 +00:00
|
|
|
@authorized_only
|
2017-11-05 15:26:03 +00:00
|
|
|
def _count(bot: Bot, update: Update) -> None:
|
2017-10-23 20:43:05 +00:00
|
|
|
"""
|
|
|
|
Handler for /count.
|
|
|
|
Returns the number of trades running
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
if get_state() != State.RUNNING:
|
|
|
|
send_msg('`trader is not running`', bot=bot)
|
|
|
|
return
|
|
|
|
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
|
2017-11-09 22:45:03 +00:00
|
|
|
message = tabulate({
|
|
|
|
'current': [len(trades)],
|
|
|
|
'max': [_CONF['max_open_trades']]
|
|
|
|
}, headers=['current', 'max'], tablefmt='simple')
|
|
|
|
message = "<pre>{}</pre>".format(message)
|
2017-10-23 20:43:05 +00:00
|
|
|
logger.debug(message)
|
|
|
|
send_msg(message, parse_mode=ParseMode.HTML)
|
|
|
|
|
|
|
|
|
2017-10-21 08:08:08 +00:00
|
|
|
@authorized_only
|
|
|
|
def _help(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /help.
|
|
|
|
Show commands of the bot
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
message = """
|
|
|
|
*/start:* `Starts the trader`
|
|
|
|
*/stop:* `Stops the trader`
|
2017-11-02 17:25:19 +00:00
|
|
|
*/status [table]:* `Lists all open trades`
|
2017-10-29 22:57:48 +00:00
|
|
|
*table :* `will display trades in a table`
|
2017-10-21 08:08:08 +00:00
|
|
|
*/profit:* `Lists cumulative profit from all finished trades`
|
2017-11-09 21:07:51 +00:00
|
|
|
*/forcesell <trade_id>|all:* `Instantly sells the given trade or all trades, regardless of profit`
|
2017-10-21 08:08:08 +00:00
|
|
|
*/performance:* `Show performance of each finished trade grouped by pair`
|
2017-12-12 08:38:18 +00:00
|
|
|
*/daily <n>:* `Shows profit or loss per day, over the last n days`
|
2017-10-29 22:57:48 +00:00
|
|
|
*/count:* `Show number of trades running compared to allowed number of trades`
|
2017-10-29 08:10:00 +00:00
|
|
|
*/balance:* `Show account balance per currency`
|
2017-10-21 08:08:08 +00:00
|
|
|
*/help:* `This help message`
|
2017-11-09 22:51:32 +00:00
|
|
|
*/version:* `Show version`
|
2017-10-21 08:08:08 +00:00
|
|
|
"""
|
|
|
|
send_msg(message, bot=bot)
|
|
|
|
|
|
|
|
|
2017-11-09 22:51:32 +00:00
|
|
|
@authorized_only
|
|
|
|
def _version(bot: Bot, update: Update) -> None:
|
|
|
|
"""
|
|
|
|
Handler for /version.
|
|
|
|
Show version information
|
|
|
|
:param bot: telegram bot
|
|
|
|
:param update: message update
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
send_msg('*Version:* `{}`'.format(__version__), bot=bot)
|
|
|
|
|
|
|
|
|
2017-12-15 22:56:02 +00:00
|
|
|
def shorten_date(_date):
|
2017-11-02 17:25:19 +00:00
|
|
|
"""
|
|
|
|
Trim the date so it fits on small screens
|
|
|
|
"""
|
2017-12-15 22:56:02 +00:00
|
|
|
new_date = re.sub('seconds?', 'sec', _date)
|
2017-11-02 17:25:19 +00:00
|
|
|
new_date = re.sub('minutes?', 'min', new_date)
|
|
|
|
new_date = re.sub('hours?', 'h', new_date)
|
|
|
|
new_date = re.sub('days?', 'd', new_date)
|
|
|
|
new_date = re.sub('^an?', '1', new_date)
|
|
|
|
return new_date
|
2017-10-29 22:57:48 +00:00
|
|
|
|
|
|
|
|
2017-12-16 00:37:06 +00:00
|
|
|
def _exec_forcesell(trade: Trade) -> None:
|
|
|
|
# Check if there is there is an open order
|
|
|
|
if trade.open_order_id:
|
|
|
|
order = exchange.get_order(trade.open_order_id)
|
|
|
|
|
|
|
|
# Cancel open LIMIT_BUY orders and close trade
|
|
|
|
if order and not order['closed'] and order['type'] == 'LIMIT_BUY':
|
|
|
|
exchange.cancel_order(trade.open_order_id)
|
|
|
|
trade.close(order.get('rate') or trade.open_rate)
|
|
|
|
# TODO: sell amount which has been bought already
|
|
|
|
return
|
|
|
|
|
|
|
|
# Ignore trades with an attached LIMIT_SELL order
|
|
|
|
if order and not order['closed'] and order['type'] == 'LIMIT_SELL':
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get current rate and execute sell
|
2018-01-02 09:56:42 +00:00
|
|
|
current_rate = exchange.get_ticker(trade.pair, False)['bid']
|
2017-12-16 00:37:06 +00:00
|
|
|
from freqtrade.main import execute_sell
|
|
|
|
execute_sell(trade, current_rate)
|
|
|
|
|
|
|
|
|
2017-09-08 21:10:22 +00:00
|
|
|
def send_msg(msg: str, bot: Bot = None, parse_mode: ParseMode = ParseMode.MARKDOWN) -> None:
|
2017-09-08 13:51:00 +00:00
|
|
|
"""
|
|
|
|
Send given markdown message
|
|
|
|
:param msg: message
|
|
|
|
:param bot: alternative bot
|
|
|
|
:param parse_mode: telegram parse mode
|
|
|
|
:return: None
|
|
|
|
"""
|
2017-11-02 17:56:57 +00:00
|
|
|
if not is_enabled():
|
|
|
|
return
|
2017-11-07 21:27:16 +00:00
|
|
|
|
|
|
|
bot = bot or _UPDATER.bot
|
2017-11-17 18:47:29 +00:00
|
|
|
|
2017-12-16 00:09:07 +00:00
|
|
|
keyboard = [['/daily', '/profit', '/balance'],
|
2017-12-15 04:59:38 +00:00
|
|
|
['/status', '/status table', '/performance'],
|
|
|
|
['/count', '/start', '/stop', '/help']]
|
2017-11-24 19:54:50 +00:00
|
|
|
|
|
|
|
reply_markup = ReplyKeyboardMarkup(keyboard)
|
|
|
|
|
2017-11-02 17:56:57 +00:00
|
|
|
try:
|
2017-11-17 18:47:29 +00:00
|
|
|
try:
|
2017-12-16 02:39:47 +00:00
|
|
|
bot.send_message(
|
|
|
|
_CONF['telegram']['chat_id'], msg,
|
|
|
|
parse_mode=parse_mode, reply_markup=reply_markup
|
|
|
|
)
|
2017-11-17 18:47:29 +00:00
|
|
|
except NetworkError as network_err:
|
|
|
|
# Sometimes the telegram server resets the current connection,
|
|
|
|
# if this is the case we send the message again.
|
|
|
|
logger.warning(
|
|
|
|
'Got Telegram NetworkError: %s! Trying one more time.',
|
|
|
|
network_err.message
|
|
|
|
)
|
2017-12-16 02:39:47 +00:00
|
|
|
bot.send_message(
|
|
|
|
_CONF['telegram']['chat_id'], msg,
|
|
|
|
parse_mode=parse_mode, reply_markup=reply_markup
|
|
|
|
)
|
2017-11-17 18:47:29 +00:00
|
|
|
except TelegramError as telegram_err:
|
|
|
|
logger.warning('Got TelegramError: %s! Giving up on that message.', telegram_err.message)
|