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
|
2018-06-08 01:49:09 +00:00
|
|
|
from abc import abstractmethod
|
2019-11-12 12:54:26 +00:00
|
|
|
from datetime import date, datetime, timedelta
|
2018-07-03 18:26:48 +00:00
|
|
|
from enum import Enum
|
2019-11-12 13:58:41 +00:00
|
|
|
from math import isnan
|
2020-08-04 17:56:49 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, Union
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-03-02 15:22:00 +00:00
|
|
|
import arrow
|
2021-02-11 19:21:07 +00:00
|
|
|
from numpy import NAN, inf, int64, mean
|
2020-07-11 13:20:50 +00:00
|
|
|
from pandas import DataFrame
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2020-07-02 05:10:56 +00:00
|
|
|
from freqtrade.configuration.timerange import TimeRange
|
2020-10-17 18:32:23 +00:00
|
|
|
from freqtrade.constants import CANCEL_REASON, DATETIME_PRINT_FORMAT
|
2020-07-02 05:10:56 +00:00
|
|
|
from freqtrade.data.history import load_data
|
2020-08-14 13:44:36 +00:00
|
|
|
from freqtrade.exceptions import ExchangeError, PricingError
|
2020-08-04 17:43:22 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_msecs
|
2020-08-14 13:44:36 +00:00
|
|
|
from freqtrade.loggers import bufferHandler
|
2018-03-17 21:44:47 +00:00
|
|
|
from freqtrade.misc import shorten_date
|
2020-10-25 09:54:30 +00:00
|
|
|
from freqtrade.persistence import PairLocks, Trade
|
2020-12-30 08:57:31 +00:00
|
|
|
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
|
2018-12-11 19:27:30 +00:00
|
|
|
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.state import State
|
2018-07-12 20:21:52 +00:00
|
|
|
from freqtrade.strategy.interface import SellType
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2018-03-25 19:37:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
class RPCMessageType(Enum):
|
|
|
|
STATUS_NOTIFICATION = 'status'
|
2018-08-15 02:39:32 +00:00
|
|
|
WARNING_NOTIFICATION = 'warning'
|
2020-09-19 15:51:31 +00:00
|
|
|
STARTUP_NOTIFICATION = 'startup'
|
2018-07-03 18:26:48 +00:00
|
|
|
BUY_NOTIFICATION = 'buy'
|
2020-02-08 20:02:52 +00:00
|
|
|
BUY_CANCEL_NOTIFICATION = 'buy_cancel'
|
2018-07-03 18:26:48 +00:00
|
|
|
SELL_NOTIFICATION = 'sell'
|
2020-02-08 20:02:52 +00:00
|
|
|
SELL_CANCEL_NOTIFICATION = 'sell_cancel'
|
2018-07-03 18:26:48 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.value
|
|
|
|
|
2020-09-19 18:04:12 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.value
|
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
class RPCException(Exception):
|
2018-06-08 22:58:24 +00:00
|
|
|
"""
|
|
|
|
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`')
|
|
|
|
"""
|
2020-02-08 20:02:52 +00:00
|
|
|
|
2018-06-22 01:32:45 +00:00
|
|
|
def __init__(self, message: str) -> None:
|
|
|
|
super().__init__(self)
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
2019-04-04 05:13:40 +00:00
|
|
|
def __json__(self):
|
|
|
|
return {
|
|
|
|
'msg': self.message
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
class RPCHandler:
|
2018-07-21 18:44:38 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
def __init__(self, rpc: 'RPC', config: Dict[str, Any]) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2020-12-24 08:01:53 +00:00
|
|
|
Initializes RPCHandlers
|
|
|
|
:param rpc: instance of RPC Helper class
|
|
|
|
:param config: Configuration object
|
2018-02-13 03:45:59 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
2020-12-24 08:01:53 +00:00
|
|
|
self._rpc = rpc
|
|
|
|
self._config: Dict[str, Any] = config
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-23 10:28:32 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
""" Returns the lowercase name of the implementation """
|
|
|
|
return self.__class__.__name__.lower()
|
|
|
|
|
2018-06-08 01:49:09 +00:00
|
|
|
@abstractmethod
|
2018-06-08 02:52:50 +00:00
|
|
|
def cleanup(self) -> None:
|
2018-06-08 01:49:09 +00:00
|
|
|
""" Cleanup pending module resources """
|
|
|
|
|
|
|
|
@abstractmethod
|
2018-06-24 22:04:27 +00:00
|
|
|
def send_msg(self, msg: Dict[str, str]) -> None:
|
2018-06-08 01:49:09 +00:00
|
|
|
""" Sends a message to all registered rpc modules """
|
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
|
|
|
|
class RPC:
|
|
|
|
"""
|
|
|
|
RPC class can be used to have extra feature, like bot data, and access to DB data
|
|
|
|
"""
|
|
|
|
# Bind _fiat_converter if needed
|
|
|
|
_fiat_converter: Optional[CryptoToFiatConverter] = None
|
|
|
|
|
|
|
|
def __init__(self, freqtrade) -> None:
|
|
|
|
"""
|
|
|
|
Initializes all enabled rpc modules
|
|
|
|
:param freqtrade: Instance of a freqtrade bot
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self._freqtrade = freqtrade
|
|
|
|
self._config: Dict[str, Any] = freqtrade.config
|
|
|
|
if self._config.get('fiat_display_currency', None):
|
|
|
|
self._fiat_converter = CryptoToFiatConverter()
|
|
|
|
|
2020-11-08 10:26:02 +00:00
|
|
|
@staticmethod
|
2021-01-02 14:48:33 +00:00
|
|
|
def _rpc_show_config(config, botstate: Union[State, str]) -> Dict[str, Any]:
|
2019-11-17 13:56:08 +00:00
|
|
|
"""
|
|
|
|
Return a dict of config options.
|
|
|
|
Explicitly does NOT return the full config to avoid leakage of sensitive
|
|
|
|
information via rpc.
|
|
|
|
"""
|
|
|
|
val = {
|
2020-01-20 19:24:40 +00:00
|
|
|
'dry_run': config['dry_run'],
|
2019-11-17 13:56:08 +00:00
|
|
|
'stake_currency': config['stake_currency'],
|
|
|
|
'stake_amount': config['stake_amount'],
|
2021-01-15 19:47:12 +00:00
|
|
|
'max_open_trades': (config['max_open_trades']
|
|
|
|
if config['max_open_trades'] != float('inf') else -1),
|
2020-11-08 10:26:02 +00:00
|
|
|
'minimal_roi': config['minimal_roi'].copy() if 'minimal_roi' in config else {},
|
|
|
|
'stoploss': config.get('stoploss'),
|
|
|
|
'trailing_stop': config.get('trailing_stop'),
|
2019-11-17 14:12:53 +00:00
|
|
|
'trailing_stop_positive': config.get('trailing_stop_positive'),
|
|
|
|
'trailing_stop_positive_offset': config.get('trailing_stop_positive_offset'),
|
|
|
|
'trailing_only_offset_is_reached': config.get('trailing_only_offset_is_reached'),
|
2021-01-30 19:11:18 +00:00
|
|
|
'use_custom_stoploss': config.get('use_custom_stoploss'),
|
2021-01-16 15:19:49 +00:00
|
|
|
'bot_name': config.get('bot_name', 'freqtrade'),
|
2020-11-08 10:26:02 +00:00
|
|
|
'timeframe': config.get('timeframe'),
|
|
|
|
'timeframe_ms': timeframe_to_msecs(config['timeframe']
|
|
|
|
) if 'timeframe' in config else '',
|
|
|
|
'timeframe_min': timeframe_to_minutes(config['timeframe']
|
|
|
|
) if 'timeframe' in config else '',
|
2019-11-17 13:56:08 +00:00
|
|
|
'exchange': config['exchange']['name'],
|
|
|
|
'strategy': config['strategy'],
|
2020-05-12 11:39:24 +00:00
|
|
|
'forcebuy_enabled': config.get('forcebuy_enable', False),
|
2020-06-05 18:31:40 +00:00
|
|
|
'ask_strategy': config.get('ask_strategy', {}),
|
|
|
|
'bid_strategy': config.get('bid_strategy', {}),
|
2020-11-08 10:26:02 +00:00
|
|
|
'state': str(botstate),
|
|
|
|
'runmode': config['runmode'].value
|
2019-11-17 13:56:08 +00:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
|
2021-01-18 14:26:53 +00:00
|
|
|
def _rpc_trade_status(self, trade_ids: List[int] = []) -> 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
|
|
|
|
"""
|
2021-01-17 19:39:35 +00:00
|
|
|
# Fetch open trades
|
|
|
|
if trade_ids:
|
2021-01-17 20:21:31 +00:00
|
|
|
trades = Trade.get_trades(trade_filter=Trade.id.in_(trade_ids)).all()
|
2021-01-17 19:39:35 +00:00
|
|
|
else:
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
|
2018-10-10 18:58:21 +00:00
|
|
|
if not trades:
|
2018-06-22 01:37:19 +00:00
|
|
|
raise RPCException('no active trade')
|
2018-02-13 03:45:59 +00:00
|
|
|
else:
|
2018-06-22 01:54:10 +00:00
|
|
|
results = []
|
2018-02-13 03:45:59 +00:00
|
|
|
for trade in trades:
|
|
|
|
order = None
|
|
|
|
if trade.open_order_id:
|
2020-06-28 14:27:35 +00:00
|
|
|
order = self._freqtrade.exchange.fetch_order(trade.open_order_id, trade.pair)
|
2018-02-13 03:45:59 +00:00
|
|
|
# calculate profit and send message to user
|
2018-10-10 19:28:48 +00:00
|
|
|
try:
|
2019-03-16 12:24:10 +00:00
|
|
|
current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
|
2020-06-28 14:01:40 +00:00
|
|
|
except (ExchangeError, PricingError):
|
2018-10-10 19:28:48 +00:00
|
|
|
current_rate = NAN
|
2019-12-17 07:53:30 +00:00
|
|
|
current_profit = trade.calc_profit_ratio(current_rate)
|
2020-06-04 04:56:59 +00:00
|
|
|
current_profit_abs = trade.calc_profit(current_rate)
|
|
|
|
# Calculate guaranteed profit (in case of trailing stop)
|
2020-06-04 05:04:32 +00:00
|
|
|
stoploss_entry_dist = trade.calc_profit(trade.stop_loss)
|
|
|
|
stoploss_entry_dist_ratio = trade.calc_profit_ratio(trade.stop_loss)
|
2020-06-04 04:56:59 +00:00
|
|
|
# calculate distance to stoploss
|
|
|
|
stoploss_current_dist = trade.stop_loss - current_rate
|
|
|
|
stoploss_current_dist_ratio = stoploss_current_dist / current_rate
|
|
|
|
|
2019-05-05 12:07:08 +00:00
|
|
|
trade_dict = trade.to_json()
|
|
|
|
trade_dict.update(dict(
|
2019-04-03 14:28:44 +00:00
|
|
|
base_currency=self._freqtrade.config['stake_currency'],
|
2020-05-27 17:15:56 +00:00
|
|
|
close_profit=trade.close_profit if trade.close_profit is not None else None,
|
2019-05-05 12:07:08 +00:00
|
|
|
current_rate=current_rate,
|
2020-11-03 06:34:21 +00:00
|
|
|
current_profit=current_profit, # Deprectated
|
|
|
|
current_profit_pct=round(current_profit * 100, 2), # Deprectated
|
|
|
|
current_profit_abs=current_profit_abs, # Deprectated
|
|
|
|
profit_ratio=current_profit,
|
|
|
|
profit_pct=round(current_profit * 100, 2),
|
|
|
|
profit_abs=current_profit_abs,
|
|
|
|
|
2020-06-04 04:56:59 +00:00
|
|
|
stoploss_current_dist=stoploss_current_dist,
|
|
|
|
stoploss_current_dist_ratio=round(stoploss_current_dist_ratio, 8),
|
2020-07-14 18:16:18 +00:00
|
|
|
stoploss_current_dist_pct=round(stoploss_current_dist_ratio * 100, 2),
|
2020-06-04 05:04:32 +00:00
|
|
|
stoploss_entry_dist=stoploss_entry_dist,
|
|
|
|
stoploss_entry_dist_ratio=round(stoploss_entry_dist_ratio, 8),
|
2018-06-22 01:54:10 +00:00
|
|
|
open_order='({} {} rem={:.8f})'.format(
|
2019-04-03 18:28:03 +00:00
|
|
|
order['type'], order['side'], order['remaining']
|
2018-06-22 01:54:10 +00:00
|
|
|
) if order else None,
|
|
|
|
))
|
2019-05-05 12:07:08 +00:00
|
|
|
results.append(trade_dict)
|
2018-06-22 01:54:10 +00:00
|
|
|
return results
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def _rpc_status_table(self, stake_currency: str,
|
|
|
|
fiat_display_currency: str) -> Tuple[List, List]:
|
2019-02-25 19:00:17 +00:00
|
|
|
trades = Trade.get_open_trades()
|
2018-10-10 18:58:21 +00:00
|
|
|
if not trades:
|
2019-12-29 18:51:47 +00:00
|
|
|
raise RPCException('no active trade')
|
2018-02-13 03:45:59 +00:00
|
|
|
else:
|
|
|
|
trades_list = []
|
|
|
|
for trade in trades:
|
|
|
|
# calculate profit and send message to user
|
2018-10-10 19:28:48 +00:00
|
|
|
try:
|
2019-03-16 12:24:10 +00:00
|
|
|
current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
|
2020-06-28 14:01:40 +00:00
|
|
|
except (PricingError, ExchangeError):
|
2018-10-10 19:28:48 +00:00
|
|
|
current_rate = NAN
|
2020-02-28 09:36:39 +00:00
|
|
|
trade_percent = (100 * trade.calc_profit_ratio(current_rate))
|
2019-11-12 13:58:41 +00:00
|
|
|
trade_profit = trade.calc_profit(current_rate)
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_str = f'{trade_percent:.2f}%'
|
2019-11-12 13:58:41 +00:00
|
|
|
if self._fiat_converter:
|
|
|
|
fiat_profit = self._fiat_converter.convert_amount(
|
2020-02-08 20:02:52 +00:00
|
|
|
trade_profit,
|
|
|
|
stake_currency,
|
|
|
|
fiat_display_currency
|
|
|
|
)
|
2019-11-12 13:58:41 +00:00
|
|
|
if fiat_profit and not isnan(fiat_profit):
|
|
|
|
profit_str += f" ({fiat_profit:.2f})"
|
2018-02-13 03:45:59 +00:00
|
|
|
trades_list.append([
|
|
|
|
trade.id,
|
2020-02-15 20:01:45 +00:00
|
|
|
trade.pair + ('*' if (trade.open_order_id is not None
|
|
|
|
and trade.close_rate_requested is None) else '')
|
|
|
|
+ ('**' if (trade.close_rate_requested is not None) else ''),
|
2018-02-13 03:45:59 +00:00
|
|
|
shorten_date(arrow.get(trade.open_date).humanize(only_distance=True)),
|
2019-11-12 13:58:41 +00:00
|
|
|
profit_str
|
2018-02-13 03:45:59 +00:00
|
|
|
])
|
2019-11-12 13:58:41 +00:00
|
|
|
profitcol = "Profit"
|
|
|
|
if self._fiat_converter:
|
|
|
|
profitcol += " (" + fiat_display_currency + ")"
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2019-11-12 13:58:41 +00:00
|
|
|
columns = ['ID', 'Pair', 'Since', profitcol]
|
2019-11-12 12:54:26 +00:00
|
|
|
return trades_list, columns
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
def _rpc_daily_profit(
|
2018-03-17 22:30:31 +00:00
|
|
|
self, timescale: int,
|
2020-05-17 18:18:35 +00:00
|
|
|
stake_currency: str, fiat_display_currency: str) -> Dict[str, 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-22 01:37:19 +00:00
|
|
|
raise RPCException('timescale must be an integer greater than 0')
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
for day in range(0, timescale):
|
|
|
|
profitday = today - timedelta(days=day)
|
2019-10-29 14:09:01 +00:00
|
|
|
trades = Trade.get_trades(trade_filter=[
|
|
|
|
Trade.is_open.is_(False),
|
|
|
|
Trade.close_date >= profitday,
|
|
|
|
Trade.close_date < (profitday + timedelta(days=1))
|
|
|
|
]).order_by(Trade.close_date).all()
|
2020-09-03 17:29:48 +00:00
|
|
|
curdayprofit = sum(
|
|
|
|
trade.close_profit_abs for trade in trades if trade.close_profit_abs is not None)
|
2018-02-13 03:45:59 +00:00
|
|
|
profit_days[profitday] = {
|
2020-08-18 18:12:14 +00:00
|
|
|
'amount': curdayprofit,
|
2018-02-13 03:45:59 +00:00
|
|
|
'trades': len(trades)
|
|
|
|
}
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2020-05-17 18:12:01 +00:00
|
|
|
data = [
|
|
|
|
{
|
|
|
|
'date': key,
|
2020-08-18 18:12:14 +00:00
|
|
|
'abs_profit': value["amount"],
|
|
|
|
'fiat_value': self._fiat_converter.convert_amount(
|
2018-02-13 03:45:59 +00:00
|
|
|
value['amount'],
|
|
|
|
stake_currency,
|
|
|
|
fiat_display_currency
|
2018-07-21 18:44:38 +00:00
|
|
|
) if self._fiat_converter else 0,
|
2020-08-18 18:12:14 +00:00
|
|
|
'trade_count': value["trades"],
|
2020-05-17 18:12:01 +00:00
|
|
|
}
|
2018-02-13 03:45:59 +00:00
|
|
|
for key, value in profit_days.items()
|
|
|
|
]
|
2020-05-17 18:12:01 +00:00
|
|
|
return {
|
|
|
|
'stake_currency': stake_currency,
|
|
|
|
'fiat_display_currency': fiat_display_currency,
|
|
|
|
'data': data
|
|
|
|
}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-04-08 05:56:21 +00:00
|
|
|
def _rpc_trade_history(self, limit: int) -> Dict:
|
2020-04-05 14:14:02 +00:00
|
|
|
""" Returns the X last trades """
|
2020-04-06 09:00:31 +00:00
|
|
|
if limit > 0:
|
2020-07-23 05:50:45 +00:00
|
|
|
trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by(
|
|
|
|
Trade.id.desc()).limit(limit)
|
2020-04-05 14:14:02 +00:00
|
|
|
else:
|
2020-07-23 05:50:45 +00:00
|
|
|
trades = Trade.get_trades([Trade.is_open.is_(False)]).order_by(Trade.id.desc()).all()
|
2020-04-05 14:14:02 +00:00
|
|
|
|
2020-04-08 05:56:21 +00:00
|
|
|
output = [trade.to_json() for trade in trades]
|
2020-04-05 14:14:02 +00:00
|
|
|
|
|
|
|
return {
|
2020-04-06 09:00:31 +00:00
|
|
|
"trades": output,
|
|
|
|
"trades_count": len(output)
|
2020-04-05 14:14:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 13:54:39 +00:00
|
|
|
def _rpc_stats(self) -> Dict[str, Any]:
|
2020-12-05 13:38:42 +00:00
|
|
|
"""
|
|
|
|
Generate generic stats for trades in database
|
|
|
|
"""
|
|
|
|
def trade_win_loss(trade):
|
2020-12-05 13:48:56 +00:00
|
|
|
if trade.close_profit > 0:
|
2020-12-07 13:54:39 +00:00
|
|
|
return 'wins'
|
2020-12-05 13:48:56 +00:00
|
|
|
elif trade.close_profit < 0:
|
2020-12-07 13:54:39 +00:00
|
|
|
return 'losses'
|
2020-12-05 13:38:42 +00:00
|
|
|
else:
|
2020-12-07 13:54:39 +00:00
|
|
|
return 'draws'
|
2020-12-05 13:06:46 +00:00
|
|
|
trades = trades = Trade.get_trades([Trade.is_open.is_(False)])
|
2020-12-05 13:38:42 +00:00
|
|
|
# Sell reason
|
|
|
|
sell_reasons = {}
|
|
|
|
for trade in trades:
|
|
|
|
if trade.sell_reason not in sell_reasons:
|
2020-12-07 13:54:39 +00:00
|
|
|
sell_reasons[trade.sell_reason] = {'wins': 0, 'losses': 0, 'draws': 0}
|
2020-12-05 13:38:42 +00:00
|
|
|
sell_reasons[trade.sell_reason][trade_win_loss(trade)] += 1
|
|
|
|
|
|
|
|
# Duration
|
2020-12-07 13:54:39 +00:00
|
|
|
dur: Dict[str, List[int]] = {'wins': [], 'draws': [], 'losses': []}
|
2020-12-05 13:38:42 +00:00
|
|
|
for trade in trades:
|
|
|
|
if trade.close_date is not None and trade.open_date is not None:
|
|
|
|
trade_dur = (trade.close_date - trade.open_date).total_seconds()
|
|
|
|
dur[trade_win_loss(trade)].append(trade_dur)
|
|
|
|
|
2020-12-07 13:54:39 +00:00
|
|
|
wins_dur = sum(dur['wins']) / len(dur['wins']) if len(dur['wins']) > 0 else 'N/A'
|
|
|
|
draws_dur = sum(dur['draws']) / len(dur['draws']) if len(dur['draws']) > 0 else 'N/A'
|
|
|
|
losses_dur = sum(dur['losses']) / len(dur['losses']) if len(dur['losses']) > 0 else 'N/A'
|
2020-12-05 13:38:42 +00:00
|
|
|
|
|
|
|
durations = {'wins': wins_dur, 'draws': draws_dur, 'losses': losses_dur}
|
2020-12-07 13:54:39 +00:00
|
|
|
return {'sell_reasons': sell_reasons, 'durations': durations}
|
2020-12-05 13:06:46 +00:00
|
|
|
|
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 """
|
2019-10-29 14:09:01 +00:00
|
|
|
trades = Trade.get_trades().order_by(Trade.id).all()
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
profit_all_coin = []
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_all_ratio = []
|
2018-02-13 03:45:59 +00:00
|
|
|
profit_closed_coin = []
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_closed_ratio = []
|
2018-02-13 03:45:59 +00:00
|
|
|
durations = []
|
2020-06-24 04:43:19 +00:00
|
|
|
winning_trades = 0
|
|
|
|
losing_trades = 0
|
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:
|
2020-03-22 10:28:18 +00:00
|
|
|
profit_ratio = trade.close_profit
|
|
|
|
profit_closed_coin.append(trade.close_profit_abs)
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_closed_ratio.append(profit_ratio)
|
2020-06-25 17:22:50 +00:00
|
|
|
if trade.close_profit >= 0:
|
2020-06-24 04:43:19 +00:00
|
|
|
winning_trades += 1
|
|
|
|
else:
|
|
|
|
losing_trades += 1
|
2018-02-13 03:45:59 +00:00
|
|
|
else:
|
|
|
|
# Get current rate
|
2018-10-10 19:28:48 +00:00
|
|
|
try:
|
2019-03-16 12:24:10 +00:00
|
|
|
current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
|
2020-06-28 14:01:40 +00:00
|
|
|
except (PricingError, ExchangeError):
|
2018-10-10 19:28:48 +00:00
|
|
|
current_rate = NAN
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_ratio = trade.calc_profit_ratio(rate=current_rate)
|
2018-03-02 15:22:00 +00:00
|
|
|
|
|
|
|
profit_all_coin.append(
|
2019-11-12 14:11:31 +00:00
|
|
|
trade.calc_profit(rate=trade.close_rate or current_rate)
|
2018-03-02 15:22:00 +00:00
|
|
|
)
|
2020-02-28 09:36:39 +00:00
|
|
|
profit_all_ratio.append(profit_ratio)
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2019-10-29 10:15:33 +00:00
|
|
|
best_pair = Trade.get_best_pair()
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
# Prepare data to display
|
2018-07-14 05:02:39 +00:00
|
|
|
profit_closed_coin_sum = round(sum(profit_closed_coin), 8)
|
2021-01-31 10:21:23 +00:00
|
|
|
profit_closed_ratio_mean = float(mean(profit_closed_ratio) if profit_closed_ratio else 0.0)
|
2020-06-03 17:40:30 +00:00
|
|
|
profit_closed_ratio_sum = sum(profit_closed_ratio) if profit_closed_ratio else 0.0
|
|
|
|
|
2018-07-21 18:44:38 +00:00
|
|
|
profit_closed_fiat = self._fiat_converter.convert_amount(
|
2018-07-14 05:02:39 +00:00
|
|
|
profit_closed_coin_sum,
|
2018-02-13 03:45:59 +00:00
|
|
|
stake_currency,
|
|
|
|
fiat_display_currency
|
2018-07-21 18:44:38 +00:00
|
|
|
) if self._fiat_converter else 0
|
|
|
|
|
2018-07-14 05:02:39 +00:00
|
|
|
profit_all_coin_sum = round(sum(profit_all_coin), 8)
|
2021-01-31 10:21:23 +00:00
|
|
|
profit_all_ratio_mean = float(mean(profit_all_ratio) if profit_all_ratio else 0.0)
|
2020-06-03 17:40:30 +00:00
|
|
|
profit_all_ratio_sum = sum(profit_all_ratio) if profit_all_ratio else 0.0
|
2018-07-21 18:44:38 +00:00
|
|
|
profit_all_fiat = self._fiat_converter.convert_amount(
|
2018-07-14 05:02:39 +00:00
|
|
|
profit_all_coin_sum,
|
2018-02-13 03:45:59 +00:00
|
|
|
stake_currency,
|
|
|
|
fiat_display_currency
|
2018-07-21 18:44:38 +00:00
|
|
|
) if self._fiat_converter else 0
|
|
|
|
|
2020-05-29 07:03:48 +00:00
|
|
|
first_date = trades[0].open_date if trades else None
|
|
|
|
last_date = trades[-1].open_date if trades else None
|
2018-02-13 03:45:59 +00:00
|
|
|
num = float(len(durations) or 1)
|
2018-06-08 02:52:50 +00:00
|
|
|
return {
|
2018-07-14 05:02:39 +00:00
|
|
|
'profit_closed_coin': profit_closed_coin_sum,
|
2020-06-03 17:40:30 +00:00
|
|
|
'profit_closed_percent': round(profit_closed_ratio_mean * 100, 2), # DEPRECATED
|
|
|
|
'profit_closed_percent_mean': round(profit_closed_ratio_mean * 100, 2),
|
|
|
|
'profit_closed_ratio_mean': profit_closed_ratio_mean,
|
|
|
|
'profit_closed_percent_sum': round(profit_closed_ratio_sum * 100, 2),
|
|
|
|
'profit_closed_ratio_sum': profit_closed_ratio_sum,
|
2018-06-08 02:52:50 +00:00
|
|
|
'profit_closed_fiat': profit_closed_fiat,
|
2018-07-14 05:02:39 +00:00
|
|
|
'profit_all_coin': profit_all_coin_sum,
|
2020-06-03 17:40:30 +00:00
|
|
|
'profit_all_percent': round(profit_all_ratio_mean * 100, 2), # DEPRECATED
|
|
|
|
'profit_all_percent_mean': round(profit_all_ratio_mean * 100, 2),
|
|
|
|
'profit_all_ratio_mean': profit_all_ratio_mean,
|
|
|
|
'profit_all_percent_sum': round(profit_all_ratio_sum * 100, 2),
|
|
|
|
'profit_all_ratio_sum': profit_all_ratio_sum,
|
2018-06-08 02:52:50 +00:00
|
|
|
'profit_all_fiat': profit_all_fiat,
|
|
|
|
'trade_count': len(trades),
|
2020-05-29 07:38:12 +00:00
|
|
|
'closed_trade_count': len([t for t in trades if not t.is_open]),
|
2020-05-29 07:03:48 +00:00
|
|
|
'first_trade_date': arrow.get(first_date).humanize() if first_date else '',
|
|
|
|
'first_trade_timestamp': int(first_date.timestamp() * 1000) if first_date else 0,
|
|
|
|
'latest_trade_date': arrow.get(last_date).humanize() if last_date else '',
|
|
|
|
'latest_trade_timestamp': int(last_date.timestamp() * 1000) if last_date else 0,
|
2018-06-08 02:52:50 +00:00
|
|
|
'avg_duration': str(timedelta(seconds=sum(durations) / num)).split('.')[0],
|
2020-05-29 07:03:48 +00:00
|
|
|
'best_pair': best_pair[0] if best_pair else '',
|
|
|
|
'best_rate': round(best_pair[1] * 100, 2) if best_pair else 0,
|
2020-06-24 04:43:19 +00:00
|
|
|
'winning_trades': winning_trades,
|
|
|
|
'losing_trades': losing_trades,
|
2018-06-08 02:52:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 05:33:07 +00:00
|
|
|
def _rpc_balance(self, stake_currency: str, 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
|
2019-11-14 19:12:41 +00:00
|
|
|
try:
|
|
|
|
tickers = self._freqtrade.exchange.get_tickers()
|
2020-06-28 14:01:40 +00:00
|
|
|
except (ExchangeError):
|
2019-11-14 19:12:41 +00:00
|
|
|
raise RPCException('Error getting current tickers.')
|
|
|
|
|
2020-01-15 05:43:41 +00:00
|
|
|
self._freqtrade.wallets.update(require_update=False)
|
|
|
|
|
2019-11-24 18:41:51 +00:00
|
|
|
for coin, balance in self._freqtrade.wallets.get_all_balances().items():
|
|
|
|
if not balance.total:
|
2018-05-14 21:31:56 +00:00
|
|
|
continue
|
|
|
|
|
2019-11-15 05:33:07 +00:00
|
|
|
est_stake: float = 0
|
|
|
|
if coin == stake_currency:
|
2018-05-14 21:31:56 +00:00
|
|
|
rate = 1.0
|
2019-11-24 18:41:51 +00:00
|
|
|
est_stake = balance.total
|
2018-02-13 03:45:59 +00:00
|
|
|
else:
|
2018-08-08 19:55:48 +00:00
|
|
|
try:
|
2019-11-15 05:33:07 +00:00
|
|
|
pair = self._freqtrade.exchange.get_valid_pair_combination(coin, stake_currency)
|
|
|
|
rate = tickers.get(pair, {}).get('bid', None)
|
|
|
|
if rate:
|
2021-02-04 18:58:44 +00:00
|
|
|
if pair.startswith(stake_currency) and not pair.endswith(stake_currency):
|
2019-11-15 05:33:07 +00:00
|
|
|
rate = 1.0 / rate
|
2019-11-24 18:41:51 +00:00
|
|
|
est_stake = rate * balance.total
|
2020-06-28 14:01:40 +00:00
|
|
|
except (ExchangeError):
|
2019-05-11 13:27:09 +00:00
|
|
|
logger.warning(f" Could not get rate for pair {coin}.")
|
2018-08-08 19:55:48 +00:00
|
|
|
continue
|
2019-11-15 05:33:07 +00:00
|
|
|
total = total + (est_stake or 0)
|
2018-06-22 02:08:51 +00:00
|
|
|
output.append({
|
|
|
|
'currency': coin,
|
2019-11-24 18:41:51 +00:00
|
|
|
'free': balance.free if balance.free is not None else 0,
|
|
|
|
'balance': balance.total if balance.total is not None else 0,
|
|
|
|
'used': balance.used if balance.used is not None else 0,
|
2019-11-15 05:33:07 +00:00
|
|
|
'est_stake': est_stake or 0,
|
|
|
|
'stake': stake_currency,
|
2018-06-22 02:08:51 +00:00
|
|
|
})
|
2018-05-14 21:31:56 +00:00
|
|
|
if total == 0.0:
|
2020-01-20 19:24:40 +00:00
|
|
|
if self._freqtrade.config['dry_run']:
|
2019-06-27 05:06:11 +00:00
|
|
|
raise RPCException('Running in Dry Run, balances are not available.')
|
|
|
|
else:
|
|
|
|
raise RPCException('All balances are zero.')
|
2018-05-14 21:31:56 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
symbol = fiat_display_currency
|
2019-12-24 05:58:30 +00:00
|
|
|
value = self._fiat_converter.convert_amount(total, stake_currency,
|
2018-07-21 18:44:38 +00:00
|
|
|
symbol) if self._fiat_converter else 0
|
2018-06-22 02:08:51 +00:00
|
|
|
return {
|
|
|
|
'currencies': output,
|
|
|
|
'total': total,
|
|
|
|
'symbol': symbol,
|
|
|
|
'value': value,
|
2019-12-24 05:58:30 +00:00
|
|
|
'stake': stake_currency,
|
2020-01-20 19:24:40 +00:00
|
|
|
'note': 'Simulated balances' if self._freqtrade.config['dry_run'] else ''
|
2018-06-22 02:08:51 +00:00
|
|
|
}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-22 01:32:45 +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:
|
2018-06-22 01:32:45 +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-22 01:32:45 +00:00
|
|
|
return {'status': 'starting trader ...'}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-22 01:32:45 +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
|
2018-06-22 01:32:45 +00:00
|
|
|
return {'status': 'stopping trader ...'}
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2018-06-22 01:32:45 +00:00
|
|
|
return {'status': 'already stopped'}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-06-09 21:03:55 +00:00
|
|
|
def _rpc_reload_config(self) -> Dict[str, str]:
|
|
|
|
""" Handler for reload_config. """
|
|
|
|
self._freqtrade.state = State.RELOAD_CONFIG
|
2020-09-05 14:44:23 +00:00
|
|
|
return {'status': 'Reloading config ...'}
|
2018-06-09 02:29:48 +00:00
|
|
|
|
2019-03-17 18:35:25 +00:00
|
|
|
def _rpc_stopbuy(self) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Handler to stop buying, but handle open trades gracefully.
|
|
|
|
"""
|
|
|
|
if self._freqtrade.state == State.RUNNING:
|
|
|
|
# Set 'max_open_trades' to 0
|
|
|
|
self._freqtrade.config['max_open_trades'] = 0
|
|
|
|
|
2020-06-09 21:03:55 +00:00
|
|
|
return {'status': 'No more buy will occur from now. Run /reload_config to reset.'}
|
2019-03-17 18:35:25 +00:00
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def _rpc_forcesell(self, trade_id: str) -> Dict[str, str]:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Handler for forcesell <id>.
|
|
|
|
Sells the given trade at current price
|
|
|
|
"""
|
2018-03-17 22:30:31 +00:00
|
|
|
def _exec_forcesell(trade: Trade) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
# Check if there is there is an open order
|
2020-08-26 19:27:09 +00:00
|
|
|
fully_canceled = False
|
2018-02-13 03:45:59 +00:00
|
|
|
if trade.open_order_id:
|
2020-06-28 14:27:35 +00:00
|
|
|
order = self._freqtrade.exchange.fetch_order(trade.open_order_id, trade.pair)
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2020-08-26 19:27:09 +00:00
|
|
|
if order['side'] == 'buy':
|
|
|
|
fully_canceled = self._freqtrade.handle_cancel_buy(
|
|
|
|
trade, order, CANCEL_REASON['FORCE_SELL'])
|
|
|
|
|
|
|
|
if order['side'] == 'sell':
|
|
|
|
# Cancel order - so it is placed anew with a fresh price.
|
|
|
|
self._freqtrade.handle_cancel_sell(trade, order, CANCEL_REASON['FORCE_SELL'])
|
|
|
|
|
|
|
|
if not fully_canceled:
|
|
|
|
# Get current rate and execute sell
|
|
|
|
current_rate = self._freqtrade.get_sell_rate(trade.pair, False)
|
|
|
|
self._freqtrade.execute_sell(trade, current_rate, SellType.FORCE_SELL)
|
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-22 01:37:19 +00:00
|
|
|
raise RPCException('trader is not running')
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2020-01-22 19:50:09 +00:00
|
|
|
with self._freqtrade._sell_lock:
|
|
|
|
if trade_id == 'all':
|
|
|
|
# Execute sell for all open orders
|
|
|
|
for trade in Trade.get_open_trades():
|
|
|
|
_exec_forcesell(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
self._freqtrade.wallets.update()
|
|
|
|
return {'result': 'Created sell orders for all open trades.'}
|
|
|
|
|
|
|
|
# Query for trade
|
|
|
|
trade = Trade.get_trades(
|
|
|
|
trade_filter=[Trade.id == trade_id, Trade.is_open.is_(True), ]
|
|
|
|
).first()
|
|
|
|
if not trade:
|
|
|
|
logger.warning('forcesell: Invalid argument received')
|
|
|
|
raise RPCException('invalid argument')
|
|
|
|
|
|
|
|
_exec_forcesell(trade)
|
2018-10-09 19:08:56 +00:00
|
|
|
Trade.session.flush()
|
2020-01-22 18:54:55 +00:00
|
|
|
self._freqtrade.wallets.update()
|
2020-01-22 19:50:09 +00:00
|
|
|
return {'result': f'Created sell order for trade {trade_id}.'}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-10-09 17:25:43 +00:00
|
|
|
def _rpc_forcebuy(self, pair: str, price: Optional[float]) -> Optional[Trade]:
|
|
|
|
"""
|
|
|
|
Handler for forcebuy <asset> <price>
|
|
|
|
Buys a pair trade at the given or current price
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not self._freqtrade.config.get('forcebuy_enable', False):
|
|
|
|
raise RPCException('Forcebuy not enabled.')
|
|
|
|
|
|
|
|
if self._freqtrade.state != State.RUNNING:
|
|
|
|
raise RPCException('trader is not running')
|
|
|
|
|
2020-02-26 06:00:08 +00:00
|
|
|
# Check if pair quote currency equals to the stake currency.
|
2018-10-09 17:25:43 +00:00
|
|
|
stake_currency = self._freqtrade.config.get('stake_currency')
|
2020-02-25 06:16:37 +00:00
|
|
|
if not self._freqtrade.exchange.get_pair_quote_currency(pair) == stake_currency:
|
2018-10-09 17:25:43 +00:00
|
|
|
raise RPCException(
|
2020-11-21 09:39:49 +00:00
|
|
|
f'Wrong pair selected. Only pairs with stake-currency {stake_currency} allowed.')
|
2018-10-09 17:25:43 +00:00
|
|
|
# check if valid pair
|
|
|
|
|
|
|
|
# check if pair already has an open pair
|
2020-07-19 14:30:55 +00:00
|
|
|
trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first()
|
2018-10-09 17:25:43 +00:00
|
|
|
if trade:
|
|
|
|
raise RPCException(f'position for {pair} already open - id: {trade.id}')
|
|
|
|
|
|
|
|
# gen stake amount
|
2021-02-03 19:00:33 +00:00
|
|
|
stakeamount = self._freqtrade.wallets.get_trade_stake_amount(
|
|
|
|
pair, self._freqtrade.get_free_open_trades())
|
2018-10-09 17:25:43 +00:00
|
|
|
|
|
|
|
# execute buy
|
|
|
|
if self._freqtrade.execute_buy(pair, stakeamount, price):
|
2020-07-19 14:30:55 +00:00
|
|
|
trade = Trade.get_trades([Trade.is_open.is_(True), Trade.pair == pair]).first()
|
2018-10-09 17:25:43 +00:00
|
|
|
return trade
|
|
|
|
else:
|
|
|
|
return None
|
2020-08-04 12:41:22 +00:00
|
|
|
|
2020-12-01 18:55:20 +00:00
|
|
|
def _rpc_delete(self, trade_id: int) -> Dict[str, Union[str, int]]:
|
2020-07-20 04:08:18 +00:00
|
|
|
"""
|
|
|
|
Handler for delete <id>.
|
2020-08-04 17:43:22 +00:00
|
|
|
Delete the given trade and close eventually existing open orders.
|
2020-07-20 04:08:18 +00:00
|
|
|
"""
|
|
|
|
with self._freqtrade._sell_lock:
|
2020-08-04 17:43:22 +00:00
|
|
|
c_count = 0
|
|
|
|
trade = Trade.get_trades(trade_filter=[Trade.id == trade_id]).first()
|
2020-07-20 04:08:18 +00:00
|
|
|
if not trade:
|
2020-08-04 17:43:22 +00:00
|
|
|
logger.warning('delete trade: Invalid argument received')
|
2020-07-20 04:08:18 +00:00
|
|
|
raise RPCException('invalid argument')
|
|
|
|
|
2020-08-04 17:43:22 +00:00
|
|
|
# Try cancelling regular order if that exists
|
|
|
|
if trade.open_order_id:
|
|
|
|
try:
|
|
|
|
self._freqtrade.exchange.cancel_order(trade.open_order_id, trade.pair)
|
|
|
|
c_count += 1
|
2020-08-12 12:25:50 +00:00
|
|
|
except (ExchangeError):
|
2020-08-04 17:43:22 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
# cancel stoploss on exchange ...
|
|
|
|
if (self._freqtrade.strategy.order_types.get('stoploss_on_exchange')
|
|
|
|
and trade.stoploss_order_id):
|
|
|
|
try:
|
|
|
|
self._freqtrade.exchange.cancel_stoploss_order(trade.stoploss_order_id,
|
|
|
|
trade.pair)
|
|
|
|
c_count += 1
|
2020-08-12 12:25:50 +00:00
|
|
|
except (ExchangeError):
|
2020-08-04 17:43:22 +00:00
|
|
|
pass
|
|
|
|
|
2020-09-06 12:27:36 +00:00
|
|
|
trade.delete()
|
2020-07-20 04:08:18 +00:00
|
|
|
self._freqtrade.wallets.update()
|
2020-08-04 17:43:22 +00:00
|
|
|
return {
|
|
|
|
'result': 'success',
|
|
|
|
'trade_id': trade_id,
|
|
|
|
'result_msg': f'Deleted trade {trade_id}. Closed {c_count} open orders.',
|
|
|
|
'cancel_order_count': c_count,
|
|
|
|
}
|
2018-10-09 17:25:43 +00:00
|
|
|
|
2019-10-30 08:59:54 +00:00
|
|
|
def _rpc_performance(self) -> List[Dict[str, Any]]:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Handler for performance.
|
|
|
|
Shows a performance statistic from finished trades
|
|
|
|
"""
|
2019-10-30 08:59:54 +00:00
|
|
|
pair_rates = Trade.get_overall_performance()
|
|
|
|
# Round and convert to %
|
|
|
|
[x.update({'profit': round(x['profit'] * 100, 2)}) for x in pair_rates]
|
|
|
|
return pair_rates
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2019-04-06 18:01:29 +00:00
|
|
|
def _rpc_count(self) -> Dict[str, float]:
|
2018-06-08 02:52:50 +00:00
|
|
|
""" Returns the number of trades running """
|
2018-06-08 22:20:10 +00:00
|
|
|
if self._freqtrade.state != State.RUNNING:
|
2018-06-22 01:37:19 +00:00
|
|
|
raise RPCException('trader is not running')
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2019-04-06 17:58:45 +00:00
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
return {
|
|
|
|
'current': len(trades),
|
2021-01-12 18:24:37 +00:00
|
|
|
'max': (int(self._freqtrade.config['max_open_trades'])
|
|
|
|
if self._freqtrade.config['max_open_trades'] != float('inf') else -1),
|
2019-04-06 18:01:29 +00:00
|
|
|
'total_stake': sum((trade.open_rate * trade.amount) for trade in trades)
|
2019-04-06 17:58:45 +00:00
|
|
|
}
|
2018-11-10 19:07:09 +00:00
|
|
|
|
2020-10-17 13:15:35 +00:00
|
|
|
def _rpc_locks(self) -> Dict[str, Any]:
|
|
|
|
""" Returns the current locks"""
|
|
|
|
|
2020-10-25 09:54:30 +00:00
|
|
|
locks = PairLocks.get_pair_locks(None)
|
2020-10-17 13:15:35 +00:00
|
|
|
return {
|
|
|
|
'lock_count': len(locks),
|
|
|
|
'locks': [lock.to_json() for lock in locks]
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:07:09 +00:00
|
|
|
def _rpc_whitelist(self) -> Dict:
|
|
|
|
""" Returns the currently active whitelist"""
|
2019-11-09 13:00:32 +00:00
|
|
|
res = {'method': self._freqtrade.pairlists.name_list,
|
2019-03-24 15:08:48 +00:00
|
|
|
'length': len(self._freqtrade.active_pair_whitelist),
|
2018-11-10 19:07:09 +00:00
|
|
|
'whitelist': self._freqtrade.active_pair_whitelist
|
|
|
|
}
|
|
|
|
return res
|
2019-03-24 15:08:48 +00:00
|
|
|
|
2019-04-26 07:55:11 +00:00
|
|
|
def _rpc_blacklist(self, add: List[str] = None) -> Dict:
|
2019-03-24 15:08:48 +00:00
|
|
|
""" Returns the currently active blacklist"""
|
2020-05-28 04:51:53 +00:00
|
|
|
errors = {}
|
2019-03-24 15:28:14 +00:00
|
|
|
if add:
|
2019-03-25 18:40:21 +00:00
|
|
|
for pair in add:
|
2020-12-30 08:57:31 +00:00
|
|
|
if pair not in self._freqtrade.pairlists.blacklist:
|
|
|
|
try:
|
|
|
|
expand_pairlist([pair], self._freqtrade.exchange.get_markets().keys())
|
2020-05-28 04:51:53 +00:00
|
|
|
self._freqtrade.pairlists.blacklist.append(pair)
|
|
|
|
|
2020-12-30 08:57:31 +00:00
|
|
|
except ValueError:
|
|
|
|
errors[pair] = {
|
|
|
|
'error_msg': f'Pair {pair} is not a valid wildcard.'}
|
2020-05-28 04:51:53 +00:00
|
|
|
else:
|
|
|
|
errors[pair] = {
|
2020-12-30 08:57:31 +00:00
|
|
|
'error_msg': f'Pair {pair} already in pairlist.'}
|
2019-03-24 15:28:14 +00:00
|
|
|
|
2019-11-09 13:00:32 +00:00
|
|
|
res = {'method': self._freqtrade.pairlists.name_list,
|
2019-03-24 15:08:48 +00:00
|
|
|
'length': len(self._freqtrade.pairlists.blacklist),
|
2019-03-25 18:40:21 +00:00
|
|
|
'blacklist': self._freqtrade.pairlists.blacklist,
|
2020-12-30 08:57:31 +00:00
|
|
|
'blacklist_expanded': self._freqtrade.pairlists.expanded_blacklist,
|
2020-05-28 04:51:53 +00:00
|
|
|
'errors': errors,
|
2019-03-24 15:08:48 +00:00
|
|
|
}
|
|
|
|
return res
|
2019-03-25 09:16:09 +00:00
|
|
|
|
2020-12-06 18:57:48 +00:00
|
|
|
@staticmethod
|
|
|
|
def _rpc_get_logs(limit: Optional[int]) -> Dict[str, Any]:
|
2020-08-14 13:44:36 +00:00
|
|
|
"""Returns the last X logs"""
|
|
|
|
if limit:
|
|
|
|
buffer = bufferHandler.buffer[-limit:]
|
|
|
|
else:
|
|
|
|
buffer = bufferHandler.buffer
|
2020-10-17 18:32:23 +00:00
|
|
|
records = [[datetime.fromtimestamp(r.created).strftime(DATETIME_PRINT_FORMAT),
|
2020-08-27 10:04:55 +00:00
|
|
|
r.created * 1000, r.name, r.levelname,
|
2020-08-15 18:15:02 +00:00
|
|
|
r.message + ('\n' + r.exc_text if r.exc_text else '')]
|
2020-08-14 17:36:12 +00:00
|
|
|
for r in buffer]
|
2020-08-14 13:44:36 +00:00
|
|
|
|
2020-08-27 12:41:31 +00:00
|
|
|
# Log format:
|
2020-08-27 10:04:55 +00:00
|
|
|
# [logtime-formatted, logepoch, logger-name, loglevel, message \n + exception]
|
|
|
|
# e.g. ["2020-08-27 11:35:01", 1598520901097.9397,
|
2020-08-27 12:41:31 +00:00
|
|
|
# "freqtrade.worker", "INFO", "Starting worker develop"]
|
2020-08-27 10:04:55 +00:00
|
|
|
|
2020-08-14 13:44:36 +00:00
|
|
|
return {'log_count': len(records), 'logs': records}
|
|
|
|
|
2019-03-25 09:16:09 +00:00
|
|
|
def _rpc_edge(self) -> List[Dict[str, Any]]:
|
2019-03-24 21:36:33 +00:00
|
|
|
""" Returns information related to Edge """
|
|
|
|
if not self._freqtrade.edge:
|
2020-05-18 09:40:25 +00:00
|
|
|
raise RPCException('Edge is not enabled.')
|
2019-04-03 12:14:47 +00:00
|
|
|
return self._freqtrade.edge.accepted_pairs()
|
2020-06-12 17:32:44 +00:00
|
|
|
|
2020-10-16 04:26:57 +00:00
|
|
|
@staticmethod
|
|
|
|
def _convert_dataframe_to_dict(strategy: str, pair: str, timeframe: str, dataframe: DataFrame,
|
|
|
|
last_analyzed: datetime) -> Dict[str, Any]:
|
2020-08-24 18:38:01 +00:00
|
|
|
has_content = len(dataframe) != 0
|
2020-09-27 07:24:39 +00:00
|
|
|
buy_signals = 0
|
|
|
|
sell_signals = 0
|
2020-08-24 18:38:01 +00:00
|
|
|
if has_content:
|
|
|
|
|
|
|
|
dataframe.loc[:, '__date_ts'] = dataframe.loc[:, 'date'].astype(int64) // 1000 // 1000
|
|
|
|
# Move open to seperate column when signal for easy plotting
|
|
|
|
if 'buy' in dataframe.columns:
|
|
|
|
buy_mask = (dataframe['buy'] == 1)
|
2020-09-27 07:24:39 +00:00
|
|
|
buy_signals = int(buy_mask.sum())
|
2020-08-24 18:38:01 +00:00
|
|
|
dataframe.loc[buy_mask, '_buy_signal_open'] = dataframe.loc[buy_mask, 'open']
|
|
|
|
if 'sell' in dataframe.columns:
|
|
|
|
sell_mask = (dataframe['sell'] == 1)
|
2020-09-27 07:24:39 +00:00
|
|
|
sell_signals = int(sell_mask.sum())
|
2020-08-24 18:38:01 +00:00
|
|
|
dataframe.loc[sell_mask, '_sell_signal_open'] = dataframe.loc[sell_mask, 'open']
|
2021-02-11 19:21:07 +00:00
|
|
|
dataframe = dataframe.replace([inf, -inf], NAN)
|
2020-08-24 18:38:01 +00:00
|
|
|
dataframe = dataframe.replace({NAN: None})
|
|
|
|
|
|
|
|
res = {
|
2020-07-02 05:10:56 +00:00
|
|
|
'pair': pair,
|
2020-09-14 05:59:47 +00:00
|
|
|
'timeframe': timeframe,
|
|
|
|
'timeframe_ms': timeframe_to_msecs(timeframe),
|
2020-07-31 05:32:27 +00:00
|
|
|
'strategy': strategy,
|
2020-07-02 05:10:56 +00:00
|
|
|
'columns': list(dataframe.columns),
|
|
|
|
'data': dataframe.values.tolist(),
|
|
|
|
'length': len(dataframe),
|
2020-09-27 07:24:39 +00:00
|
|
|
'buy_signals': buy_signals,
|
|
|
|
'sell_signals': sell_signals,
|
2020-07-02 05:10:56 +00:00
|
|
|
'last_analyzed': last_analyzed,
|
2020-07-11 13:20:50 +00:00
|
|
|
'last_analyzed_ts': int(last_analyzed.timestamp()),
|
2020-08-24 18:38:01 +00:00
|
|
|
'data_start': '',
|
|
|
|
'data_start_ts': 0,
|
|
|
|
'data_stop': '',
|
|
|
|
'data_stop_ts': 0,
|
2020-07-02 05:10:56 +00:00
|
|
|
}
|
2020-08-24 18:38:01 +00:00
|
|
|
if has_content:
|
|
|
|
res.update({
|
|
|
|
'data_start': str(dataframe.iloc[0]['date']),
|
|
|
|
'data_start_ts': int(dataframe.iloc[0]['__date_ts']),
|
|
|
|
'data_stop': str(dataframe.iloc[-1]['date']),
|
|
|
|
'data_stop_ts': int(dataframe.iloc[-1]['__date_ts']),
|
|
|
|
})
|
|
|
|
return res
|
2020-07-02 05:10:56 +00:00
|
|
|
|
2021-02-11 19:29:31 +00:00
|
|
|
def _rpc_analysed_dataframe(self, pair: str, timeframe: str,
|
|
|
|
limit: Optional[int]) -> Dict[str, Any]:
|
2020-06-12 17:32:44 +00:00
|
|
|
|
2020-08-24 18:38:01 +00:00
|
|
|
_data, last_analyzed = self._freqtrade.dataprovider.get_analyzed_dataframe(
|
|
|
|
pair, timeframe)
|
|
|
|
_data = _data.copy()
|
2020-06-15 05:53:23 +00:00
|
|
|
if limit:
|
2020-08-24 18:38:01 +00:00
|
|
|
_data = _data.iloc[-limit:]
|
2020-07-31 05:32:27 +00:00
|
|
|
return self._convert_dataframe_to_dict(self._freqtrade.config['strategy'],
|
2020-09-14 05:59:47 +00:00
|
|
|
pair, timeframe, _data, last_analyzed)
|
2020-07-02 05:10:56 +00:00
|
|
|
|
2020-10-16 04:26:57 +00:00
|
|
|
@staticmethod
|
|
|
|
def _rpc_analysed_history_full(config, pair: str, timeframe: str,
|
2020-07-02 05:10:56 +00:00
|
|
|
timerange: str) -> Dict[str, Any]:
|
2020-10-02 04:41:28 +00:00
|
|
|
timerange_parsed = TimeRange.parse_timerange(timerange)
|
2020-07-02 05:10:56 +00:00
|
|
|
|
|
|
|
_data = load_data(
|
2020-07-26 18:31:11 +00:00
|
|
|
datadir=config.get("datadir"),
|
2020-07-02 05:10:56 +00:00
|
|
|
pairs=[pair],
|
2020-07-26 18:31:11 +00:00
|
|
|
timeframe=timeframe,
|
2020-10-02 04:41:28 +00:00
|
|
|
timerange=timerange_parsed,
|
2020-07-26 18:31:11 +00:00
|
|
|
data_format=config.get('dataformat_ohlcv', 'json'),
|
2020-07-02 05:10:56 +00:00
|
|
|
)
|
2021-01-06 14:38:46 +00:00
|
|
|
if pair not in _data:
|
|
|
|
raise RPCException(f"No data for {pair}, {timeframe} in {timerange} found.")
|
2020-07-02 05:10:56 +00:00
|
|
|
from freqtrade.resolvers.strategy_resolver import StrategyResolver
|
2020-07-26 18:31:11 +00:00
|
|
|
strategy = StrategyResolver.load_strategy(config)
|
2020-07-02 06:39:07 +00:00
|
|
|
df_analyzed = strategy.analyze_ticker(_data[pair], {'pair': pair})
|
2020-07-02 05:10:56 +00:00
|
|
|
|
2020-10-16 04:26:57 +00:00
|
|
|
return RPC._convert_dataframe_to_dict(strategy.get_strategy_name(), pair, timeframe,
|
|
|
|
df_analyzed, arrow.Arrow.utcnow().datetime)
|
2020-06-23 04:49:53 +00:00
|
|
|
|
|
|
|
def _rpc_plot_config(self) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
return self._freqtrade.strategy.plot_config
|