2018-01-28 05:26:57 +00:00
|
|
|
"""
|
|
|
|
IStrategy interface
|
|
|
|
This module defines the interface to apply for strategies
|
|
|
|
"""
|
2018-07-16 05:11:17 +00:00
|
|
|
import logging
|
2018-07-22 15:39:35 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2018-07-16 05:11:17 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from enum import Enum
|
2018-07-12 20:21:52 +00:00
|
|
|
from typing import Dict, List, NamedTuple, Tuple
|
2018-06-15 16:59:34 +00:00
|
|
|
import warnings
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
import arrow
|
2018-01-15 08:35:11 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
from freqtrade import constants
|
2018-07-17 09:12:25 +00:00
|
|
|
from freqtrade.exchange.exchange_helpers import parse_ticker_dataframe
|
2018-07-16 05:11:17 +00:00
|
|
|
from freqtrade.persistence import Trade
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class SignalType(Enum):
|
|
|
|
"""
|
|
|
|
Enum to distinguish between buy and sell signals
|
|
|
|
"""
|
|
|
|
BUY = "buy"
|
|
|
|
SELL = "sell"
|
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-07-11 17:22:34 +00:00
|
|
|
class SellType(Enum):
|
|
|
|
"""
|
|
|
|
Enum to distinguish between sell reasons
|
|
|
|
"""
|
|
|
|
ROI = "roi"
|
|
|
|
STOP_LOSS = "stop_loss"
|
|
|
|
TRAILING_STOP_LOSS = "trailing_stop_loss"
|
|
|
|
SELL_SIGNAL = "sell_signal"
|
2018-07-11 17:57:01 +00:00
|
|
|
FORCE_SELL = "force_sell"
|
2018-07-11 17:59:30 +00:00
|
|
|
NONE = ""
|
2018-07-11 17:22:34 +00:00
|
|
|
|
|
|
|
|
2018-07-12 20:21:52 +00:00
|
|
|
class SellCheckTuple(NamedTuple):
|
|
|
|
"""
|
|
|
|
NamedTuple for Sell type + reason
|
|
|
|
"""
|
|
|
|
sell_flag: bool
|
|
|
|
sell_type: SellType
|
|
|
|
|
|
|
|
|
2018-01-15 08:35:11 +00:00
|
|
|
class IStrategy(ABC):
|
2018-01-28 05:26:57 +00:00
|
|
|
"""
|
|
|
|
Interface for freqtrade strategies
|
|
|
|
Defines the mandatory structure must follow any custom strategies
|
|
|
|
|
|
|
|
Attributes you can use:
|
|
|
|
minimal_roi -> Dict: Minimal ROI designed for the strategy
|
|
|
|
stoploss -> float: optimal stoploss designed for the strategy
|
2018-05-31 19:59:22 +00:00
|
|
|
ticker_interval -> str: value of the ticker interval to use for the strategy
|
2018-01-28 05:26:57 +00:00
|
|
|
"""
|
2018-01-15 08:35:11 +00:00
|
|
|
|
2018-07-23 16:38:21 +00:00
|
|
|
_populate_fun_len: int = 0
|
|
|
|
_buy_fun_len: int = 0
|
|
|
|
_sell_fun_len: int = 0
|
2018-06-15 03:27:41 +00:00
|
|
|
# associated minimal roi
|
2018-05-31 19:59:22 +00:00
|
|
|
minimal_roi: Dict
|
2018-06-15 03:27:41 +00:00
|
|
|
|
|
|
|
# associated stoploss
|
2018-05-31 19:59:22 +00:00
|
|
|
stoploss: float
|
2018-06-15 03:27:41 +00:00
|
|
|
|
|
|
|
# associated ticker interval
|
2018-05-31 19:59:22 +00:00
|
|
|
ticker_interval: str
|
|
|
|
|
2018-07-17 07:47:15 +00:00
|
|
|
def __init__(self, config: dict) -> None:
|
2018-07-16 05:11:17 +00:00
|
|
|
self.config = config
|
|
|
|
|
2018-07-22 15:39:35 +00:00
|
|
|
@abstractmethod
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
Populate indicators that will be used in the Buy and Sell strategy
|
|
|
|
:param dataframe: Raw data from the exchange and parsed by parse_ticker_dataframe()
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-01-15 08:35:11 +00:00
|
|
|
:return: a Dataframe with all mandatory indicators for the strategies
|
|
|
|
"""
|
|
|
|
|
2018-07-22 15:39:35 +00:00
|
|
|
@abstractmethod
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the buy signal for the given dataframe
|
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-01-15 08:35:11 +00:00
|
|
|
:return: DataFrame with buy column
|
|
|
|
"""
|
|
|
|
|
2018-07-22 15:39:35 +00:00
|
|
|
@abstractmethod
|
2018-07-29 18:36:03 +00:00
|
|
|
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the sell signal for the given dataframe
|
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-03-25 18:24:56 +00:00
|
|
|
:return: DataFrame with sell column
|
2018-01-15 08:35:11 +00:00
|
|
|
"""
|
2018-07-12 18:38:14 +00:00
|
|
|
|
|
|
|
def get_strategy_name(self) -> str:
|
|
|
|
"""
|
|
|
|
Returns strategy class name
|
|
|
|
"""
|
2018-07-19 17:41:42 +00:00
|
|
|
return self.__class__.__name__
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def analyze_ticker(self, ticker_history: List[Dict], metadata: dict) -> DataFrame:
|
2018-07-16 05:11:17 +00:00
|
|
|
"""
|
|
|
|
Parses the given ticker history and returns a populated DataFrame
|
|
|
|
add several TA indicators and buy signal to it
|
|
|
|
:return DataFrame with ticker data and indicator data
|
|
|
|
"""
|
|
|
|
dataframe = parse_ticker_dataframe(ticker_history)
|
2018-07-29 18:36:03 +00:00
|
|
|
dataframe = self.advise_indicators(dataframe, metadata)
|
|
|
|
dataframe = self.advise_buy(dataframe, metadata)
|
|
|
|
dataframe = self.advise_sell(dataframe, metadata)
|
2018-07-16 05:11:17 +00:00
|
|
|
return dataframe
|
|
|
|
|
2018-07-26 17:22:23 +00:00
|
|
|
def get_signal(self, pair: str, interval: str, ticker_hist: List[Dict]) -> Tuple[bool, bool]:
|
2018-07-16 05:11:17 +00:00
|
|
|
"""
|
|
|
|
Calculates current signal based several technical analysis indicators
|
|
|
|
:param pair: pair in format ANT/BTC
|
|
|
|
:param interval: Interval to use (in min)
|
|
|
|
:return: (Buy, Sell) A bool-tuple indicating buy/sell signal
|
|
|
|
"""
|
|
|
|
if not ticker_hist:
|
|
|
|
logger.warning('Empty ticker history for pair %s', pair)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
try:
|
2018-07-29 18:36:03 +00:00
|
|
|
dataframe = self.analyze_ticker(ticker_hist, {'pair': pair})
|
2018-07-16 05:11:17 +00:00
|
|
|
except ValueError as error:
|
|
|
|
logger.warning(
|
|
|
|
'Unable to analyze ticker for pair %s: %s',
|
|
|
|
pair,
|
|
|
|
str(error)
|
|
|
|
)
|
|
|
|
return False, False
|
|
|
|
except Exception as error:
|
|
|
|
logger.exception(
|
|
|
|
'Unexpected error when analyzing ticker for pair %s: %s',
|
|
|
|
pair,
|
|
|
|
str(error)
|
|
|
|
)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
if dataframe.empty:
|
|
|
|
logger.warning('Empty dataframe for pair %s', pair)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
latest = dataframe.iloc[-1]
|
|
|
|
|
|
|
|
# Check if dataframe is out of date
|
|
|
|
signal_date = arrow.get(latest['date'])
|
|
|
|
interval_minutes = constants.TICKER_INTERVAL_MINUTES[interval]
|
2018-08-07 07:25:21 +00:00
|
|
|
offset = self.config.get('outdated_offset', 5)
|
|
|
|
if signal_date < (arrow.utcnow().shift(minutes=-(interval_minutes * 2 + offset))):
|
2018-07-16 05:11:17 +00:00
|
|
|
logger.warning(
|
|
|
|
'Outdated history for pair %s. Last tick is %s minutes old',
|
|
|
|
pair,
|
|
|
|
(arrow.utcnow() - signal_date).seconds // 60
|
|
|
|
)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
(buy, sell) = latest[SignalType.BUY.value] == 1, latest[SignalType.SELL.value] == 1
|
|
|
|
logger.debug(
|
|
|
|
'trigger: %s (pair=%s) buy=%s sell=%s',
|
|
|
|
latest['date'],
|
|
|
|
pair,
|
|
|
|
str(buy),
|
|
|
|
str(sell)
|
|
|
|
)
|
|
|
|
return buy, sell
|
|
|
|
|
2018-07-11 17:57:01 +00:00
|
|
|
def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool,
|
2018-07-12 20:21:52 +00:00
|
|
|
sell: bool) -> SellCheckTuple:
|
2018-07-16 05:11:17 +00:00
|
|
|
"""
|
|
|
|
This function evaluate if on the condition required to trigger a sell has been reached
|
|
|
|
if the threshold is reached and updates the trade record.
|
|
|
|
:return: True if trade should be sold, False otherwise
|
|
|
|
"""
|
|
|
|
current_profit = trade.calc_profit_percent(rate)
|
2018-07-11 17:57:01 +00:00
|
|
|
stoplossflag = self.stop_loss_reached(current_rate=rate, trade=trade, current_time=date,
|
|
|
|
current_profit=current_profit)
|
2018-07-12 20:21:52 +00:00
|
|
|
if stoplossflag.sell_flag:
|
|
|
|
return stoplossflag
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
experimental = self.config.get('experimental', {})
|
|
|
|
|
|
|
|
if buy and experimental.get('ignore_roi_if_buy_signal', False):
|
|
|
|
logger.debug('Buy signal still active - not selling.')
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
# Check if minimal roi has been reached and no longer in buy conditions (avoiding a fee)
|
|
|
|
if self.min_roi_reached(trade=trade, current_profit=current_profit, current_time=date):
|
|
|
|
logger.debug('Required profit reached. Selling..')
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=True, sell_type=SellType.ROI)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
if experimental.get('sell_profit_only', False):
|
|
|
|
logger.debug('Checking if trade is profitable..')
|
|
|
|
if trade.calc_profit(rate=rate) <= 0:
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
|
2018-07-16 05:11:17 +00:00
|
|
|
if sell and not buy and experimental.get('use_sell_signal', False):
|
|
|
|
logger.debug('Sell signal received. Selling..')
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=True, sell_type=SellType.SELL_SIGNAL)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
def stop_loss_reached(self, current_rate: float, trade: Trade, current_time: datetime,
|
2018-07-12 20:21:52 +00:00
|
|
|
current_profit: float) -> SellCheckTuple:
|
2018-07-16 05:11:17 +00:00
|
|
|
"""
|
|
|
|
Based on current profit of the trade and configured (trailing) stoploss,
|
|
|
|
decides to sell or not
|
2018-07-16 19:23:35 +00:00
|
|
|
:param current_profit: current profit in percent
|
2018-07-16 05:11:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
trailing_stop = self.config.get('trailing_stop', False)
|
|
|
|
|
|
|
|
trade.adjust_stop_loss(trade.open_rate, self.stoploss, initial=True)
|
|
|
|
|
|
|
|
# evaluate if the stoploss was hit
|
|
|
|
if self.stoploss is not None and trade.stop_loss >= current_rate:
|
2018-07-11 17:57:01 +00:00
|
|
|
selltype = SellType.STOP_LOSS
|
2018-07-16 05:11:17 +00:00
|
|
|
if trailing_stop:
|
2018-07-11 17:57:01 +00:00
|
|
|
selltype = SellType.TRAILING_STOP_LOSS
|
2018-07-16 05:11:17 +00:00
|
|
|
logger.debug(
|
|
|
|
f"HIT STOP: current price at {current_rate:.6f}, "
|
|
|
|
f"stop loss is {trade.stop_loss:.6f}, "
|
|
|
|
f"initial stop loss was at {trade.initial_stop_loss:.6f}, "
|
|
|
|
f"trade opened at {trade.open_rate:.6f}")
|
|
|
|
logger.debug(f"trailing stop saved {trade.stop_loss - trade.initial_stop_loss:.6f}")
|
|
|
|
|
|
|
|
logger.debug('Stop loss hit.')
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=True, sell_type=selltype)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
# update the stop loss afterwards, after all by definition it's supposed to be hanging
|
|
|
|
if trailing_stop:
|
|
|
|
|
|
|
|
# check if we have a special stop loss for positive condition
|
|
|
|
# and if profit is positive
|
|
|
|
stop_loss_value = self.stoploss
|
2018-07-16 19:23:35 +00:00
|
|
|
sl_offset = self.config.get('trailing_stop_positive_offset', 0.0)
|
|
|
|
|
|
|
|
if 'trailing_stop_positive' in self.config and current_profit > sl_offset:
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
# Ignore mypy error check in configuration that this is a float
|
|
|
|
stop_loss_value = self.config.get('trailing_stop_positive') # type: ignore
|
|
|
|
logger.debug(f"using positive stop loss mode: {stop_loss_value} "
|
2018-07-16 19:23:35 +00:00
|
|
|
f"with offset {sl_offset:.4g} "
|
|
|
|
f"since we have profit {current_profit:.4f}%")
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
trade.adjust_stop_loss(current_rate, stop_loss_value)
|
|
|
|
|
2018-07-12 20:21:52 +00:00
|
|
|
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
|
|
|
def min_roi_reached(self, trade: Trade, current_profit: float, current_time: datetime) -> bool:
|
|
|
|
"""
|
|
|
|
Based an earlier trade and current price and ROI configuration, decides whether bot should
|
|
|
|
sell
|
|
|
|
:return True if bot should sell at current rate
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check if time matches and current rate is above threshold
|
|
|
|
time_diff = (current_time.timestamp() - trade.open_date.timestamp()) / 60
|
|
|
|
for duration, threshold in self.minimal_roi.items():
|
|
|
|
if time_diff <= duration:
|
|
|
|
return False
|
|
|
|
if current_profit > threshold:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def tickerdata_to_dataframe(self, tickerdata: Dict[str, List]) -> Dict[str, DataFrame]:
|
|
|
|
"""
|
|
|
|
Creates a dataframe and populates indicators for given ticker data
|
|
|
|
"""
|
2018-07-29 18:36:03 +00:00
|
|
|
return {pair: self.advise_indicators(parse_ticker_dataframe(pair_data), {'pair': pair})
|
2018-07-16 05:11:17 +00:00
|
|
|
for pair, pair_data in tickerdata.items()}
|
2018-06-15 03:27:41 +00:00
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def advise_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-06-15 03:27:41 +00:00
|
|
|
"""
|
|
|
|
Populate indicators that will be used in the Buy and Sell strategy
|
2018-07-22 15:39:35 +00:00
|
|
|
This method should not be overridden.
|
2018-06-15 03:27:41 +00:00
|
|
|
:param dataframe: Raw data from the exchange and parsed by parse_ticker_dataframe()
|
2018-07-29 18:36:03 +00:00
|
|
|
:param metadata: Additional information, like the currently traded pair
|
2018-06-15 03:27:41 +00:00
|
|
|
:return: a Dataframe with all mandatory indicators for the strategies
|
|
|
|
"""
|
2018-07-23 16:38:21 +00:00
|
|
|
if self._populate_fun_len == 2:
|
2018-07-22 15:39:35 +00:00
|
|
|
warnings.warn("deprecated - check out the Sample strategy to see "
|
|
|
|
"the current function headers!", DeprecationWarning)
|
|
|
|
return self.populate_indicators(dataframe) # type: ignore
|
|
|
|
else:
|
2018-07-29 18:36:03 +00:00
|
|
|
return self.populate_indicators(dataframe, metadata)
|
2018-06-15 03:27:41 +00:00
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def advise_buy(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-06-15 03:27:41 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the buy signal for the given dataframe
|
2018-07-22 15:39:35 +00:00
|
|
|
This method should not be overridden.
|
2018-06-15 03:27:41 +00:00
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param pair: Additional information, like the currently traded pair
|
2018-06-15 03:27:41 +00:00
|
|
|
:return: DataFrame with buy column
|
|
|
|
"""
|
2018-07-23 16:38:21 +00:00
|
|
|
if self._buy_fun_len == 2:
|
2018-07-22 15:39:35 +00:00
|
|
|
warnings.warn("deprecated - check out the Sample strategy to see "
|
|
|
|
"the current function headers!", DeprecationWarning)
|
|
|
|
return self.populate_buy_trend(dataframe) # type: ignore
|
|
|
|
else:
|
2018-07-29 18:36:03 +00:00
|
|
|
return self.populate_buy_trend(dataframe, metadata)
|
2018-06-15 03:27:41 +00:00
|
|
|
|
2018-07-29 18:36:03 +00:00
|
|
|
def advise_sell(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
2018-06-15 03:27:41 +00:00
|
|
|
"""
|
|
|
|
Based on TA indicators, populates the sell signal for the given dataframe
|
2018-07-22 15:39:35 +00:00
|
|
|
This method should not be overridden.
|
2018-06-15 03:27:41 +00:00
|
|
|
:param dataframe: DataFrame
|
2018-07-29 18:36:03 +00:00
|
|
|
:param pair: Additional information, like the currently traded pair
|
2018-06-15 03:27:41 +00:00
|
|
|
:return: DataFrame with sell column
|
|
|
|
"""
|
2018-07-23 16:38:21 +00:00
|
|
|
if self._sell_fun_len == 2:
|
2018-07-22 15:39:35 +00:00
|
|
|
warnings.warn("deprecated - check out the Sample strategy to see "
|
|
|
|
"the current function headers!", DeprecationWarning)
|
|
|
|
return self.populate_sell_trend(dataframe) # type: ignore
|
|
|
|
else:
|
2018-07-29 18:36:03 +00:00
|
|
|
return self.populate_sell_trend(dataframe, metadata)
|