freqtrade_origin/freqtrade/rpc/rpc.py

399 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-06-22 02:08:51 +00:00
from datetime import timedelta, datetime, date
2018-03-17 21:44:47 +00:00
from decimal import Decimal
from enum import Enum
2018-06-22 02:08:51 +00:00
from typing import Dict, Any, List
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__)
class RPCMessageType(Enum):
STATUS_NOTIFICATION = 'status'
BUY_NOTIFICATION = 'buy'
SELL_NOTIFICATION = 'sell'
def __repr__(self):
return self.value
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`')
"""
def __init__(self, message: str) -> None:
super().__init__(self)
self.message = message
def __str__(self):
return self.message
2018-06-08 02:52:50 +00:00
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
@property
def name(self) -> str:
""" Returns the lowercase name of the implementation """
return self.__class__.__name__.lower()
@abstractmethod
2018-06-08 02:52:50 +00:00
def cleanup(self) -> None:
""" Cleanup pending module resources """
@abstractmethod
2018-06-24 22:04:27 +00:00
def send_msg(self, msg: Dict[str, str]) -> None:
""" Sends a message to all registered rpc modules """
def _rpc_trade_status(self) -> List[Dict[str, Any]]:
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:
raise RPCException('trader is not running')
2018-02-13 03:45:59 +00:00
elif not trades:
raise RPCException('no active trade')
2018-02-13 03:45:59 +00:00
else:
results = []
2018-02-13 03:45:59 +00:00
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)
results.append(dict(
trade_id=trade.id,
pair=trade.pair,
market_url=self._freqtrade.exchange.get_pair_detail_url(trade.pair),
date=arrow.get(trade.open_date),
open_rate=trade.open_rate,
close_rate=trade.close_rate,
current_rate=current_rate,
amount=round(trade.amount, 8),
close_profit=fmt_close_profit,
current_profit=round(current_profit * 100, 2),
open_order='({} {} rem={:.8f})'.format(
order['type'], order['side'], order['remaining']
) if order else None,
))
return results
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:
raise RPCException('trader is not running')
2018-02-13 03:45:59 +00:00
elif not trades:
raise RPCException('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):
raise RPCException('timescale 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:
raise RPCException('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_sum = 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_sum,
2018-02-13 03:45:59 +00:00
stake_currency,
fiat_display_currency
)
profit_all_coin_sum = 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_sum,
2018-02-13 03:45:59 +00:00
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_sum,
2018-06-08 02:52:50 +00:00
'profit_closed_percent': profit_closed_percent,
'profit_closed_fiat': profit_closed_fiat,
'profit_all_coin': profit_all_coin_sum,
2018-06-08 02:52:50 +00:00
'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),
}
2018-06-22 02:08:51 +00:00
def _rpc_balance(self, fiat_display_currency: str) -> Dict:
2018-06-08 02:52:50 +00:00
""" 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-06-22 02:08:51 +00:00
output.append({
'currency': coin,
'available': balance['free'],
'balance': balance['total'],
'pending': balance['used'],
'est_btc': est_btc,
})
2018-05-14 21:31:56 +00:00
if total == 0.0:
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-22 02:08:51 +00:00
return {
'currencies': output,
'total': total,
'symbol': symbol,
'value': value,
}
2018-02-13 03:45:59 +00:00
def _rpc_start(self) -> Dict[str, str]:
2018-06-08 02:52:50 +00:00
""" Handler for start """
2018-06-08 22:20:10 +00:00
if self._freqtrade.state == State.RUNNING:
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
return {'status': 'starting trader ...'}
2018-02-13 03:45:59 +00:00
def _rpc_stop(self) -> Dict[str, str]:
2018-06-08 02:52:50 +00:00
""" Handler for stop """
2018-06-08 22:20:10 +00:00
if self._freqtrade.state == State.RUNNING:
self._freqtrade.state = State.STOPPED
return {'status': 'stopping trader ...'}
2018-03-02 15:22:00 +00:00
return {'status': 'already stopped'}
2018-02-13 03:45:59 +00:00
def _rpc_reload_conf(self) -> Dict[str, str]:
""" Handler for reload_conf. """
self._freqtrade.state = State.RELOAD_CONF
return {'status': 'reloading config ...'}
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:
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')
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:
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:
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()