stable/freqtrade/rpc/rpc.py

392 lines
15 KiB
Python
Raw Normal View History

2018-02-13 03:45:59 +00:00
"""
This module contains class to define a RPC communications
"""
2018-03-25 19:37:14 +00:00
import logging
from abc import abstractmethod
2018-07-04 07:31:35 +00:00
from datetime import date, datetime, timedelta
2018-03-17 21:44:47 +00:00
from decimal import Decimal
2018-07-04 07:31:35 +00:00
from typing import Any, Dict, List, Tuple
2018-03-17 21:44:47 +00:00
2018-03-02 15:22:00 +00:00
import arrow
2018-02-13 03:45:59 +00:00
import sqlalchemy as sql
from numpy import mean, nan_to_num
from pandas import DataFrame
2018-03-17 21:44:47 +00:00
from freqtrade.misc import shorten_date
2018-02-13 03:45:59 +00:00
from freqtrade.persistence import Trade
from freqtrade.state import State
2018-03-25 19:37:14 +00:00
logger = logging.getLogger(__name__)
2018-06-08 02:52:50 +00:00
class RPCException(Exception):
"""
Should be raised with a rpc-formatted message in an _rpc_* method
if the required state is wrong, i.e.:
raise RPCException('*Status:* `no active trade`')
"""
2018-06-08 02:52:50 +00:00
pass
2018-02-13 03:45:59 +00:00
class RPC(object):
"""
RPC class can be used to have extra feature, like bot data, and access to DB data
"""
def __init__(self, freqtrade) -> None:
"""
Initializes all enabled rpc modules
:param freqtrade: Instance of a freqtrade bot
:return: None
"""
2018-06-08 22:20:10 +00:00
self._freqtrade = freqtrade
2018-02-13 03:45:59 +00:00
@abstractmethod
2018-06-08 02:52:50 +00:00
def cleanup(self) -> None:
""" Cleanup pending module resources """
@property
@abstractmethod
2018-06-08 02:52:50 +00:00
def name(self) -> str:
""" Returns the lowercase name of this module """
@abstractmethod
def send_msg(self, msg: str) -> None:
""" Sends a message to all registered rpc modules """
2018-06-08 02:52:50 +00:00
def _rpc_trade_status(self) -> List[str]:
2018-02-13 03:45:59 +00:00
"""
Below follows the RPC backend it is prefixed with rpc_ to raise awareness that it is
a remotely exposed function
"""
# Fetch open trade
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
2018-06-08 22:20:10 +00:00
if self._freqtrade.state != State.RUNNING:
2018-06-08 02:52:50 +00:00
raise RPCException('*Status:* `trader is not running`')
2018-02-13 03:45:59 +00:00
elif not trades:
2018-06-08 02:52:50 +00:00
raise RPCException('*Status:* `no active trade`')
2018-02-13 03:45:59 +00:00
else:
result = []
for trade in trades:
order = None
if trade.open_order_id:
2018-06-17 19:24:36 +00:00
order = self._freqtrade.exchange.get_order(trade.open_order_id, trade.pair)
2018-02-13 03:45:59 +00:00
# calculate profit and send message to user
2018-06-17 19:24:36 +00:00
current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid']
2018-02-13 03:45:59 +00:00
current_profit = trade.calc_profit_percent(current_rate)
2018-07-04 18:53:45 +00:00
fmt_close_profit = (f'{round(trade.close_profit * 100, 2):.2f}%'
if trade.close_profit else None)
market_url = self._freqtrade.exchange.get_pair_detail_url(trade.pair)
trade_date = arrow.get(trade.open_date).humanize()
open_rate = trade.open_rate
close_rate = trade.close_rate
amount = round(trade.amount, 8)
current_profit = round(current_profit * 100, 2)
open_order = ''
2018-07-04 18:53:45 +00:00
if order:
order_type = order['type']
order_side = order['side']
order_rem = order['remaining']
open_order = f'({order_type} {order_side} rem={order_rem:.8f})'
message = f"*Trade ID:* `{trade.id}`\n" \
f"*Current Pair:* [{trade.pair}]({market_url})\n" \
f"*Open Since:* `{trade_date}`\n" \
f"*Amount:* `{amount}`\n" \
f"*Open Rate:* `{open_rate:.8f}`\n" \
f"*Close Rate:* `{close_rate}`\n" \
f"*Current Rate:* `{current_rate:.8f}`\n" \
f"*Close Profit:* `{fmt_close_profit}`\n" \
f"*Current Profit:* `{current_profit:.2f}%`\n" \
f"*Open Order:* `{open_order}`"\
2018-02-13 03:45:59 +00:00
result.append(message)
2018-06-08 02:52:50 +00:00
return result
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_status_table(self) -> DataFrame:
2018-02-13 03:45:59 +00:00
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
2018-06-08 22:20:10 +00:00
if self._freqtrade.state != State.RUNNING:
2018-06-08 02:52:50 +00:00
raise RPCException('*Status:* `trader is not running`')
2018-02-13 03:45:59 +00:00
elif not trades:
2018-06-08 02:52:50 +00:00
raise RPCException('*Status:* `no active order`')
2018-02-13 03:45:59 +00:00
else:
trades_list = []
for trade in trades:
# calculate profit and send message to user
2018-06-17 19:24:36 +00:00
current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid']
2018-07-04 18:53:45 +00:00
trade_perc = (100 * trade.calc_profit_percent(current_rate))
2018-02-13 03:45:59 +00:00
trades_list.append([
trade.id,
trade.pair,
shorten_date(arrow.get(trade.open_date).humanize(only_distance=True)),
2018-07-04 18:53:45 +00:00
f'{trade_perc:.2f}%'
2018-02-13 03:45:59 +00:00
])
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
columns = ['ID', 'Pair', 'Since', 'Profit']
df_statuses = DataFrame.from_records(trades_list, columns=columns)
df_statuses = df_statuses.set_index(columns[0])
2018-06-08 02:52:50 +00:00
return df_statuses
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_daily_profit(
self, timescale: int,
2018-06-08 02:52:50 +00:00
stake_currency: str, fiat_display_currency: str) -> List[List[Any]]:
2018-02-13 03:45:59 +00:00
today = datetime.utcnow().date()
2018-06-02 11:43:51 +00:00
profit_days: Dict[date, Dict] = {}
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
if not (isinstance(timescale, int) and timescale > 0):
2018-06-08 02:52:50 +00:00
raise RPCException('*Daily [n]:* `must be an integer greater than 0`')
2018-03-02 15:22:00 +00:00
2018-06-08 22:20:10 +00:00
fiat = self._freqtrade.fiat_converter
2018-02-13 03:45:59 +00:00
for day in range(0, timescale):
profitday = today - timedelta(days=day)
trades = Trade.query \
.filter(Trade.is_open.is_(False)) \
.filter(Trade.close_date >= profitday)\
.filter(Trade.close_date < (profitday + timedelta(days=1)))\
.order_by(Trade.close_date)\
.all()
curdayprofit = sum(trade.calc_profit() for trade in trades)
profit_days[profitday] = {
2018-07-04 18:53:45 +00:00
'amount': f'{curdayprofit:.8f}',
2018-02-13 03:45:59 +00:00
'trades': len(trades)
}
2018-03-02 15:22:00 +00:00
2018-06-08 02:52:50 +00:00
return [
2018-02-13 03:45:59 +00:00
[
key,
'{value:.8f} {symbol}'.format(
value=float(value['amount']),
symbol=stake_currency
),
'{value:.3f} {symbol}'.format(
value=fiat.convert_amount(
value['amount'],
stake_currency,
fiat_display_currency
),
symbol=fiat_display_currency
),
2018-03-02 15:22:00 +00:00
'{value} trade{s}'.format(
value=value['trades'],
s='' if value['trades'] < 2 else 's'
),
2018-02-13 03:45:59 +00:00
]
for key, value in profit_days.items()
]
2018-06-08 02:52:50 +00:00
def _rpc_trade_statistics(
self, stake_currency: str, fiat_display_currency: str) -> Dict[str, Any]:
""" Returns cumulative profit statistics """
2018-02-13 03:45:59 +00:00
trades = Trade.query.order_by(Trade.id).all()
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
profit_all_coin = []
profit_all_percent = []
profit_closed_coin = []
profit_closed_percent = []
durations = []
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
for trade in trades:
2018-06-02 11:43:51 +00:00
current_rate: float = 0.0
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
if not trade.open_rate:
continue
if trade.close_date:
durations.append((trade.close_date - trade.open_date).total_seconds())
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
if not trade.is_open:
profit_percent = trade.calc_profit_percent()
profit_closed_coin.append(trade.calc_profit())
profit_closed_percent.append(profit_percent)
else:
# Get current rate
2018-06-17 19:24:36 +00:00
current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid']
2018-02-13 03:45:59 +00:00
profit_percent = trade.calc_profit_percent(rate=current_rate)
2018-03-02 15:22:00 +00:00
profit_all_coin.append(
trade.calc_profit(rate=Decimal(trade.close_rate or current_rate))
)
2018-02-13 03:45:59 +00:00
profit_all_percent.append(profit_percent)
2018-03-02 15:22:00 +00:00
best_pair = Trade.session.query(
2018-03-17 23:27:57 +00:00
Trade.pair, sql.func.sum(Trade.close_profit).label('profit_sum')
).filter(Trade.is_open.is_(False)) \
.group_by(Trade.pair) \
2018-03-02 15:22:00 +00:00
.order_by(sql.text('profit_sum DESC')).first()
2018-02-13 03:45:59 +00:00
if not best_pair:
2018-06-08 02:52:50 +00:00
raise RPCException('*Status:* `no closed trade`')
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
bp_pair, bp_rate = best_pair
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
# FIX: we want to keep fiatconverter in a state/environment,
# doing this will utilize its caching functionallity, instead we reinitialize it here
2018-06-08 22:20:10 +00:00
fiat = self._freqtrade.fiat_converter
2018-02-13 03:45:59 +00:00
# Prepare data to display
profit_closed_coin = round(sum(profit_closed_coin), 8)
profit_closed_percent = round(nan_to_num(mean(profit_closed_percent)) * 100, 2)
2018-02-13 03:45:59 +00:00
profit_closed_fiat = fiat.convert_amount(
profit_closed_coin,
stake_currency,
fiat_display_currency
)
profit_all_coin = round(sum(profit_all_coin), 8)
profit_all_percent = round(nan_to_num(mean(profit_all_percent)) * 100, 2)
2018-02-13 03:45:59 +00:00
profit_all_fiat = fiat.convert_amount(
profit_all_coin,
stake_currency,
fiat_display_currency
)
num = float(len(durations) or 1)
2018-06-08 02:52:50 +00:00
return {
'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,
'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) / num)).split('.')[0],
'best_pair': bp_pair,
'best_rate': round(bp_rate * 100, 2),
}
def _rpc_balance(self, fiat_display_currency: str) -> Tuple[List[Dict], float, str, float]:
""" Returns current account balance per crypto """
2018-02-13 03:45:59 +00:00
output = []
total = 0.0
2018-06-17 19:24:36 +00:00
for coin, balance in self._freqtrade.exchange.get_balances().items():
2018-05-14 21:31:56 +00:00
if not balance['total']:
continue
2018-02-13 03:45:59 +00:00
if coin == 'BTC':
2018-05-14 21:31:56 +00:00
rate = 1.0
2018-02-13 03:45:59 +00:00
else:
if coin == 'USDT':
2018-06-17 19:24:36 +00:00
rate = 1.0 / self._freqtrade.exchange.get_ticker('BTC/USDT', False)['bid']
2018-02-13 03:45:59 +00:00
else:
2018-06-17 19:24:36 +00:00
rate = self._freqtrade.exchange.get_ticker(coin + '/BTC', False)['bid']
2018-05-14 21:31:56 +00:00
est_btc: float = rate * balance['total']
total = total + est_btc
2018-03-02 15:22:00 +00:00
output.append(
{
2018-05-14 21:31:56 +00:00
'currency': coin,
'available': balance['free'],
'balance': balance['total'],
'pending': balance['used'],
'est_btc': est_btc
2018-03-02 15:22:00 +00:00
}
)
2018-05-14 21:31:56 +00:00
if total == 0.0:
2018-06-08 02:52:50 +00:00
raise RPCException('`All balances are zero.`')
2018-05-14 21:31:56 +00:00
2018-06-08 22:20:10 +00:00
fiat = self._freqtrade.fiat_converter
2018-02-13 03:45:59 +00:00
symbol = fiat_display_currency
value = fiat.convert_amount(total, 'BTC', symbol)
2018-06-08 02:52:50 +00:00
return output, total, symbol, value
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_start(self) -> str:
""" Handler for start """
2018-06-08 22:20:10 +00:00
if self._freqtrade.state == State.RUNNING:
2018-06-08 02:52:50 +00:00
return '*Status:* `already running`'
2018-03-02 15:22:00 +00:00
2018-06-08 22:20:10 +00:00
self._freqtrade.state = State.RUNNING
2018-06-08 02:52:50 +00:00
return '`Starting trader ...`'
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_stop(self) -> str:
""" Handler for stop """
2018-06-08 22:20:10 +00:00
if self._freqtrade.state == State.RUNNING:
self._freqtrade.state = State.STOPPED
2018-06-08 02:52:50 +00:00
return '`Stopping trader ...`'
2018-03-02 15:22:00 +00:00
2018-06-08 02:52:50 +00:00
return '*Status:* `already stopped`'
2018-02-13 03:45:59 +00:00
def _rpc_reload_conf(self) -> str:
""" Handler for reload_conf. """
self._freqtrade.state = State.RELOAD_CONF
return '*Status:* `Reloading config ...`'
2018-02-13 03:45:59 +00:00
# FIX: no test for this!!!!
2018-06-08 02:52:50 +00:00
def _rpc_forcesell(self, trade_id) -> None:
2018-02-13 03:45:59 +00:00
"""
Handler for forcesell <id>.
Sells the given trade at current price
"""
def _exec_forcesell(trade: Trade) -> None:
2018-02-13 03:45:59 +00:00
# Check if there is there is an open order
if trade.open_order_id:
2018-06-17 19:24:36 +00:00
order = self._freqtrade.exchange.get_order(trade.open_order_id, trade.pair)
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
# Cancel open LIMIT_BUY orders and close trade
if order and order['status'] == 'open' \
and order['type'] == 'limit' \
and order['side'] == 'buy':
2018-06-17 19:24:36 +00:00
self._freqtrade.exchange.cancel_order(trade.open_order_id, trade.pair)
trade.close(order.get('price') or trade.open_rate)
# Do the best effort, if we don't know 'filled' amount, don't try selling
if order['filled'] is None:
return
trade.amount = order['filled']
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
# Ignore trades with an attached LIMIT_SELL order
if order and order['status'] == 'open' \
and order['type'] == 'limit' \
and order['side'] == 'sell':
2018-02-13 03:45:59 +00:00
return
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
# Get current rate and execute sell
2018-06-17 19:24:36 +00:00
current_rate = self._freqtrade.exchange.get_ticker(trade.pair, False)['bid']
2018-06-08 22:20:10 +00:00
self._freqtrade.execute_sell(trade, current_rate)
2018-02-13 03:45:59 +00:00
# ---- EOF def _exec_forcesell ----
2018-06-08 22:20:10 +00:00
if self._freqtrade.state != State.RUNNING:
2018-06-08 02:52:50 +00:00
raise RPCException('`trader is not running`')
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
if trade_id == 'all':
# Execute sell for all open orders
for trade in Trade.query.filter(Trade.is_open.is_(True)).all():
_exec_forcesell(trade)
2018-06-08 02:52:50 +00:00
return
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
# Query for trade
trade = Trade.query.filter(
sql.and_(
Trade.id == trade_id,
Trade.is_open.is_(True)
)
).first()
if not trade:
2018-03-25 19:37:14 +00:00
logger.warning('forcesell: Invalid argument received')
2018-06-08 02:52:50 +00:00
raise RPCException('Invalid argument.')
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
_exec_forcesell(trade)
2018-06-08 00:35:10 +00:00
Trade.session.flush()
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_performance(self) -> List[Dict]:
2018-02-13 03:45:59 +00:00
"""
Handler for performance.
Shows a performance statistic from finished trades
"""
2018-06-08 22:20:10 +00:00
if self._freqtrade.state != State.RUNNING:
2018-06-08 02:52:50 +00:00
raise RPCException('`trader is not running`')
2018-03-02 15:22:00 +00:00
2018-02-13 03:45:59 +00:00
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()
2018-06-08 02:52:50 +00:00
return [
{'pair': pair, 'profit': round(rate * 100, 2), 'count': count}
for pair, rate, count in pair_rates
]
2018-03-02 15:22:00 +00:00
2018-06-08 02:52:50 +00:00
def _rpc_count(self) -> List[Trade]:
""" Returns the number of trades running """
2018-06-08 22:20:10 +00:00
if self._freqtrade.state != State.RUNNING:
2018-06-08 02:52:50 +00:00
raise RPCException('`trader is not running`')
2018-03-02 15:22:00 +00:00
2018-06-08 02:52:50 +00:00
return Trade.query.filter(Trade.is_open.is_(True)).all()