import logging from tokenize import String import freqtrade.vendor.qtpylib.indicators as qtpylib import numpy as np import talib.abstract as ta from freqtrade.strategy.interface import IStrategy from freqtrade.strategy import merge_informative_pair from pandas import DataFrame, Series from functools import reduce, partial from freqtrade.persistence import Trade from datetime import datetime, timedelta import time log = logging.getLogger(__name__) class NostalgiaForInfinityX2(IStrategy): INTERFACE_VERSION = 2 def version(self) -> str: return "v0.0.1" minimal_roi = { "0": 100.0, } stoploss = -0.99 trailing_stop = False trailing_only_offset_is_reached = True trailing_stop_positive = 0.01 trailing_stop_positive_offset = 0.03 use_custom_stoploss = False timeframe = '5m' info_timeframes = ['15m','1h','4h','1d'] btc_info_timeframes = ['5m','15m','1h','4h','1d'] has_bt_agefilter = False bt_min_age_days = 3 has_downtime_protection = False process_only_new_candles = True use_sell_signal = True sell_profit_only = False ignore_roi_if_buy_signal = True startup_candle_count: int = 480 order_types = { 'buy': 'limit', 'sell': 'limit', 'trailing_stop_loss': 'limit', 'stoploss': 'limit', 'stoploss_on_exchange': False, 'stoploss_on_exchange_interval': 60, 'stoploss_on_exchange_limit_ratio': 0.99 } buy_params = { "buy_condition_1_enable": True, } buy_protection_params = {} def get_ticker_indicator(self): return int(self.timeframe[:-1]) def custom_sell(self, pair: str, trade: 'Trade', current_time: 'datetime', current_rate: float, current_profit: float, **kwargs): dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe) last_candle = dataframe.iloc[-1] previous_candle_1 = dataframe.iloc[-2] previous_candle_2 = dataframe.iloc[-3] previous_candle_3 = dataframe.iloc[-4] previous_candle_4 = dataframe.iloc[-5] previous_candle_5 = dataframe.iloc[-6] buy_tag = 'empty' if hasattr(trade, 'buy_tag') and trade.buy_tag is not None: buy_tag = trade.buy_tag buy_tags = buy_tag.split() max_profit = ((trade.max_rate - trade.open_rate) / trade.open_rate) max_loss = ((trade.open_rate - trade.min_rate) / trade.min_rate) return None def informative_pairs(self): pairs = self.dp.current_whitelist() informative_pairs = [] for info_timeframe in self.info_timeframes: informative_pairs.extend([(pair, info_timeframe) for pair in pairs]) if self.config['stake_currency'] in ['USDT','BUSD','USDC','DAI','TUSD','PAX','USD','EUR','GBP']: btc_info_pair = f"BTC/{self.config['stake_currency']}" else: btc_info_pair = "BTC/USDT" informative_pairs.extend([(btc_info_pair, btc_info_timeframe) for btc_info_timeframe in self.btc_info_timeframes]) return informative_pairs def informative_1d_indicators(self, metadata: dict, info_timeframe) -> DataFrame: tik = time.perf_counter() assert self.dp, "DataProvider is required for multiple timeframes." informative_1d = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=info_timeframe) informative_1d['rsi_14'] = ta.RSI(informative_1d, timeperiod=14) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] informative_1d_indicators took: {tok - tik:0.4f} seconds.") return informative_1d def informative_4h_indicators(self, metadata: dict, info_timeframe) -> DataFrame: tik = time.perf_counter() assert self.dp, "DataProvider is required for multiple timeframes." informative_4h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=info_timeframe) informative_4h['rsi_14'] = ta.RSI(informative_4h, timeperiod=14, fillna=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] informative_1d_indicators took: {tok - tik:0.4f} seconds.") return informative_4h def informative_1h_indicators(self, metadata: dict, info_timeframe) -> DataFrame: tik = time.perf_counter() assert self.dp, "DataProvider is required for multiple timeframes." informative_1h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=info_timeframe) informative_1h['rsi_14'] = ta.RSI(informative_1h, timeperiod=14) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] informative_1h_indicators took: {tok - tik:0.4f} seconds.") return informative_1h def informative_15m_indicators(self, metadata: dict, info_timeframe) -> DataFrame: tik = time.perf_counter() assert self.dp, "DataProvider is required for multiple timeframes." informative_15m = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=info_timeframe) informative_15m['rsi_14'] = ta.RSI(informative_15m, timeperiod=14) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] informative_15m_indicators took: {tok - tik:0.4f} seconds.") return informative_15m def base_tf_5m_indicators(self, metadata: dict, dataframe: DataFrame) -> DataFrame: tik = time.perf_counter() dataframe['rsi_14'] = ta.RSI(dataframe, timeperiod=14) if not self.config['runmode'].value in ('live', 'dry_run'): dataframe['bt_agefilter_ok'] = False dataframe.loc[dataframe.index > (12 * 24 * self.bt_min_age_days),'bt_agefilter_ok'] = True else: dataframe['live_data_ok'] = (dataframe['volume'].rolling(window=72, min_periods=72).min() > 0) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] base_tf_5m_indicators took: {tok - tik:0.4f} seconds.") return dataframe def info_switcher(self, metadata: dict, info_timeframe: String) -> DataFrame: if info_timeframe == '1d': return self.informative_1d_indicators(metadata, info_timeframe) elif info_timeframe == '4h': return self.informative_4h_indicators(metadata, info_timeframe) elif info_timeframe == '1h': return self.informative_1h_indicators(metadata, info_timeframe) elif info_timeframe == '15m': return self.informative_15m_indicators(metadata, info_timeframe) else: raise RuntimeError(f"{info_timeframe} not supported as informative timeframe for BTC pair.") def btc_info_1d_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict) -> DataFrame: tik = time.perf_counter() btc_info_1d = self.dp.get_pair_dataframe(btc_info_pair, btc_info_timeframe) btc_info_1d['rsi_14'] = ta.RSI(btc_info_1d, timeperiod=14) ignore_columns = ['date', 'open', 'high', 'low', 'close', 'volume'] btc_info_1d.rename(columns=lambda s: f"btc_{s}" if s not in ignore_columns else s, inplace=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] btc_info_1d_indicators took: {tok - tik:0.4f} seconds.") return btc_info_1d def btc_info_4h_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict) -> DataFrame: tik = time.perf_counter() btc_info_4h = self.dp.get_pair_dataframe(btc_info_pair, btc_info_timeframe) btc_info_4h['rsi_14'] = ta.RSI(btc_info_4h, timeperiod=14) ignore_columns = ['date', 'open', 'high', 'low', 'close', 'volume'] btc_info_4h.rename(columns=lambda s: f"btc_{s}" if s not in ignore_columns else s, inplace=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] btc_info_4h_indicators took: {tok - tik:0.4f} seconds.") return btc_info_4h def btc_info_1h_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict) -> DataFrame: tik = time.perf_counter() btc_info_1h = self.dp.get_pair_dataframe(btc_info_pair, btc_info_timeframe) btc_info_1h['rsi_14'] = ta.RSI(btc_info_1h, timeperiod=14) btc_info_1h['not_downtrend'] = ((btc_info_1h['close'] > btc_info_1h['close'].shift(2)) | (btc_info_1h['rsi_14'] > 50)) ignore_columns = ['date', 'open', 'high', 'low', 'close', 'volume'] btc_info_1h.rename(columns=lambda s: f"btc_{s}" if s not in ignore_columns else s, inplace=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] btc_info_1h_indicators took: {tok - tik:0.4f} seconds.") return btc_info_1h def btc_info_15m_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict) -> DataFrame: tik = time.perf_counter() btc_info_15m = self.dp.get_pair_dataframe(btc_info_pair, btc_info_timeframe) btc_info_15m['rsi_14'] = ta.RSI(btc_info_15m, timeperiod=14) ignore_columns = ['date', 'open', 'high', 'low', 'close', 'volume'] btc_info_15m.rename(columns=lambda s: f"btc_{s}" if s not in ignore_columns else s, inplace=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] btc_info_15m_indicators took: {tok - tik:0.4f} seconds.") return btc_info_15m def btc_info_5m_indicators(self, btc_info_pair, btc_info_timeframe, metadata: dict) -> DataFrame: tik = time.perf_counter() btc_info_5m = self.dp.get_pair_dataframe(btc_info_pair, btc_info_timeframe) btc_info_5m['rsi_14'] = ta.RSI(btc_info_5m, timeperiod=14) ignore_columns = ['date', 'open', 'high', 'low', 'close', 'volume'] btc_info_5m.rename(columns=lambda s: f"btc_{s}" if s not in ignore_columns else s, inplace=True) tok = time.perf_counter() log.debug(f"[{metadata['pair']}] btc_info_5m_indicators took: {tok - tik:0.4f} seconds.") return btc_info_5m def btc_info_switcher(self, btc_info_pair: String, btc_info_timeframe: String, metadata: dict) -> DataFrame: if btc_info_timeframe == '1d': return self.btc_info_1d_indicators(btc_info_pair, btc_info_timeframe, metadata) elif btc_info_timeframe == '4h': return self.btc_info_4h_indicators(btc_info_pair, btc_info_timeframe, metadata) elif btc_info_timeframe == '1h': return self.btc_info_1h_indicators(btc_info_pair, btc_info_timeframe, metadata) elif btc_info_timeframe == '15m': return self.btc_info_15m_indicators(btc_info_pair, btc_info_timeframe, metadata) elif btc_info_timeframe == '5m': return self.btc_info_5m_indicators(btc_info_pair, btc_info_timeframe, metadata) else: raise RuntimeError(f"{btc_info_timeframe} not supported as informative timeframe for BTC pair.") def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: tik = time.perf_counter() ''' --> BTC informative indicators ___________________________________________________________________________________________ ''' if self.config['stake_currency'] in ['USDT','BUSD','USDC','DAI','TUSD','PAX','USD','EUR','GBP']: btc_info_pair = f"BTC/{self.config['stake_currency']}" else: btc_info_pair = "BTC/USDT" for btc_info_timeframe in self.btc_info_timeframes: btc_informative = self.btc_info_switcher(btc_info_pair, btc_info_timeframe, metadata) dataframe = merge_informative_pair(dataframe, btc_informative, self.timeframe, btc_info_timeframe, ffill=True) drop_columns = { '1d': [f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '4h': [f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '1h': [f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '15m': [f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '5m': [f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], }.get(btc_info_timeframe,[f"{s}_{btc_info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']]) dataframe.drop(columns=dataframe.columns.intersection(drop_columns), inplace=True) ''' --> Indicators on informative timeframes ___________________________________________________________________________________________ ''' for info_timeframe in self.info_timeframes: info_indicators = self.info_switcher(metadata, info_timeframe) dataframe = merge_informative_pair(dataframe, info_indicators, self.timeframe, info_timeframe, ffill=True) drop_columns = { '1d': [f"{s}_{info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '4h': [f"{s}_{info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '1h': [f"{s}_{info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']], '15m': [f"{s}_{info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']] }.get(info_timeframe,[f"{s}_{info_timeframe}" for s in ['date', 'open', 'high', 'low', 'close', 'volume']]) dataframe.drop(columns=dataframe.columns.intersection(drop_columns), inplace=True) ''' --> The indicators for the base timeframe (5m) ___________________________________________________________________________________________ ''' dataframe = self.base_tf_5m_indicators(metadata, dataframe) dataframe.to_html(f"{metadata['pair'].split('/')[0]}_df.html") tok = time.perf_counter() log.debug(f"[{metadata['pair']}] Populate indicators took a total of: {tok - tik:0.4f} seconds.") return dataframe def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: conditions = [] dataframe.loc[:, 'buy_tag'] = '' for index in self.buy_protection_params: item_buy_protection_list = [True] global_buy_protection_params = self.buy_protection_params[index] if self.buy_params[f"buy_condition_{index}_enable"]: if not self.config['runmode'].value in ('live', 'dry_run'): if self.has_bt_agefilter: item_buy_protection_list.append(dataframe['bt_agefilter_ok']) else: if self.has_downtime_protection: item_buy_protection_list.append(dataframe['live_data_ok']) item_buy_logic = [] item_buy_logic.append(reduce(lambda x, y: x & y, item_buy_protection_list)) if index == 1: item_buy_logic.append(dataframe['rsi_14'] < 30.0) item_buy_logic.append(dataframe['volume'] > 0) item_buy = reduce(lambda x, y: x & y, item_buy_logic) dataframe.loc[item_buy, 'buy_tag'] += f"{index} " conditions.append(item_buy) if conditions: dataframe.loc[:, 'buy'] = reduce(lambda x, y: x | y, conditions) return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe.loc[:, 'sell'] = 0 return dataframe def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, current_time: datetime, **kwargs) -> bool: dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe) if(len(dataframe) < 1): return False dataframe = dataframe.iloc[-1].squeeze() if ((rate > dataframe['close'])): slippage = ((rate / dataframe['close']) - 1.0) if slippage < 0.038: return True else: log.warning( "Cancelling buy for %s due to slippage %s", pair, slippage ) return False return True def is_support(self, row_data) -> bool: conditions = [] for row in range(len(row_data)-1): if row < len(row_data)/2: conditions.append(row_data[row] > row_data[row+1]) else: conditions.append(row_data[row] < row_data[row+1]) return reduce(lambda x, y: x & y, conditions) def is_resistance(self, row_data) -> bool: conditions = [] for row in range(len(row_data)-1): if row < len(row_data)/2: conditions.append(row_data[row] < row_data[row+1]) else: conditions.append(row_data[row] > row_data[row+1]) return reduce(lambda x, y: x & y, conditions) def ewo(dataframe, sma1_length=5, sma2_length=35): sma1 = ta.EMA(dataframe, timeperiod=sma1_length) sma2 = ta.EMA(dataframe, timeperiod=sma2_length) smadif = (sma1 - sma2) / dataframe['close'] * 100 return smadif def chaikin_money_flow(dataframe, n=20, fillna=False) -> Series: """Chaikin Money Flow (CMF) It measures the amount of Money Flow Volume over a specific period. http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:chaikin_money_flow_cmf Args: dataframe(pandas.Dataframe): dataframe containing ohlcv n(int): n period. fillna(bool): if True, fill nan values. Returns: pandas.Series: New feature generated. """ mfv = ((dataframe['close'] - dataframe['low']) - (dataframe['high'] - dataframe['close'])) / (dataframe['high'] - dataframe['low']) mfv = mfv.fillna(0.0) # float division by zero mfv *= dataframe['volume'] cmf = (mfv.rolling(n, min_periods=0).sum() / dataframe['volume'].rolling(n, min_periods=0).sum()) if fillna: cmf = cmf.replace([np.inf, -np.inf], np.nan).fillna(0) return Series(cmf, name='cmf') def williams_r(dataframe: DataFrame, period: int = 14) -> Series: """Williams %R, or just %R, is a technical analysis oscillator showing the current closing price in relation to the high and low of the past N days (for a given N). It was developed by a publisher and promoter of trading materials, Larry Williams. Its purpose is to tell whether a stock or commodity market is trading near the high or the low, or somewhere in between, of its recent trading range. The oscillator is on a negative scale, from −100 (lowest) up to 0 (highest). """ highest_high = dataframe["high"].rolling(center=False, window=period).max() lowest_low = dataframe["low"].rolling(center=False, window=period).min() WR = Series( (highest_high - dataframe["close"]) / (highest_high - lowest_low), name=f"{period} Williams %R", ) return WR * -100 def vwma(dataframe: DataFrame, length: int = 10): """Indicator: Volume Weighted Moving Average (VWMA)""" pv = dataframe['close'] * dataframe['volume'] vwma = Series(ta.SMA(pv, timeperiod=length) / ta.SMA(dataframe['volume'], timeperiod=length)) vwma = vwma.fillna(0, inplace=True) return vwma def ema_vwma_osc(dataframe, len_slow_ma): slow_ema = Series(ta.EMA(vwma(dataframe, len_slow_ma), len_slow_ma)) return ((slow_ema - slow_ema.shift(1)) / slow_ema.shift(1)) * 100 def t3_average(dataframe, length=5): """ T3 Average by HPotter on Tradingview https://www.tradingview.com/script/qzoC9H1I-T3-Average/ """ df = dataframe.copy() df['xe1'] = ta.EMA(df['close'], timeperiod=length) df['xe1'].fillna(0, inplace=True) df['xe2'] = ta.EMA(df['xe1'], timeperiod=length) df['xe2'].fillna(0, inplace=True) df['xe3'] = ta.EMA(df['xe2'], timeperiod=length) df['xe3'].fillna(0, inplace=True) df['xe4'] = ta.EMA(df['xe3'], timeperiod=length) df['xe4'].fillna(0, inplace=True) df['xe5'] = ta.EMA(df['xe4'], timeperiod=length) df['xe5'].fillna(0, inplace=True) df['xe6'] = ta.EMA(df['xe5'], timeperiod=length) df['xe6'].fillna(0, inplace=True) b = 0.7 c1 = -b * b * b c2 = 3 * b * b + 3 * b * b * b c3 = -6 * b * b - 3 * b - 3 * b * b * b c4 = 1 + 3 * b + b * b * b + 3 * b * b df['T3Average'] = c1 * df['xe6'] + c2 * df['xe5'] + c3 * df['xe4'] + c4 * df['xe3'] return df['T3Average'] def pivot_points(dataframe: DataFrame, mode = 'fibonacci') -> Series: if mode == 'simple': hlc3_pivot = (dataframe['high'] + dataframe['low'] + dataframe['close']).shift(1) / 3 res1 = hlc3_pivot * 2 - dataframe['low'].shift(1) sup1 = hlc3_pivot * 2 - dataframe['high'].shift(1) res2 = hlc3_pivot + (dataframe['high'] - dataframe['low']).shift() sup2 = hlc3_pivot - (dataframe['high'] - dataframe['low']).shift() res3 = hlc3_pivot * 2 + (dataframe['high'] - 2 * dataframe['low']).shift() sup3 = hlc3_pivot * 2 - (2 * dataframe['high'] - dataframe['low']).shift() return hlc3_pivot, res1, res2, res3, sup1, sup2, sup3 elif mode == 'fibonacci': hlc3_pivot = (dataframe['high'] + dataframe['low'] + dataframe['close']).shift(1) / 3 hl_range = (dataframe['high'] - dataframe['low']).shift(1) res1 = hlc3_pivot + 0.382 * hl_range sup1 = hlc3_pivot - 0.382 * hl_range res2 = hlc3_pivot + 0.618 * hl_range sup2 = hlc3_pivot - 0.618 * hl_range res3 = hlc3_pivot + 1 * hl_range sup3 = hlc3_pivot - 1 * hl_range return hlc3_pivot, res1, res2, res3, sup1, sup2, sup3 elif mode == 'DeMark': demark_pivot_lt = (dataframe['low'] * 2 + dataframe['high'] + dataframe['close']) demark_pivot_eq = (dataframe['close'] * 2 + dataframe['low'] + dataframe['high']) demark_pivot_gt = (dataframe['high'] * 2 + dataframe['low'] + dataframe['close']) demark_pivot = np.where((dataframe['close'] < dataframe['open']), demark_pivot_lt, np.where((dataframe['close'] > dataframe['open']), demark_pivot_gt, demark_pivot_eq)) dm_pivot = demark_pivot / 4 dm_res = demark_pivot / 2 - dataframe['low'] dm_sup = demark_pivot / 2 - dataframe['high'] return dm_pivot, dm_res, dm_sup def heikin_ashi(dataframe, smooth_inputs = False, smooth_outputs = False, length = 10): df = dataframe[['open','close','high','low']].copy().fillna(0) if smooth_inputs: df['open_s'] = ta.EMA(df['open'], timeframe = length) df['high_s'] = ta.EMA(df['high'], timeframe = length) df['low_s'] = ta.EMA(df['low'], timeframe = length) df['close_s'] = ta.EMA(df['close'],timeframe = length) open_ha = (df['open_s'].shift(1) + df['close_s'].shift(1)) / 2 high_ha = df.loc[:, ['high_s', 'open_s', 'close_s']].max(axis=1) low_ha = df.loc[:, ['low_s', 'open_s', 'close_s']].min(axis=1) close_ha = (df['open_s'] + df['high_s'] + df['low_s'] + df['close_s'])/4 else: open_ha = (df['open'].shift(1) + df['close'].shift(1)) / 2 high_ha = df.loc[:, ['high', 'open', 'close']].max(axis=1) low_ha = df.loc[:, ['low', 'open', 'close']].min(axis=1) close_ha = (df['open'] + df['high'] + df['low'] + df['close'])/4 open_ha = open_ha.fillna(0) high_ha = high_ha.fillna(0) low_ha = low_ha.fillna(0) close_ha = close_ha.fillna(0) if smooth_outputs: open_sha = ta.EMA(open_ha, timeframe = length) high_sha = ta.EMA(high_ha, timeframe = length) low_sha = ta.EMA(low_ha, timeframe = length) close_sha = ta.EMA(close_ha, timeframe = length) return open_sha, close_sha, low_sha else: return open_ha, close_ha, low_ha def range_percent_change(self, dataframe: DataFrame, method, length: int) -> float: """ Rolling Percentage Change Maximum across interval. :param dataframe: DataFrame The original OHLC dataframe :param method: High to Low / Open to Close :param length: int The length to look back """ if method == 'HL': return (dataframe['high'].rolling(length).max() - dataframe['low'].rolling(length).min()) / dataframe['low'].rolling(length).min() elif method == 'OC': return (dataframe['open'].rolling(length).max() - dataframe['close'].rolling(length).min()) / dataframe['close'].rolling(length).min() else: raise ValueError(f"Method {method} not defined!") def top_percent_change(self, dataframe: DataFrame, length: int) -> float: """ Percentage change of the current close from the range maximum Open price :param dataframe: DataFrame The original OHLC dataframe :param length: int The length to look back """ if length == 0: return (dataframe['open'] - dataframe['close']) / dataframe['close'] else: return (dataframe['open'].rolling(length).max() - dataframe['close']) / dataframe['close']