# # # from datetime import timedelta, datetime from typing import Optional from freqtrade.persistence import Trade from freqtrade.strategy.parameters import CategoricalParameter, DecimalParameter, IntParameter, BooleanParameter from freqtrade.strategy.interface import IStrategy from pandas import DataFrame import logging # -------------------------------- # Add your lib to import here import ta import talib.abstract as talib from freqtrade.strategy.strategy_helper import merge_informative_pair import freqtrade.vendor.qtpylib.indicators as qtpylib logger = logging.getLogger(__name__) class Ishimoku_2(IStrategy): # ROI table: minimal_roi = { "0": 0.564, "567": 0.273, "2814": 0.12, "7675": 0 } # Stoploss: stoploss = -0.256 # Buy hypers timeframe = '4h' stop_buying = False max_open_trades = 5 plot_config = { "main_plot": { "min200": { "color": "#86c932" }, "max50": { "color": "white" }, "max200": { "color": "yellow" }, "sma3_1d": { "color": "pink" }, "sma5_1d": { "color": "blue" }, "sma10_1d": { "color": "orange" }, "close_1d": { "color": "#73e233", }, "bb_lowerband": { "color": "#da59a6"}, "bb_upperband": { "color": "#da59a6", }, "sar": { "color": "#4f9f51", } }, "subplots": { "Ind": { "trend_ichimoku_base": { "color": "#dd1384" }, "trend_kst_diff": { "color": "#850678" } }, "BB": { "bb_width": { "color": "white" }, # "bb_lower_5": { # "color": "yellow" # } }, # "Cond": { # "cond1": { # "color": "yellow" # } # }, "Rsi": { "rsi": { "color": "pink" }, # "rsi_1d": { # "color": "yellow" # } }, # "Percent": { # "max_min": { # "color": "#74effc" # }, # "pct_change_1_1d": { # "color": "green" # }, # "pct_change_3_1d": { # "color": "orange" # }, # "pct_change_5_1d": { # "color": "red" # } # } } } trades = list() buy_base = DecimalParameter(0, 0.2, decimals=2, default=0.05, space='buy') buy_rsi = IntParameter(20, 60, default=45, space='buy') profit_b_no_change = BooleanParameter(default=True, space="sell") profit_b_quick_lost = BooleanParameter(default=True, space="sell") profit_b_sma5 = BooleanParameter(default=True, space="sell") profit_b_sma10 = BooleanParameter(default=True, space="sell") profit_b_sma20 = BooleanParameter(default=True, space="sell") profit_b_quick_gain = BooleanParameter(default=True, space="sell") profit_b_quick_gain_3 = BooleanParameter(default=True, space="sell") profit_b_old_sma10 = BooleanParameter(default=True, space="sell") profit_b_very_old_sma10 = BooleanParameter(default=True, space="sell") profit_b_over_rsi = BooleanParameter(default=True, space="sell") profit_b_short_loss = BooleanParameter(default=True, space="sell") sell_b_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_b_percent3 = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_b_candels = IntParameter(0, 48, default=12, space='sell') sell_b_too_old_day = IntParameter(0, 10, default=5, space='sell') sell_b_too_old_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_b_profit_no_change = DecimalParameter(0, 0.02, decimals=3, default=0.005, space='sell') sell_b_profit_percent10 = DecimalParameter(0, 0.002, decimals=4, default=0.001, space='sell') sell_b_RSI = IntParameter(70, 98, default=88, space='sell') sell_b_RSI2 = IntParameter(70, 98, default=88, space='sell') sell_b_RSI3 = IntParameter(70, 98, default=80, space='sell') sell_b_RSI2_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') # sell_b_expected_profit = DecimalParameter(0, 0.01, decimals=3, default=0.01, space='sell') profit_h_no_change = BooleanParameter(default=True, space="sell") profit_h_quick_lost = BooleanParameter(default=True, space="sell") profit_h_sma5 = BooleanParameter(default=True, space="sell") profit_h_sma10 = BooleanParameter(default=True, space="sell") profit_h_sma20 = BooleanParameter(default=True, space="sell") profit_h_quick_gain = BooleanParameter(default=True, space="sell") profit_h_quick_gain_3 = BooleanParameter(default=True, space="sell") profit_h_old_sma10 = BooleanParameter(default=True, space="sell") profit_h_very_old_sma10 = BooleanParameter(default=True, space="sell") profit_h_over_rsi = BooleanParameter(default=True, space="sell") profit_h_short_loss = BooleanParameter(default=True, space="sell") sell_h_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_h_percent3 = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_h_candels = IntParameter(0, 48, default=12, space='sell') sell_h_too_old_day = IntParameter(0, 10, default=5, space='sell') sell_h_too_old_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') sell_h_profit_no_change = DecimalParameter(0, 0.02, decimals=3, default=0.005, space='sell') sell_h_profit_percent10 = DecimalParameter(0, 0.002, decimals=4, default=0.001, space='sell') sell_h_RSI = IntParameter(70, 98, default=88, space='sell') sell_h_RSI2 = IntParameter(70, 98, default=88, space='sell') sell_h_RSI3 = IntParameter(70, 98, default=80, space='sell') sell_h_RSI2_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell') # protection_max_allowed_dd = DecimalParameter(0, 1, decimals=2, default=0.04, space='protection') # protection_stop = IntParameter(0, 100, default=48, space='protection') # protection_stoploss_stop = IntParameter(0, 100, default=48, space='protection') # lookback = IntParameter(0, 200, default=48, space='protection') # trade_limit = IntParameter(0, 10, default=2, space='protection') protection_down_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='protection') protection_down_percent3 = DecimalParameter(0, 0.05, decimals=2, default=0.02, space='protection') protection_down_percent5 = DecimalParameter(0, 0.05, decimals=2, default=0.03, space='protection') protection_up_percent = DecimalParameter(-0.02, 0.02, decimals=3, default=0.0, space='protection') protection_up_percent3 = DecimalParameter(-0.02, 0.05, decimals=2, default=0.0, space='protection') protection_up_percent5 = DecimalParameter(-0.02, 0.05, decimals=2, default=0.0, space='protection') # def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, # proposed_stake: float, min_stake: float, max_stake: float, # **kwargs) -> float: # # informative, _ = self.dp.get_analyzed_dataframe(pair='BTC/USDT', timeframe=self.timeframe) # # current_candle = informative.iloc[-1].squeeze() # # current = informative.tail(1).iloc[0]['close'] # # 50000 => 2 30000 => 20 # if current > 50000: # self.max_open_trades = 2 # proposed_stake = self.config['stake_amount'] / 2 # else: # if current > 32000: # self.max_open_trades = 2 + int((50000 - current) / 1000) # proposed_stake = self.config['stake_amount'] / 2 \ # + self.config['stake_amount'] * self.max_open_trades / self.config['max_open_trades'] # else: # self.max_open_trades = self.config['max_open_trades'] # proposed_stake = self.config['stake_amount'] # # return min(max_stake, proposed_stake) @property def protections(self): return [ { "method": "CooldownPeriod", "stop_duration_candles": 10 }, # { # "method": "MaxDrawdown", # "lookback_period_candles": self.lookback.value, # "trade_limit": self.trade_limit.value, # "stop_duration_candles": self.protection_stop.value, # "max_allowed_drawdown": self.protection_max_allowed_dd.value, # "only_per_pair": False # } ] def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, current_time: datetime, entry_tag: Optional[str], **kwargs) -> bool: allow_to_buy = True informative, _ = self.dp.get_analyzed_dataframe(pair='BTC/USDT', timeframe=self.timeframe) info_last_candle = informative.iloc[-1].squeeze() if (self.stop_buying is True) & ( (info_last_candle['percent'] > self.protection_up_percent.value) | (info_last_candle['percent3'] > self.protection_up_percent3.value) | (info_last_candle['percent5'] > self.protection_up_percent5.value)): # print("Enable buying") self.stop_buying = False if self.stop_buying: allow_to_buy = False return allow_to_buy 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].squeeze() previous_last_candle = dataframe.iloc[-2].squeeze() previous_5_candle = dataframe.iloc[-5].squeeze() expected_profit = 0.01 days = (current_time - trade.open_date_utc).days ###### informative, _ = self.dp.get_analyzed_dataframe(pair='BTC/USDT', timeframe=self.timeframe) info_last_candle = informative.iloc[-1].squeeze() if self.stop_buying is True: if (info_last_candle['percent'] > 0) | (info_last_candle['percent3'] > 0) | ( info_last_candle['percent5'] > 0): # print("Enable buying") self.stop_buying = False else: if self.stop_buying is False: if (info_last_candle['percent'] < - self.protection_down_percent.value) \ | (info_last_candle['percent3'] < - self.protection_down_percent3.value) \ | (info_last_candle['percent5'] < - self.protection_down_percent5.value): self.stop_buying = True # print("Disable buying", info_last_candle['percent'], info_last_candle['percent3'], info_last_candle['percent5']) # return 'send_all' if (last_candle['pct_change_1_1h'] < 0): if (current_profit > 0.015) & ((last_candle['percent'] < -0.005) | (last_candle['percent3'] < -0.005) | ( last_candle['percent5'] < -0.005)): return 'b_percent_quick' if (current_profit > last_candle['bb_width'] / 2) & (previous_last_candle['close'] > previous_last_candle['bb_upperband'])\ & (last_candle['percent'] < -0.005) & ((current_time - trade.open_date_utc).seconds <= 3600): return 'b_bb_width' if (current_profit >= - self.sell_b_too_old_percent.value) & (days >= self.sell_b_too_old_day.value) \ & (days < self.sell_b_too_old_day.value * 2) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "b_too_old_0.01" if (current_profit >= - self.sell_b_too_old_percent.value * 2) & (days >= self.sell_b_too_old_day.value * 2) \ & (days < self.sell_b_too_old_day.value * 3) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "b_too_old_0.02" if (current_profit >= - self.sell_b_too_old_percent.value * 3) & (days >= self.sell_b_too_old_day.value * 3) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "b_too_old_0.03" if self.profit_b_quick_lost.value and (current_profit >= 0.015) & (last_candle['percent3'] < -0.005): return "b_quick_lost" if self.profit_b_no_change.value and (current_profit > self.sell_b_profit_no_change.value) \ & (last_candle['percent10'] < self.sell_b_profit_percent10.value) & (last_candle['percent5'] < 0) \ & ((current_time - trade.open_date_utc).seconds >= 3600): return "b_no_change" if (current_profit > self.sell_b_percent.value) & (last_candle['percent3'] < - self.sell_b_percent3.value) \ & ((current_time - trade.open_date_utc).seconds <= 300 * self.sell_b_candels.value): return "b_quick_gain_param" if self.profit_b_sma5.value: if (current_profit > expected_profit) \ & ((previous_5_candle['sma5'] > last_candle['sma5']) \ | (last_candle['percent3'] < -expected_profit) | ( last_candle['percent5'] < -expected_profit)) \ & ((last_candle['percent'] < 0) & (last_candle['percent3'] < 0)): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_sma5' if self.profit_b_sma10.value: if (current_profit > expected_profit) \ & ((previous_5_candle['sma10'] > last_candle['sma10']) \ | (last_candle['percent3'] < -expected_profit) | ( last_candle['percent5'] < -expected_profit)) \ & ((last_candle['percent'] < 0) & (last_candle['percent3'] < 0)): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_sma10' if self.profit_b_sma20.value: if (current_profit > 0.005) \ & (previous_last_candle['sma10'] > last_candle['sma10']) \ & ((current_time - trade.open_date_utc).seconds >= 3600) \ & ((previous_last_candle['sma20'] > last_candle['sma20']) & ((last_candle['percent5'] < 0) | (last_candle['percent10'] < 0) | ( last_candle['percent20'] < 0))): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_sma20' if self.profit_b_over_rsi.value: if (current_profit > 0) & (previous_last_candle[ 'rsi'] > self.sell_b_RSI.value): # & (last_candle['percent'] < 0): #| (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_over_rsi' if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_b_RSI2.value) & \ (last_candle[ 'percent'] < - self.sell_b_RSI2_percent.value): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_over_rsi_2' if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_b_RSI3.value) & \ (last_candle['close'] >= last_candle['max200']) & (last_candle[ 'percent'] < - self.sell_b_RSI2_percent.value): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_over_rsi_max' if self.profit_b_short_loss.value: if (current_profit > -expected_profit) & (previous_last_candle['percent10'] > 0.04) & ( last_candle['percent'] < 0) \ & (days >= 1): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'b_short_lost' else: if (current_profit > 0.025) & ((last_candle['percent'] < -0.005) | (last_candle['percent3'] < -0.005) | ( last_candle['percent5'] < -0.005)): return 'h_percent_quick' if (current_profit >= - self.sell_h_too_old_percent.value) & (days >= self.sell_h_too_old_day.value) \ & (days < self.sell_h_too_old_day.value * 2) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "h_too_old_0.01" if (current_profit >= - self.sell_h_too_old_percent.value * 2) & (days >= self.sell_h_too_old_day.value * 2) \ & (days < self.sell_h_too_old_day.value * 3) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "h_too_old_0.02" if (current_profit >= - self.sell_h_too_old_percent.value * 3) & (days >= self.sell_h_too_old_day.value * 3) \ & (previous_last_candle['sma10'] > last_candle['sma10']) & (last_candle['percent3'] < 0): return "h_too_old_0.03" if self.profit_h_quick_lost.value and (current_profit >= 0.015) & (last_candle['percent3'] < -0.005): return "h_quick_lost" if self.profit_h_no_change.value and (current_profit > self.sell_h_profit_no_change.value) \ & (last_candle['percent10'] < self.sell_h_profit_percent10.value) & (last_candle['percent5'] < 0) \ & ((current_time - trade.open_date_utc).seconds >= 3600): return "h_no_change" if (current_profit > self.sell_h_percent.value) & (last_candle['percent3'] < - self.sell_h_percent3.value) \ & ((current_time - trade.open_date_utc).seconds <= 300 * self.sell_h_candels.value): return "h_quick_gain_param" if self.profit_h_sma5.value: if (current_profit > expected_profit) \ & ((previous_5_candle['sma5'] > last_candle['sma5']) \ | (last_candle['percent3'] < -expected_profit) | ( last_candle['percent5'] < -expected_profit)) \ & ((last_candle['percent'] < 0) & (last_candle['percent3'] < 0)): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_sma5' if self.profit_h_sma10.value: if (current_profit > expected_profit) \ & ((previous_5_candle['sma10'] > last_candle['sma10']) \ | (last_candle['percent3'] < -expected_profit) | ( last_candle['percent5'] < -expected_profit)) \ & ((last_candle['percent'] < 0) & (last_candle['percent3'] < 0)): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_sma10' if self.profit_h_sma20.value: if (current_profit > 0.005) \ & (previous_last_candle['sma10'] > last_candle['sma10']) \ & ((current_time - trade.open_date_utc).seconds >= 3600) \ & ((previous_last_candle['sma20'] > last_candle['sma20']) & ((last_candle['percent5'] < 0) | (last_candle['percent10'] < 0) | ( last_candle['percent20'] < 0))): # print("over_bb_band_sma10_desc", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_sma20' if self.profit_h_over_rsi.value: if (current_profit > 0) & (previous_last_candle[ 'rsi'] > self.sell_h_RSI.value): # & (last_candle['percent'] < 0): #| (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_over_rsi' if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_h_RSI2.value) & \ (last_candle[ 'percent'] < - self.sell_h_RSI2_percent.value): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_over_rsi_2' if (current_profit > 0) & (previous_last_candle['rsi'] > self.sell_h_RSI3.value) & \ (last_candle['close'] >= last_candle[ 'max200']): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_over_rsi_max' if self.profit_h_short_loss.value: if (current_profit > -expected_profit) & (previous_last_candle['percent10'] > 0.04) & ( last_candle['percent'] < 0) \ & (days >= 1): # | (previous_last_candle['rsi'] > 75 & last_candle['rsi'] < 70)): # print("over_rsi", pair, trade, " profit=", current_profit, " rate=", current_rate) return 'h_short_lost' def informative_pairs(self): informative_pairs = [('BTC/USDT', '1h')] return informative_pairs def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # Add all ta features dataframe['trend_ichimoku_base'] = ta.trend.ichimoku_base_line( dataframe['high'], dataframe['low'], window1=9, window2=26, visual=False, fillna=False ) KST = ta.trend.KSTIndicator( close=dataframe['close'], roc1=10, roc2=15, roc3=20, roc4=30, window1=10, window2=10, window3=10, window4=15, nsig=9, fillna=False ) dataframe['trend_kst_diff'] = KST.kst_diff() dataframe['rsi'] = talib.RSI(dataframe) dataframe['pct_change'] = dataframe['close'].pct_change(5) dataframe['min10'] = talib.MIN(dataframe['close'], timeperiod=10) dataframe['min20'] = talib.MIN(dataframe['close'], timeperiod=20) dataframe['min50'] = talib.MIN(dataframe['close'], timeperiod=50) dataframe['min200'] = talib.MIN(dataframe['close'], timeperiod=200) dataframe['min'] = talib.MIN(dataframe['close'], timeperiod=200) dataframe['moy200_12'] = dataframe['min200'].rolling(12).mean() dataframe['max50'] = talib.MAX(dataframe['close'], timeperiod=50) dataframe['max200'] = talib.MAX(dataframe['close'], timeperiod=200) dataframe['sma5'] = talib.SMA(dataframe, timeperiod=5) dataframe['sma10'] = talib.SMA(dataframe, timeperiod=10) dataframe['sma20'] = talib.SMA(dataframe, timeperiod=20) dataframe['sma50'] = talib.SMA(dataframe, timeperiod=50) dataframe['sma100'] = talib.SMA(dataframe, timeperiod=100) dataframe["percent"] = (dataframe["close"] - dataframe["open"]) / dataframe["open"] dataframe["percent5"] = dataframe["percent"].rolling(5).sum() dataframe["percent3"] = dataframe["percent"].rolling(3).sum() dataframe["percent10"] = dataframe["percent"].rolling(10).sum() dataframe["percent20"] = dataframe["percent"].rolling(20).sum() dataframe["percent50"] = dataframe["percent"].rolling(50).sum() dataframe["volume10"] = dataframe["volume"].rolling(10).mean() dataframe["volume10"] = dataframe["volume"].rolling(10).mean() # # Bollinger Bands bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) dataframe['bb_lowerband'] = bollinger['lower'] dataframe['bb_middleband'] = bollinger['mid'] dataframe['bb_upperband'] = bollinger['upper'] dataframe["bb_percent"] = ( (dataframe["close"] - dataframe["bb_lowerband"]) / (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) ) dataframe["bb_width"] = ( (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"] ) dataframe['sar'] = talib.SAR(dataframe) # Normalization tib = dataframe['trend_ichimoku_base'] dataframe['trend_ichimoku_base'] = (tib - tib.min()) / (tib.max() - tib.min()) tkd = dataframe['trend_kst_diff'] dataframe['trend_kst_diff'] = (tkd - tkd.min()) / (tkd.max() - tkd.min()) ################### INFORMATIVE BTC 1H informative = self.dp.get_pair_dataframe(pair='BTC/USDT', timeframe="1h") informative["rsi"] = talib.RSI(informative) informative['sma7'] = talib.SMA(informative, timeperiod=7) informative['pct_change_1'] = informative['close'].pct_change(1) bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(informative), window=20, stds=2) informative['bb_lowerband'] = bollinger['lower'] informative['bb_middleband'] = bollinger['mid'] informative['bb_upperband'] = bollinger['upper'] informative["bb_percent"] = ( (informative["close"] - informative["bb_lowerband"]) / (informative["bb_upperband"] - informative["bb_lowerband"]) ) informative["bb_width"] = ( (informative["bb_upperband"] - informative["bb_lowerband"]) / informative["bb_middleband"] ) dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1h", ffill=True) return dataframe def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe.loc[ ( (dataframe['trend_ichimoku_base'] <= self.buy_base.value) & (dataframe['rsi'] < self.buy_rsi.value) #& (dataframe['close'] < dataframe['sma10']) #& (dataframe['sma7_1h'].shift(1) <= dataframe['sma7_1h']) #& (dataframe['close_1h'] <= dataframe['bb_middleband_1h']) ), ['buy', 'buy_tag']] = (1, 'buy_ichimoku') return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: return dataframe