# --- Do not remove these libs --- from logging import FATAL from freqtrade.strategy.interface import IStrategy from typing import Dict, List from functools import reduce from pandas import DataFrame import talib.abstract as ta import numpy as np import freqtrade.vendor.qtpylib.indicators as qtpylib import datetime from technical.util import resample_to_interval, resampled_merge from datetime import datetime, timedelta from freqtrade.persistence import Trade from freqtrade.strategy import stoploss_from_open, stoploss_from_absolute, merge_informative_pair, DecimalParameter, IntParameter, CategoricalParameter import technical.indicators as ftt # @Rallipanos # @pluxury # with help from @stash86 and @Perkmeister # Buy hyperspace params: buy_params = { "low_offset": 0.983, "base_nb_candles_buy": 8, "ewo_high": 3.553, "ewo_high_2": -5.585, "ewo_low": -14.378, "lookback_candles": 32, "low_offset_2": 0.942, "profit_threshold": 1.037, "rsi_buy": 78, "rsi_fast_buy": 37, } # Sell hyperspace params: sell_params = { "base_nb_candles_sell": 16, "high_offset": 1.069, "high_offset_2": 1.472, } def EWO(dataframe, ema_length=5, ema2_length=35): df = dataframe.copy() ema1 = ta.EMA(df, timeperiod=ema_length) ema2 = ta.EMA(df, timeperiod=ema2_length) emadif = (ema1 - ema2) / df['low'] * 100 return emadif class NASOSv5(IStrategy): INTERFACE_VERSION = 3 minimal_roi = { "360": 0 } stoploss = -0.08 # MAE data: 95% of winners recovered from -5.29% or less @property def protections(self): return [ {"method": "CooldownPeriod", "stop_duration_candles": 5}, {"method": "StoplossGuard", "lookback_period_candles": 24, "trade_limit": 3, "stop_duration_candles": 12, "only_per_pair": True}, {"method": "LowProfitPairs", "lookback_period_candles": 72, "trade_limit": 2, "stop_duration_candles": 72, "required_profit": -0.02}, {"method": "MaxDrawdown", "lookback_period_candles": 576, "max_allowed_drawdown": 0.20, "stop_duration_candles": 72, "trade_limit": 1}, ] # SMAOffset base_nb_candles_buy = IntParameter( 2, 20, default=buy_params['base_nb_candles_buy'], space='buy', optimize=False) base_nb_candles_sell = IntParameter( 2, 25, default=sell_params['base_nb_candles_sell'], space='sell', optimize=False) low_offset = DecimalParameter( 0.9, 0.99, default=buy_params['low_offset'], space='buy', optimize=True) low_offset_2 = DecimalParameter( 0.9, 0.99, default=buy_params['low_offset_2'], space='buy', optimize=False) high_offset = DecimalParameter( 0.95, 1.1, default=sell_params['high_offset'], space='sell', optimize=True) high_offset_2 = DecimalParameter( 0.99, 1.5, default=sell_params['high_offset_2'], space='sell', optimize=False) # Protection fast_ewo = 50 slow_ewo = 200 lookback_candles = IntParameter( 1, 36, default=buy_params['lookback_candles'], space='buy', optimize=False) profit_threshold = DecimalParameter(0.99, 1.05, default=buy_params['profit_threshold'], space='buy', optimize=False) ewo_low = DecimalParameter(-20.0, -8.0, default=buy_params['ewo_low'], space='buy', optimize=False) ewo_high = DecimalParameter( 2.0, 12.0, default=buy_params['ewo_high'], space='buy', optimize=False) ewo_high_2 = DecimalParameter( -6.0, 12.0, default=buy_params['ewo_high_2'], space='buy', optimize=False) rsi_buy = IntParameter(10, 80, default=buy_params['rsi_buy'], space='buy', optimize=False) rsi_fast_buy = IntParameter( 10, 50, default=buy_params['rsi_fast_buy'], space='buy', optimize=False) # Trailing stop: trailing_stop = False # Exit signal use_exit_signal = True exit_profit_only = False exit_profit_offset = 0.01 ignore_roi_if_entry_signal = False order_time_in_force = { 'entry': 'GTC', 'exit': 'IOC' } timeframe = '5m' inf_15m = '15m' inf_1h = '1h' process_only_new_candles = True startup_candle_count = 200 use_custom_stoploss = True slippage_protection = { 'retries': 3, 'max_slippage': -0.02 } def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float, current_profit: float, **kwargs) -> float: # --- Time-based exit tightening (5m strategy: 93% of winners close in <50min) --- trade_duration = (current_time - trade.open_date_utc).total_seconds() / 3600 if trade_duration > 8: return -0.001 # Force close after 8h elif trade_duration > 4: time_sl = stoploss_from_open(-0.03, current_profit) if time_sl != -0.99: return time_sl else: return -0.001 # already past -3%, force close now elif trade_duration > 2: atr_sl = self._atr_stoploss(pair, current_rate, multiplier=2.0) if atr_sl is not None: return atr_sl # --- Original tiered profit-lock --- if (current_profit > 0.3): return 0.05 elif (current_profit > 0.1): return 0.03 elif (current_profit > 0.06): return 0.02 elif (current_profit > 0.04): return 0.01 elif (current_profit > 0.025): return 0.005 elif (current_profit > 0.018): return 0.005 return -1 # Defer to strategy-level stoploss (-8%) def _atr_stoploss(self, pair: str, current_rate: float, multiplier: float = 3.0): """ATR-based dynamic stoploss.""" import numpy as np dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe) if len(dataframe) < 1: return None atr = dataframe.iloc[-1]['regime_atr_14'] if np.isnan(atr) or atr <= 0: return None stoploss_price = current_rate - (atr * multiplier) return stoploss_from_absolute(stoploss_price, current_rate=current_rate, is_short=False, leverage=1.0) def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float, rate: float, time_in_force: str, exit_reason: str, current_time: datetime, **kwargs) -> bool: dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe) last_candle = dataframe.iloc[-1] if (last_candle is not None): if (exit_reason in ['exit_signal']): if (last_candle['hma_50']*1.149 > last_candle['ema_100']) and (last_candle['close'] < last_candle['ema_100']*0.951): return False # slippage protection try: state = self.slippage_protection['__pair_retries'] except KeyError: state = self.slippage_protection['__pair_retries'] = {} candle = dataframe.iloc[-1].squeeze() slippage = (rate / candle['close']) - 1 if slippage < self.slippage_protection['max_slippage']: pair_retries = state.get(pair, 0) if pair_retries < self.slippage_protection['retries']: state[pair] = pair_retries + 1 return False state[pair] = 0 return True def informative_pairs(self): pairs = self.dp.current_whitelist() informative_pairs = [(pair, '15m') for pair in pairs] return informative_pairs def informative_15m_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: assert self.dp, "DataProvider is required for multiple timeframes." informative_15m = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=self.inf_15m) return informative_15m def normal_tf_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: for val in self.base_nb_candles_buy.range: dataframe[f'ma_buy_{val}'] = ta.EMA(dataframe, timeperiod=val) for val in self.base_nb_candles_sell.range: dataframe[f'ma_sell_{val}'] = ta.EMA(dataframe, timeperiod=val) dataframe['hma_50'] = qtpylib.hull_moving_average(dataframe['close'], window=50) dataframe['ema_100'] = ta.EMA(dataframe, timeperiod=100) dataframe['sma_9'] = ta.SMA(dataframe, timeperiod=9) dataframe['EWO'] = EWO(dataframe, self.fast_ewo, self.slow_ewo) dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14) dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4) dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20) # --- Regime Detection --- # ATR regime — detect crash/high volatility periods dataframe['regime_atr_14'] = ta.ATR(dataframe, timeperiod=14) dataframe['regime_atr_sma_50'] = dataframe['regime_atr_14'].rolling(50).mean() dataframe['regime_volatile'] = (dataframe['regime_atr_14'] > 2.0 * dataframe['regime_atr_sma_50']).astype(int) # ADX regime — detect trending vs ranging dataframe['regime_adx_14'] = ta.ADX(dataframe, timeperiod=14) dataframe['regime_trending'] = (dataframe['regime_adx_14'] > 35).astype(int) return dataframe def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: informative_15m = self.informative_15m_indicators(dataframe, metadata) dataframe = merge_informative_pair( dataframe, informative_15m, self.timeframe, self.inf_15m, ffill=True) dataframe = self.normal_tf_indicators(dataframe, metadata) return dataframe def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dont_buy_conditions = [] dont_buy_conditions.append( ( (dataframe['close_15m'].rolling(self.lookback_candles.value).max() < (dataframe['close'] * self.profit_threshold.value)) ) ) dataframe.loc[ ( (dataframe['rsi_fast'] < self.rsi_fast_buy.value) & (dataframe['close'] < (dataframe[f'ma_buy_{self.base_nb_candles_buy.value}'] * self.low_offset.value)) & (dataframe['EWO'] > self.ewo_high.value) & (dataframe['rsi'] < self.rsi_buy.value) & (dataframe['volume'] > 0) & (dataframe['close'] < ( dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset.value)) & (dataframe['regime_volatile'] == 0) & (dataframe['regime_trending'] == 0) ), ['enter_long', 'enter_tag']] = (1, 'ewo1') dataframe.loc[ ( (dataframe['rsi_fast'] < self.rsi_fast_buy.value) & (dataframe['close'] < (dataframe[f'ma_buy_{self.base_nb_candles_buy.value}'] * self.low_offset_2.value)) & (dataframe['EWO'] > self.ewo_high_2.value) & (dataframe['rsi'] < self.rsi_buy.value) & (dataframe['volume'] > 0) & (dataframe['close'] < (dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset.value)) & (dataframe['rsi'] < 25) & (dataframe['regime_volatile'] == 0) & (dataframe['regime_trending'] == 0) ), ['enter_long', 'enter_tag']] = (1, 'ewo2') dataframe.loc[ ( (dataframe['rsi_fast'] < self.rsi_fast_buy.value) & (dataframe['close'] < (dataframe[f'ma_buy_{self.base_nb_candles_buy.value}'] * self.low_offset.value)) & (dataframe['EWO'] < self.ewo_low.value) & (dataframe['volume'] > 0) & (dataframe['close'] < ( dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset.value)) & (dataframe['regime_volatile'] == 0) & (dataframe['regime_trending'] == 0) ), ['enter_long', 'enter_tag']] = (1, 'ewolow') if dont_buy_conditions: for condition in dont_buy_conditions: dataframe.loc[condition, 'enter_long'] = 0 return dataframe def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: conditions = [] conditions.append( ((dataframe['close'] > dataframe['sma_9']) & (dataframe['close'] > (dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset_2.value)) & (dataframe['rsi'] > 50) & (dataframe['volume'] > 0) & (dataframe['rsi_fast'] > dataframe['rsi_slow']) ) | ( (dataframe['close'] < dataframe['hma_50']) & (dataframe['close'] > (dataframe[f'ma_sell_{self.base_nb_candles_sell.value}'] * self.high_offset.value)) & (dataframe['volume'] > 0) & (dataframe['rsi_fast'] > dataframe['rsi_slow']) ) ) if conditions: dataframe.loc[ reduce(lambda x, y: x | y, conditions), 'exit_long' ]=1 return dataframe