# source: https://raw.githubusercontent.com/DerSalvador/freqtrade-helm-chart/a669dc11b640b0eb63aa8f8b51e9f181fd7ee43c/chart/deployed_strategies/bot-ssc-04-k8s-namespace/BBMod.py%20GodStraNew_SMAonly.py
kubectl --context=gke_vaulted-gift-406223_europe-west1-b_private-cluster-3 -n bot-ssc-04 exec -it pod/freqtrade-bot-ssc-04-8499ff6998-p5khc -c freqtrade -- cat /freqtrade/user_data/strategies/Github_DerSalvador_freqtrade_helm_chart__BBMod_pyGodStraNew_SMAonly__20260411_075128.py GodStraNew_SMAonly.py
# --- Do not remove these libs ---
from typing import Optional
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy as np
import talib.abstract as ta
import pandas_ta as pta
from freqtrade.persistence import Trade
from freqtrade.strategy.interface import IStrategy
from pandas import DataFrame, Series
from datetime import datetime
from freqtrade.strategy import merge_informative_pair, DecimalParameter, IntParameter, stoploss_from_open
from functools import reduce
from technical.indicators import RMI, zema, VIDYA
TMP_HOLD = []
# --------------------------------

def ha_typical_price(bars):
    res = (bars['ha_high'] + bars['ha_low'] + bars['ha_close']) / 3.0
    return Series(index=bars.index, data=res)
# Volume Weighted Moving Average

def vwma(dataframe: DataFrame, length: int=10):
    """Indicator: Volume Weighted Moving Average (VWMA)"""
    # Calculate Result
    pv = dataframe['close'] * dataframe['volume']
    vwma = Series(ta.SMA(pv, timeperiod=length) / ta.SMA(dataframe['volume'], timeperiod=length))
    return vwma
# Modified Elder Ray Index

def moderi(dataframe: DataFrame, len_slow_ma: int=32) -> Series:
    slow_ma = Series(ta.EMA(vwma(dataframe, length=len_slow_ma), timeperiod=len_slow_ma))
    return slow_ma >= slow_ma.shift(1)  # we just need true & false for ERI trend

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

def range_percent_change(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!')
# Williams %R

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
# Chaikin Money Flow

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 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 vmap_b(dataframe, window_size=20, num_of_std=1):
    df = dataframe.copy()
    df['vwap'] = qtpylib.rolling_vwap(df, window=window_size)
    rolling_std = df['vwap'].rolling(window=window_size).std()
    df['vwap_low'] = df['vwap'] - rolling_std * num_of_std
    df['vwap_high'] = df['vwap'] + rolling_std * num_of_std
    return (df['vwap_low'], df['vwap'], df['vwap_high'])

def top_percent_change(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']

class Github_DerSalvador_freqtrade_helm_chart__BBMod_pyGodStraNew_SMAonly__20260411_075128(IStrategy):
    INTERFACE_VERSION = 3
    '\n        Github_DerSalvador_freqtrade_helm_chart__BBMod_pyGodStraNew_SMAonly__20260411_0751281 modified from BB_RPB_TSL ( https://github.com/jilv220/BB_RPB_TSL )\n        @author jilv220\n        Simple bollinger brand strategy inspired by this blog  ( https://hacks-for-life.blogspot.com/2020/12/freqtrade\n        -notes.html )RPB, which stands for Real Pull Back, taken from ( https://github.com/GeorgeMurAlkh/freqtrade-stuff\n        /blob/main/user_data/strategies/TheRealPullbackV2.py )The trailing custom stoploss taken from BigZ04_TSL from\n        Perkmeister ( modded by ilya )I modified it to better suit my taste and added Hyperopt for this strategy.\n    '
    # entry space
    ##
    ##
    #
    ##
    ##
    # rng
    # rng
    ##
    ##
    ##
    ##
    ##
    ##
    ##
    ##
    ##
    ##
    entry_params = {'entry_bb_width_1h': 0.954, 'entry_roc_1h': 86, 'entry_threshold': 0.003, 'entry_bb_factor': 0.999, 'entry_bb_delta': 0.025, 'entry_bb_width': 0.095, 'entry_cci': -116, 'entry_cci_length': 25, 'entry_rmi': 49, 'entry_rmi_length': 17, 'entry_srsi_fk': 32, 'entry_closedelta': 12.148, 'entry_ema_diff': 0.022, 'entry_ema_high': 0.968, 'entry_ema_low': 0.935, 'entry_ewo': -5.001, 'entry_rsi': 23, 'entry_rsi_fast': 44, 'entry_ema_high_2': 1.087, 'entry_ema_low_2': 0.97, 'entry_ewo_high_2': 4.179, 'entry_rsi_ewo_2': 35, 'entry_rsi_fast_ewo_2': 45, 'entry_closedelta_local_dip': 12.044, 'entry_ema_diff_local_dip': 0.024, 'entry_ema_high_local_dip': 1.014, 'entry_rsi_local_dip': 21, 'entry_r_deadfish_bb_factor': 1.014, 'entry_r_deadfish_bb_width': 0.299, 'entry_r_deadfish_ema': 1.054, 'entry_r_deadfish_volume_factor': 1.59, 'entry_r_deadfish_cti': -0.115, 'entry_r_deadfish_r14': -44.34, 'entry_clucha_bbdelta_close': 0.049, 'entry_clucha_bbdelta_tail': 1.146, 'entry_clucha_close_bblower': 0.018, 'entry_clucha_closedelta_close': 0.017, 'entry_clucha_rocr_1h': 0.526, 'entry_adx': 13, 'entry_cofi_r14': -85.016, 'entry_cofi_cti': -0.892, 'entry_ema_cofi': 1.147, 'entry_ewo_high': 8.594, 'entry_fastd': 28, 'entry_fastk': 39, 'entry_gumbo_ema': 1.121, 'entry_gumbo_ewo_low': -9.442, 'entry_gumbo_cti': -0.374, 'entry_gumbo_r14': -51.971, 'entry_sqzmom_ema': 0.981, 'entry_sqzmom_ewo': -3.966, 'entry_sqzmom_r14': -45.068, 'entry_nfix_39_ema': 0.912, 'entry_nfix_49_cti': -0.105, 'entry_nfix_49_r14': -81.827}
    # exit space
    ##
    ##
    ##
    exit_params = {'exit_cmf': -0.046, 'exit_ema': 0.988, 'exit_ema_close_delta': 0.022, 'exit_deadfish_profit': -0.063, 'exit_deadfish_bb_factor': 0.954, 'exit_deadfish_bb_width': 0.043, 'exit_deadfish_volume_factor': 2.37, 'exit_cti_r_cti': 0.844, 'exit_cti_r_r': -19.99, 'high_offset_2': 0.997, 'pHSL': -0.18, 'pPF_1': 0.019, 'pPF_2': 0.05, 'pSL_1': 0.017, 'pSL_2': 0.045}
    minimal_roi = {'0': 100}
    # Optimal timeframe for the strategy
    timeframe = '5m'
    inf_1h = '1h'
    # Run "populate_indicators()" only for new candle.
    process_only_new_candles = True
    startup_candle_count = 120
    order_types = {'entry': 'limit', 'exit': 'limit', 'emergencyexit': 'limit', 'forceentry': 'limit', 'forceexit': 'limit', 'stoploss': 'limit', 'stoploss_on_exchange': False, 'stoploss_on_exchange_interval': 60, 'stoploss_on_exchange_limit_ratio': 0.99}
    # Disabled
    stoploss = -0.99
    # Custom stoploss
    use_custom_stoploss = True
    use_exit_signal = True
    # Buy params
    is_optimize_dip = False
    entry_rmi = IntParameter(30, 50, default=35, optimize=is_optimize_dip)
    entry_cci = IntParameter(-135, -90, default=-133, optimize=is_optimize_dip)
    entry_srsi_fk = IntParameter(30, 50, default=25, optimize=is_optimize_dip)
    entry_cci_length = IntParameter(25, 45, default=25, optimize=is_optimize_dip)
    entry_rmi_length = IntParameter(8, 20, default=8, optimize=is_optimize_dip)
    is_optimize_break = False
    entry_bb_width = DecimalParameter(0.065, 0.135, default=0.095, optimize=is_optimize_break)
    entry_bb_delta = DecimalParameter(0.018, 0.035, default=0.025, optimize=is_optimize_break)
    is_optimize_local_uptrend = False
    entry_ema_diff = DecimalParameter(0.022, 0.027, default=0.025, optimize=is_optimize_local_uptrend)
    entry_bb_factor = DecimalParameter(0.99, 0.999, default=0.995, optimize=False)
    entry_closedelta = DecimalParameter(12.0, 18.0, default=15.0, optimize=is_optimize_local_uptrend)
    is_optimize_local_dip = False
    entry_ema_diff_local_dip = DecimalParameter(0.022, 0.027, default=0.025, optimize=is_optimize_local_dip)
    entry_ema_high_local_dip = DecimalParameter(0.9, 1.2, default=0.942, optimize=is_optimize_local_dip)
    entry_closedelta_local_dip = DecimalParameter(12.0, 18.0, default=15.0, optimize=is_optimize_local_dip)
    entry_rsi_local_dip = IntParameter(15, 45, default=28, optimize=is_optimize_local_dip)
    entry_crsi_local_dip = IntParameter(10, 18, default=10, optimize=False)
    is_optimize_ewo = False
    entry_rsi_fast = IntParameter(35, 50, default=45, optimize=is_optimize_ewo)
    entry_rsi = IntParameter(15, 35, default=35, optimize=is_optimize_ewo)
    entry_ewo = DecimalParameter(-6.0, 5, default=-5.585, optimize=is_optimize_ewo)
    entry_ema_low = DecimalParameter(0.9, 0.99, default=0.942, optimize=is_optimize_ewo)
    entry_ema_high = DecimalParameter(0.95, 1.2, default=1.084, optimize=is_optimize_ewo)
    is_optimize_ewo_2 = False
    entry_rsi_fast_ewo_2 = IntParameter(15, 50, default=45, optimize=is_optimize_ewo_2)
    entry_rsi_ewo_2 = IntParameter(15, 50, default=35, optimize=is_optimize_ewo_2)
    entry_ema_low_2 = DecimalParameter(0.9, 1.2, default=0.97, optimize=is_optimize_ewo_2)
    entry_ema_high_2 = DecimalParameter(0.9, 1.2, default=1.087, optimize=is_optimize_ewo_2)
    entry_ewo_high_2 = DecimalParameter(2, 12, default=4.179, optimize=is_optimize_ewo_2)
    is_optimize_r_deadfish = False
    entry_r_deadfish_ema = DecimalParameter(0.9, 1.2, default=1.087, optimize=is_optimize_r_deadfish)
    entry_r_deadfish_bb_width = DecimalParameter(0.03, 0.75, default=0.05, optimize=is_optimize_r_deadfish)
    entry_r_deadfish_bb_factor = DecimalParameter(0.9, 1.2, default=1.0, optimize=is_optimize_r_deadfish)
    entry_r_deadfish_volume_factor = DecimalParameter(1, 2.5, default=1.0, optimize=is_optimize_r_deadfish)
    is_optimize_r_deadfish_protection = False
    entry_r_deadfish_cti = DecimalParameter(-0.6, -0.0, default=-0.5, optimize=is_optimize_r_deadfish_protection)
    entry_r_deadfish_r14 = DecimalParameter(-60, -44, default=-60, optimize=is_optimize_r_deadfish_protection)
    is_optimize_clucha = False
    entry_clucha_bbdelta_close = DecimalParameter(0.01, 0.05, default=0.02206, optimize=is_optimize_clucha)
    entry_clucha_bbdelta_tail = DecimalParameter(0.7, 1.2, default=1.02515, optimize=is_optimize_clucha)
    entry_clucha_closedelta_close = DecimalParameter(0.001, 0.05, default=0.04401, optimize=is_optimize_clucha)
    entry_clucha_rocr_1h = DecimalParameter(0.1, 1.0, default=0.47782, optimize=is_optimize_clucha)
    entry_clucha_close_bblower = DecimalParameter(0.0005, 0.02, default=0.00799, optimize=is_optimize_clucha)
    is_optimize_cofi = False
    entry_ema_cofi = DecimalParameter(0.94, 1.2, default=0.97, optimize=is_optimize_cofi)
    entry_fastk = IntParameter(0, 40, default=20, optimize=is_optimize_cofi)
    entry_fastd = IntParameter(0, 40, default=20, optimize=is_optimize_cofi)
    entry_adx = IntParameter(0, 30, default=30, optimize=is_optimize_cofi)
    entry_ewo_high = DecimalParameter(2, 12, default=3.553, optimize=is_optimize_cofi)
    is_optimize_cofi_protection = False
    entry_cofi_cti = DecimalParameter(-0.9, -0.0, default=-0.5, optimize=is_optimize_cofi_protection)
    entry_cofi_r14 = DecimalParameter(-100, -44, default=-60, optimize=is_optimize_cofi_protection)
    is_optimize_gumbo = False
    entry_gumbo_ema = DecimalParameter(0.9, 1.2, default=0.97, optimize=is_optimize_gumbo)
    entry_gumbo_ewo_low = DecimalParameter(-12.0, 5, default=-5.585, optimize=is_optimize_gumbo)
    is_optimize_gumbo_protection = False
    entry_gumbo_cti = DecimalParameter(-0.9, -0.0, default=-0.5, optimize=is_optimize_gumbo_protection)
    entry_gumbo_r14 = DecimalParameter(-100, -44, default=-60, optimize=is_optimize_gumbo_protection)
    is_optimize_sqzmom_protection = False
    entry_sqzmom_ema = DecimalParameter(0.9, 1.2, default=0.97, optimize=is_optimize_sqzmom_protection)
    entry_sqzmom_ewo = DecimalParameter(-12, 12, default=0, optimize=is_optimize_sqzmom_protection)
    entry_sqzmom_r14 = DecimalParameter(-100, -22, default=-50, optimize=is_optimize_sqzmom_protection)
    is_optimize_nfix_39 = True
    entry_nfix_39_ema = DecimalParameter(0.9, 1.2, default=0.97, optimize=is_optimize_nfix_39)
    is_optimize_nfix_49_protection = False
    entry_nfix_49_cti = DecimalParameter(-0.9, -0.0, default=-0.5, optimize=is_optimize_nfix_49_protection)
    entry_nfix_49_r14 = DecimalParameter(-100, -44, default=-60, optimize=is_optimize_nfix_49_protection)
    is_optimize_btc_safe = False
    entry_btc_safe = IntParameter(-300, 50, default=-200, optimize=is_optimize_btc_safe)
    entry_btc_safe_1d = DecimalParameter(-0.075, -0.025, default=-0.05, optimize=is_optimize_btc_safe)
    entry_threshold = DecimalParameter(0.003, 0.012, default=0.008, optimize=is_optimize_btc_safe)
    is_optimize_check = False
    entry_roc_1h = IntParameter(-25, 200, default=10, optimize=is_optimize_check)
    entry_bb_width_1h = DecimalParameter(0.3, 2.0, default=0.3, optimize=is_optimize_check)
    # Sell params
    exit_btc_safe = IntParameter(-400, -300, default=-365, optimize=False)
    is_optimize_exit_stoploss = False
    exit_cmf = DecimalParameter(-0.4, 0.0, default=0.0, optimize=is_optimize_exit_stoploss)
    exit_ema_close_delta = DecimalParameter(0.022, 0.027, default=0.024, optimize=is_optimize_exit_stoploss)
    exit_ema = DecimalParameter(0.97, 0.99, default=0.987, optimize=is_optimize_exit_stoploss)
    is_optimize_deadfish = False
    exit_deadfish_bb_width = DecimalParameter(0.03, 0.75, default=0.05, optimize=is_optimize_deadfish)
    exit_deadfish_profit = DecimalParameter(-0.15, -0.05, default=-0.05, optimize=is_optimize_deadfish)
    exit_deadfish_bb_factor = DecimalParameter(0.9, 1.2, default=1.0, optimize=is_optimize_deadfish)
    exit_deadfish_volume_factor = DecimalParameter(1, 2.5, default=1.0, optimize=is_optimize_deadfish)
    is_optimize_bleeding = False
    exit_bleeding_cti = DecimalParameter(-0.9, -0.0, default=-0.5, optimize=is_optimize_bleeding)
    exit_bleeding_r14 = DecimalParameter(-100, -44, default=-60, optimize=is_optimize_bleeding)
    exit_bleeding_volume_factor = DecimalParameter(1, 2.5, default=1.0, optimize=is_optimize_bleeding)
    is_optimize_cti_r = False
    exit_cti_r_cti = DecimalParameter(0.55, 1, default=0.5, optimize=is_optimize_cti_r)
    exit_cti_r_r = DecimalParameter(-15, 0, default=-20, optimize=is_optimize_cti_r)
    # rng exit
    high_offset_2 = DecimalParameter(0.99, 1.5, default=exit_params['high_offset_2'], space='exit', optimize=True)
    exit_fisher = DecimalParameter(0.1, 0.5, default=0.38414, space='exit', optimize=True)
    exit_bbmiddle_close = DecimalParameter(0.97, 1.1, default=1.07634, space='exit', optimize=True)
    # hard stoploss profit
    pHSL = DecimalParameter(-0.2, -0.04, default=-0.08, decimals=3, space='exit', load=True)
    # profit threshold 1, trigger point, SL_1 is used
    pPF_1 = DecimalParameter(0.008, 0.02, default=0.016, decimals=3, space='exit', load=True)
    pSL_1 = DecimalParameter(0.008, 0.02, default=0.011, decimals=3, space='exit', load=True)
    # profit threshold 2, SL_2 is used
    pPF_2 = DecimalParameter(0.04, 0.1, default=0.08, decimals=3, space='exit', load=True)
    pSL_2 = DecimalParameter(0.02, 0.07, default=0.04, decimals=3, space='exit', load=True)
    ############################################################################

    def informative_pairs(self):
        pairs = self.dp.current_whitelist()
        informative_pairs = [(pair, '1h') for pair in pairs]
        return informative_pairs

    def informative_1h_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        assert self.dp, 'DataProvider is required for multiple timeframes.'
        # Get the informative pair
        informative_1h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=self.inf_1h)
        # EMA
        informative_1h['ema_8'] = ta.EMA(informative_1h, timeperiod=8)
        informative_1h['ema_50'] = ta.EMA(informative_1h, timeperiod=50)
        informative_1h['ema_100'] = ta.EMA(informative_1h, timeperiod=100)
        informative_1h['ema_200'] = ta.EMA(informative_1h, timeperiod=200)
        # CTI
        informative_1h['cti'] = pta.cti(informative_1h['close'], length=20)
        informative_1h['cti_40'] = pta.cti(informative_1h['close'], length=40)
        # CRSI (3, 2, 100)
        crsi_closechange = informative_1h['close'] / informative_1h['close'].shift(1)
        crsi_updown = np.where(crsi_closechange.gt(1), 1.0, np.where(crsi_closechange.lt(1), -1.0, 0.0))
        informative_1h['crsi'] = (ta.RSI(informative_1h['close'], timeperiod=3) + ta.RSI(crsi_updown, timeperiod=2) + ta.ROC(informative_1h['close'], 100)) / 3
        # Williams %R
        informative_1h['r_96'] = williams_r(informative_1h, period=96)
        informative_1h['r_480'] = williams_r(informative_1h, period=480)
        # Bollinger bands
        bollinger2 = qtpylib.bollinger_bands(qtpylib.typical_price(informative_1h), window=20, stds=2)
        informative_1h['bb_lowerband2'] = bollinger2['lower']
        informative_1h['bb_middleband2'] = bollinger2['mid']
        informative_1h['bb_upperband2'] = bollinger2['upper']
        informative_1h['bb_width'] = (informative_1h['bb_upperband2'] - informative_1h['bb_lowerband2']) / informative_1h['bb_middleband2']
        # ROC
        informative_1h['roc'] = ta.ROC(dataframe, timeperiod=9)
        # MOMDIV
        mom = momdiv(informative_1h)
        informative_1h['momdiv_entry'] = mom['momdiv_entry']
        informative_1h['momdiv_exit'] = mom['momdiv_exit']
        informative_1h['momdiv_coh'] = mom['momdiv_coh']
        informative_1h['momdiv_col'] = mom['momdiv_col']
        # RSI
        informative_1h['rsi'] = ta.RSI(informative_1h, timeperiod=14)
        # CMF
        informative_1h['cmf'] = chaikin_money_flow(informative_1h, 20)
        # Heikin Ashi
        inf_heikinashi = qtpylib.heikinashi(informative_1h)
        informative_1h['ha_close'] = inf_heikinashi['close']
        informative_1h['rocr'] = ta.ROCR(informative_1h['ha_close'], timeperiod=168)
        # T3 Average
        informative_1h['T3'] = t3(informative_1h)
        # Elliot
        informative_1h['EWO'] = EWO(informative_1h, 50, 200)
        # nfi 37
        informative_1h['hl_pct_change_5'] = range_percent_change(informative_1h, 'HL', 5)
        informative_1h['low_5'] = informative_1h['low'].shift().rolling(5).min()
        informative_1h['safe_dump_50'] = (informative_1h['hl_pct_change_5'] < 0.66) | (informative_1h['close'] < informative_1h['low_5']) | (informative_1h['close'] > informative_1h['open'])
        return informative_1h

    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float, current_profit: float, **kwargs) -> float:
        # hard stoploss profit
        stoploss = self.pHSL.value
        pf_1 = self.pPF_1.value
        sl_1 = self.pSL_1.value
        pf_2 = self.pPF_2.value
        sl_2 = self.pSL_2.value
        # For profits between PF_1 and PF_2 the stoploss (sl_profit) used is linearly interpolated
        # between the values of SL_1 and SL_2. For all profits above PL_2 the sl_profit value
        # rises linearly with current profit, for profits below PF_1 the hard stoploss profit is used.
        if current_profit >= pf_2:
            sl_profit = sl_2 + (current_profit - pf_2)
        elif current_profit >= pf_1:
            sl_profit = sl_1 + (current_profit - pf_1) * (sl_2 - sl_1) / (pf_2 - pf_1)
        else:
            sl_profit = stoploss
        # Only for hyperopt invalid return
        if sl_profit >= current_profit:
            return -0.99
        return stoploss_from_open(sl_profit, current_profit)

    def custom_exit(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()
        if current_profit >= 0.019:
            return None
        if current_profit < 0.019 and last_candle['close'] > last_candle['sma_9'] and (last_candle['close'] > last_candle['ema_24'] * self.high_offset_2.value) and (last_candle['rsi'] > 50) and (last_candle['rsi_fast'] > last_candle['rsi_slow']):
            if last_candle['close'] > last_candle['bb_middleband2']:
                if trade.id not in TMP_HOLD:
                    TMP_HOLD.append(trade.id)
                    return None
        # start cross under bb mid to exit
        for i in TMP_HOLD:
            if trade.id == i and last_candle['close'] < last_candle['bb_middleband2']:
                TMP_HOLD.remove(i)
                return 'exit_drop_bb_mid'
    ############################################################################

    def normal_tf_indicators(self, dataframe: DataFrame) -> DataFrame:
        # Bollinger bands
        bollinger2 = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
        dataframe['bb_lowerband2'] = bollinger2['lower']
        dataframe['bb_middleband2'] = bollinger2['mid']
        dataframe['bb_upperband2'] = bollinger2['upper']
        bollinger3 = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=3)
        dataframe['bb_lowerband3'] = bollinger3['lower']
        dataframe['bb_middleband3'] = bollinger3['mid']
        dataframe['bb_upperband3'] = bollinger3['upper']
        # Other BB checks
        dataframe['bb_width'] = (dataframe['bb_upperband2'] - dataframe['bb_lowerband2']) / dataframe['bb_middleband2']
        dataframe['bb_delta'] = (dataframe['bb_lowerband2'] - dataframe['bb_lowerband3']) / dataframe['bb_lowerband2']
        # CCI hyperopt
        for val in self.entry_cci_length.range:
            dataframe[f'cci_length_{val}'] = ta.CCI(dataframe, val)
        dataframe['cci'] = ta.CCI(dataframe, 26)
        dataframe['cci_long'] = ta.CCI(dataframe, 170)
        # RMI hyperopt
        for val in self.entry_rmi_length.range:
            dataframe[f'rmi_length_{val}'] = RMI(dataframe, length=val, mom=4)
        # SRSI hyperopt
        stoch = ta.STOCHRSI(dataframe, 15, 20, 2, 2)
        dataframe['srsi_fk'] = stoch['fastk']
        dataframe['srsi_fd'] = stoch['fastd']
        # BinH
        dataframe['closedelta'] = (dataframe['close'] - dataframe['close'].shift()).abs()
        dataframe['hma_50'] = qtpylib.hull_moving_average(dataframe['close'], window=50)
        # SMA
        dataframe['sma_9'] = ta.SMA(dataframe, timeperiod=9)
        dataframe['sma_15'] = ta.SMA(dataframe, timeperiod=15)
        dataframe['sma_20'] = ta.SMA(dataframe, timeperiod=20)
        dataframe['sma_21'] = ta.SMA(dataframe, timeperiod=21)
        dataframe['sma_28'] = ta.SMA(dataframe, timeperiod=28)
        dataframe['sma_30'] = ta.SMA(dataframe, timeperiod=30)
        dataframe['sma_75'] = ta.SMA(dataframe, timeperiod=75)
        # CTI
        dataframe['cti'] = pta.cti(dataframe['close'], length=20)
        # CMF
        dataframe['cmf'] = chaikin_money_flow(dataframe, 20)
        # CRSI (3, 2, 100)
        crsi_closechange = dataframe['close'] / dataframe['close'].shift(1)
        crsi_updown = np.where(crsi_closechange.gt(1), 1.0, np.where(crsi_closechange.lt(1), -1.0, 0.0))
        dataframe['crsi'] = (ta.RSI(dataframe['close'], timeperiod=3) + ta.RSI(crsi_updown, timeperiod=2) + ta.ROC(dataframe['close'], 100)) / 3
        # EMA
        dataframe['ema_4'] = ta.EMA(dataframe, timeperiod=4)
        dataframe['ema_8'] = ta.EMA(dataframe, timeperiod=8)
        dataframe['ema_12'] = ta.EMA(dataframe, timeperiod=12)
        dataframe['ema_13'] = ta.EMA(dataframe, timeperiod=13)
        dataframe['ema_16'] = ta.EMA(dataframe, timeperiod=16)
        dataframe['ema_20'] = ta.EMA(dataframe, timeperiod=20)
        dataframe['ema_24'] = ta.EMA(dataframe, timeperiod=24)
        dataframe['ema_26'] = ta.EMA(dataframe, timeperiod=26)
        dataframe['ema_49'] = ta.EMA(dataframe, timeperiod=49)
        dataframe['ema_50'] = ta.EMA(dataframe, timeperiod=50)
        dataframe['ema_100'] = ta.EMA(dataframe, timeperiod=100)
        dataframe['ema_200'] = ta.EMA(dataframe, timeperiod=200)
        # RSI
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)
        dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20)
        dataframe['rsi_84'] = ta.RSI(dataframe, timeperiod=84)
        dataframe['rsi_112'] = ta.RSI(dataframe, timeperiod=112)
        # Elliot
        dataframe['EWO'] = EWO(dataframe, 50, 200)
        # Williams %R
        dataframe['r_14'] = williams_r(dataframe, period=14)
        dataframe['r_32'] = williams_r(dataframe, period=32)
        dataframe['r_64'] = williams_r(dataframe, period=64)
        dataframe['r_96'] = williams_r(dataframe, period=96)
        dataframe['r_480'] = williams_r(dataframe, period=480)
        # Volume
        dataframe['volume_mean_4'] = dataframe['volume'].rolling(4).mean().shift(1)
        dataframe['volume_mean_12'] = dataframe['volume'].rolling(12).mean().shift(1)
        dataframe['volume_mean_24'] = dataframe['volume'].rolling(24).mean().shift(1)
        # MFI
        dataframe['mfi'] = ta.MFI(dataframe)
        # Heiken Ashi
        heikinashi = qtpylib.heikinashi(dataframe)
        dataframe['ha_open'] = heikinashi['open']
        dataframe['ha_close'] = heikinashi['close']
        dataframe['ha_high'] = heikinashi['high']
        dataframe['ha_low'] = heikinashi['low']
        # BB 40
        bollinger2_40 = qtpylib.bollinger_bands(ha_typical_price(dataframe), window=40, stds=2)
        dataframe['bb_lowerband2_40'] = bollinger2_40['lower']
        dataframe['bb_middleband2_40'] = bollinger2_40['mid']
        dataframe['bb_upperband2_40'] = bollinger2_40['upper']
        # ClucHA
        dataframe['bb_delta_cluc'] = (dataframe['bb_middleband2_40'] - dataframe['bb_lowerband2_40']).abs()
        dataframe['ha_closedelta'] = (dataframe['ha_close'] - dataframe['ha_close'].shift()).abs()
        dataframe['tail'] = (dataframe['ha_close'] - dataframe['ha_low']).abs()
        dataframe['ema_slow'] = ta.EMA(dataframe['ha_close'], timeperiod=50)
        dataframe['ema_fast'] = ta.EMA(dataframe['ha_close'], timeperiod=3)
        dataframe['rocr'] = ta.ROCR(dataframe['ha_close'], timeperiod=28)
        # Cofi
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe['fastd'] = stoch_fast['fastd']
        dataframe['fastk'] = stoch_fast['fastk']
        dataframe['adx'] = ta.ADX(dataframe)
        # Profit Maximizer - PMAX
        dataframe['pm'], dataframe['pmx'] = pmax(heikinashi, ma_type=1, length=9, multiplier=27, period=10, src=3)
        dataframe['source'] = (dataframe['high'] + dataframe['low'] + dataframe['open'] + dataframe['close']) / 4
        dataframe['pmax_thresh'] = ta.EMA(dataframe['source'], timeperiod=9)
        # MOMDIV
        mom = momdiv(dataframe)
        dataframe['momdiv_entry'] = mom['momdiv_entry']
        dataframe['momdiv_exit'] = mom['momdiv_exit']
        dataframe['momdiv_coh'] = mom['momdiv_coh']
        dataframe['momdiv_col'] = mom['momdiv_col']
        # T3 Average
        dataframe['T3'] = t3(dataframe)
        # True range
        dataframe['trange'] = ta.TRANGE(dataframe)
        # KC
        dataframe['range_ma_28'] = ta.SMA(dataframe['trange'], 28)
        dataframe['kc_upperband_28_1'] = dataframe['sma_28'] + dataframe['range_ma_28']
        dataframe['kc_lowerband_28_1'] = dataframe['sma_28'] - dataframe['range_ma_28']
        # KC 20
        dataframe['range_ma_20'] = ta.SMA(dataframe['trange'], 20)
        dataframe['kc_upperband_20_2'] = dataframe['sma_20'] + dataframe['range_ma_20'] * 2
        dataframe['kc_lowerband_20_2'] = dataframe['sma_20'] - dataframe['range_ma_20'] * 2
        dataframe['kc_bb_delta'] = (dataframe['kc_lowerband_20_2'] - dataframe['bb_lowerband2']) / dataframe['bb_lowerband2'] * 100
        # Linreg
        dataframe['hh_20'] = ta.MAX(dataframe['high'], 20)
        dataframe['ll_20'] = ta.MIN(dataframe['low'], 20)
        dataframe['avg_hh_ll_20'] = (dataframe['hh_20'] + dataframe['ll_20']) / 2
        dataframe['avg_close_20'] = ta.SMA(dataframe['close'], 20)
        dataframe['avg_val_20'] = (dataframe['avg_hh_ll_20'] + dataframe['avg_close_20']) / 2
        dataframe['linreg_val_20'] = ta.LINEARREG(dataframe['close'] - dataframe['avg_val_20'], 20, 0)
        # fisher
        rsi = 0.1 * (dataframe['rsi'] - 50)
        dataframe['fisher'] = (np.exp(2 * rsi) - 1) / (np.exp(2 * rsi) + 1)
        # Modified Elder Ray Index
        dataframe['moderi_96'] = moderi(dataframe, 96)
        # vmap indicators
        vwap_low, vwap, vwap_high = vmap_b(dataframe, 20, 1)
        dataframe['vwap_low'] = vwap_low
        dataframe['tcp_percent_4'] = top_percent_change(dataframe, 4)
        return dataframe
    ############################################################################

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # The indicators for the 1h informative timeframe
        informative_1h = self.informative_1h_indicators(dataframe, metadata)
        dataframe = merge_informative_pair(dataframe, informative_1h, self.timeframe, self.inf_1h, ffill=True)
        # The indicators for the normal (5m) timeframe
        dataframe = self.normal_tf_indicators(dataframe)
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
        dataframe.loc[:, 'enter_tag'] = ''  # from NFI next gen, credit goes to @iterativ
        is_local_uptrend = (dataframe['ema_26'] > dataframe['ema_12']) & (dataframe['ema_26'] - dataframe['ema_12'] > dataframe['open'] * self.entry_ema_diff.value) & (dataframe['ema_26'].shift() - dataframe['ema_12'].shift() > dataframe['open'] / 100) & (dataframe['close'] < dataframe['bb_lowerband2'] * self.entry_bb_factor.value) & (dataframe['closedelta'] > dataframe['close'] * self.entry_closedelta.value / 1000)  # use origin bb_rpb_tsl value
        is_local_uptrend2 = (dataframe['ema_26'] > dataframe['ema_12']) & (dataframe['ema_26'] - dataframe['ema_12'] > dataframe['open'] * 0.026) & (dataframe['ema_26'].shift() - dataframe['ema_12'].shift() > dataframe['open'] / 100) & (dataframe['close'] < dataframe['bb_lowerband2'] * self.entry_bb_factor.value) & (dataframe['closedelta'] > dataframe['close'] * 17.922 / 1000)
        is_local_dip = (dataframe['ema_26'] > dataframe['ema_12']) & (dataframe['ema_26'] - dataframe['ema_12'] > dataframe['open'] * self.entry_ema_diff_local_dip.value) & (dataframe['ema_26'].shift() - dataframe['ema_12'].shift() > dataframe['open'] / 100) & (dataframe['close'] < dataframe['ema_20'] * self.entry_ema_high_local_dip.value) & (dataframe['rsi'] < self.entry_rsi_local_dip.value) & (dataframe['crsi'] > self.entry_crsi_local_dip.value) & (dataframe['closedelta'] > dataframe['close'] * self.entry_closedelta_local_dip.value / 1000)  # from SMA offset
        is_ewo = (dataframe['rsi_fast'] < self.entry_rsi_fast.value) & (dataframe['close'] < dataframe['ema_8'] * self.entry_ema_low.value) & (dataframe['EWO'] > self.entry_ewo.value) & (dataframe['close'] < dataframe['ema_16'] * self.entry_ema_high.value) & (dataframe['rsi'] < self.entry_rsi.value)
        is_clucha = dataframe['rocr_1h'].gt(self.entry_clucha_rocr_1h.value) & dataframe['bb_lowerband2_40'].shift().gt(0) & dataframe['bb_delta_cluc'].gt(dataframe['ha_close'] * self.entry_clucha_bbdelta_close.value) & dataframe['ha_closedelta'].gt(dataframe['ha_close'] * self.entry_clucha_closedelta_close.value) & dataframe['tail'].lt(dataframe['bb_delta_cluc'] * self.entry_clucha_bbdelta_tail.value) & dataframe['ha_close'].lt(dataframe['bb_lowerband2_40'].shift()) & dataframe['ha_close'].le(dataframe['ha_close'].shift())  # Modified from cofi, credit goes to original author "slack user CofiBit"
        is_cofi = (dataframe['open'] < dataframe['ema_8'] * self.entry_ema_cofi.value) & qtpylib.crossed_above(dataframe['fastk'], dataframe['fastd']) & (dataframe['fastk'] < self.entry_fastk.value) & (dataframe['fastd'] < self.entry_fastd.value) & (dataframe['adx'] > self.entry_adx.value) & (dataframe['EWO'] > self.entry_ewo_high.value) & (dataframe['cti'] < self.entry_cofi_cti.value) & (dataframe['r_14'] < self.entry_cofi_r14.value)
        is_nfi_32 = (dataframe['rsi_slow'] < dataframe['rsi_slow'].shift(1)) & (dataframe['rsi_fast'] < 46) & (dataframe['rsi'] > 19) & (dataframe['close'] < dataframe['sma_15'] * 0.942) & (dataframe['cti'] < -0.86)
        is_nfi_33 = (dataframe['close'] < dataframe['ema_13'] * 0.978) & (dataframe['EWO'] > 8) & (dataframe['cti'] < -0.88) & (dataframe['rsi'] < 32) & (dataframe['r_14'] < -98.0) & (dataframe['volume'] < dataframe['volume_mean_4'] * 2.5)
        is_nfix_5 = (dataframe['ema_200_1h'] > dataframe['ema_200_1h'].shift(12)) & (dataframe['ema_200_1h'].shift(12) > dataframe['ema_200_1h'].shift(24)) & (dataframe['close'] < dataframe['sma_75'] * 0.932) & (dataframe['EWO'] > 3.6) & (dataframe['cti'] < -0.9) & (dataframe['r_14'] < -97.0)
        is_nfix_39 = (dataframe['ema_200_1h'] > dataframe['ema_200_1h'].shift(12)) & (dataframe['ema_200_1h'].shift(12) > dataframe['ema_200_1h'].shift(24)) & dataframe['bb_lowerband2_40'].shift().gt(0) & dataframe['bb_delta_cluc'].gt(dataframe['close'] * 0.056) & dataframe['closedelta'].gt(dataframe['close'] * 0.01) & dataframe['tail'].lt(dataframe['bb_delta_cluc'] * 0.5) & dataframe['close'].lt(dataframe['bb_lowerband2_40'].shift()) & dataframe['close'].le(dataframe['close'].shift()) & (dataframe['close'] > dataframe['ema_13'] * self.entry_nfix_39_ema.value)
        is_nfix_49 = (dataframe['ema_26'].shift(3) > dataframe['ema_12'].shift(3)) & (dataframe['ema_26'].shift(3) - dataframe['ema_12'].shift(3) > dataframe['open'].shift(3) * 0.032) & (dataframe['ema_26'].shift(9) - dataframe['ema_12'].shift(9) > dataframe['open'].shift(3) / 100) & (dataframe['close'].shift(3) < dataframe['ema_20'].shift(3) * 0.916) & (dataframe['rsi'].shift(3) < 32.5) & (dataframe['crsi'].shift(3) > 18.0) & (dataframe['cti'] < self.entry_nfix_49_cti.value) & (dataframe['r_14'] < self.entry_nfix_49_r14.value)
        is_nfi7_33 = dataframe['moderi_96'] & (dataframe['cti'] < -0.88) & (dataframe['close'] < dataframe['ema_13'] * 0.988) & (dataframe['EWO'] > 6.4) & (dataframe['rsi'] < 32.0) & (dataframe['volume'] < dataframe['volume_mean_4'] * 2.0)
        is_nfi7_37 = (dataframe['pm'] > dataframe['pmax_thresh']) & (dataframe['close'] < dataframe['sma_75'] * 0.98) & (dataframe['EWO'] > 9.8) & (dataframe['rsi'] < 56.0) & (dataframe['cti'] < -0.7) & dataframe['safe_dump_50_1h']
        is_vwap = (dataframe['close'] < dataframe['vwap_low']) & (dataframe['tcp_percent_4'] > 0.04) & (dataframe['cti'] < -0.8) & (dataframe['rsi'] < 35) & (dataframe['rsi_84'] < 60) & (dataframe['rsi_112'] < 60) & (dataframe['volume'] > 0)
        conditions.append(is_local_uptrend)  # ~3.28 / 92.4% / 69.72%
        dataframe.loc[is_local_uptrend, 'enter_tag'] += 'local_uptrend '
        conditions.append(is_local_dip)  # ~0.76 / 91.1% / 15.54%
        dataframe.loc[is_local_dip, 'enter_tag'] += 'local_dip '
        conditions.append(is_ewo)  # ~0.92 / 92.0% / 43.74%      D
        dataframe.loc[is_ewo, 'enter_tag'] += 'ewo '
        conditions.append(is_clucha)  # ~7.2 / 92.5% / 97.98%       D
        dataframe.loc[is_clucha, 'enter_tag'] += 'clucHA '
        conditions.append(is_cofi)  # ~0.4 / 94.4% / 9.59%        D
        dataframe.loc[is_cofi, 'enter_tag'] += 'cofi '
        conditions.append(is_nfi_32)  # ~0.78 / 92.0 % / 37.41%     D
        dataframe.loc[is_nfi_32, 'enter_tag'] += 'nfi_32 '
        conditions.append(is_nfi_33)  # ~0.11 / 100%                D
        dataframe.loc[is_nfi_33, 'enter_tag'] += 'nfi_33 '
        conditions.append(is_nfix_5)  # ~0.25 / 97.7% / 6.53%       D
        dataframe.loc[is_nfix_5, 'enter_tag'] += 'nfix_5 '
        conditions.append(is_nfix_39)  # ~5.33 / 91.8% / 58.57%      D
        dataframe.loc[is_nfix_39, 'enter_tag'] += 'nfix_39 '
        conditions.append(is_nfix_49)  # ~0.33 / 100% / 0%           D
        dataframe.loc[is_nfix_49, 'enter_tag'] += 'nfix_49 '
        conditions.append(is_nfi7_33)  # ~0.71 / 91.3% / 28.94%      D
        dataframe.loc[is_nfi7_33, 'enter_tag'] += 'nfi7_33 '
        conditions.append(is_nfi7_37)  # ~0.46 / 92.6% / 17.05%      D
        dataframe.loc[is_nfi7_37, 'enter_tag'] += 'nfi7_37 '
        conditions.append(is_vwap)
        dataframe.loc[is_vwap, 'enter_tag'] += 'vwap '
        conditions.append(is_local_uptrend2)
        dataframe.loc[is_local_uptrend2, 'enter_tag'] += 'local_uptrend2 '
        if conditions:
            dataframe.loc[reduce(lambda x, y: x | y, conditions), 'enter_long'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(), 'exit_long'] = 1
        return dataframe
# PMAX

def pmax(df, period, multiplier, length, ma_type, src):
    period = int(period)
    multiplier = int(multiplier)
    length = int(length)
    ma_type = int(ma_type)
    src = int(src)
    mavalue = f'MA_{ma_type}_{length}'
    atr = f'ATR_{period}'
    if src == 1:
        masrc = df['close']
    elif src == 2:
        masrc = (df['high'] + df['low']) / 2
    elif src == 3:
        masrc = (df['high'] + df['low'] + df['close'] + df['open']) / 4
    if ma_type == 1:
        mavalue = ta.EMA(masrc, timeperiod=length)
    elif ma_type == 2:
        mavalue = ta.DEMA(masrc, timeperiod=length)
    elif ma_type == 3:
        mavalue = ta.T3(masrc, timeperiod=length)
    elif ma_type == 4:
        mavalue = ta.SMA(masrc, timeperiod=length)
    elif ma_type == 5:
        mavalue = VIDYA(df, length=length)
    elif ma_type == 6:
        mavalue = ta.TEMA(masrc, timeperiod=length)
    elif ma_type == 7:
        mavalue = ta.WMA(df, timeperiod=length)
    elif ma_type == 8:
        mavalue = vwma(df, length)
    elif ma_type == 9:
        mavalue = zema(df, period=length)
    df[atr] = ta.ATR(df, timeperiod=period)
    df['basic_ub'] = mavalue + multiplier / 10 * df[atr]
    df['basic_lb'] = mavalue - multiplier / 10 * df[atr]
    basic_ub = df['basic_ub'].values
    final_ub = np.full(len(df), 0.0)
    basic_lb = df['basic_lb'].values
    final_lb = np.full(len(df), 0.0)
    for i in range(period, len(df)):
        final_ub[i] = basic_ub[i] if basic_ub[i] < final_ub[i - 1] or mavalue[i - 1] > final_ub[i - 1] else final_ub[i - 1]
        final_lb[i] = basic_lb[i] if basic_lb[i] > final_lb[i - 1] or mavalue[i - 1] < final_lb[i - 1] else final_lb[i - 1]
    df['final_ub'] = final_ub
    df['final_lb'] = final_lb
    pm_arr = np.full(len(df), 0.0)
    for i in range(period, len(df)):
        pm_arr[i] = final_ub[i] if pm_arr[i - 1] == final_ub[i - 1] and mavalue[i] <= final_ub[i] else final_lb[i] if pm_arr[i - 1] == final_ub[i - 1] and mavalue[i] > final_ub[i] else final_lb[i] if pm_arr[i - 1] == final_lb[i - 1] and mavalue[i] >= final_lb[i] else final_ub[i] if pm_arr[i - 1] == final_lb[i - 1] and mavalue[i] < final_lb[i] else 0.0
    pm = Series(pm_arr)
    # Mark the trend direction up/down
    pmx = np.where(pm_arr > 0.0, np.where(mavalue < pm_arr, 'down', 'up'), np.NaN)
    return (pm, pmx)
# Mom DIV

def momdiv(dataframe: DataFrame, mom_length: int=10, bb_length: int=20, bb_dev: float=2.0, lookback: int=30) -> DataFrame:
    mom: Series = ta.MOM(dataframe, timeperiod=mom_length)
    upperband, middleband, lowerband = ta.BBANDS(mom, timeperiod=bb_length, nbdevup=bb_dev, nbdevdn=bb_dev, ma_type=0)
    enter_long = qtpylib.crossed_below(mom, lowerband)
    exit_long = qtpylib.crossed_above(mom, upperband)
    hh = dataframe['high'].rolling(lookback).max()
    ll = dataframe['low'].rolling(lookback).min()
    coh = dataframe['high'] >= hh
    col = dataframe['low'] <= ll
    df = DataFrame({'momdiv_mom': mom, 'momdiv_upperb': upperband, 'momdiv_lowerb': lowerband, 'momdiv_entry': enter_long, 'momdiv_exit': exit_long, 'momdiv_coh': coh, 'momdiv_col': col}, index=dataframe['close'].index)
    return df

def t3(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['xe2'] = ta.EMA(df['xe1'], timeperiod=length)
    df['xe3'] = ta.EMA(df['xe2'], timeperiod=length)
    df['xe4'] = ta.EMA(df['xe3'], timeperiod=length)
    df['xe5'] = ta.EMA(df['xe4'], timeperiod=length)
    df['xe6'] = ta.EMA(df['xe5'], timeperiod=length)
    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']

class Github_DerSalvador_freqtrade_helm_chart__BBMod_pyGodStraNew_SMAonly__20260411_0751281DCA(Github_DerSalvador_freqtrade_helm_chart__BBMod_pyGodStraNew_SMAonly__20260411_075128):
    position_adjustment_enable = True
    max_reentry_orders = 2
    max_reentry_multiplier = 3
    # This is called when placing the initial order (opening trade)

    def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: float, proposed_stake: float, min_stake: float, max_stake: float, entry_tag: Optional[str], **kwargs) -> float:
        if self.config['position_adjustment_enable'] is True and self.config['stake_amount'] == 'unlimited':
            return proposed_stake / self.max_reentry_multiplier
        else:
            return proposed_stake

    def adjust_trade_position(self, trade: Trade, current_time: datetime, current_rate: float, current_profit: float, min_stake: float, max_stake: float, **kwargs):
        if self.config['position_adjustment_enable'] is False or current_profit > -0.03:
            return None
        filled_entrys = trade.select_filled_orders('entry')
        count_of_entrys = len(filled_entrys)
        # Maximum 2 reentrys, equal stake as the original
        if 0 < count_of_entrys <= self.max_reentry_orders:
            try:
                # This returns first order stake size
                stake_amount = filled_entrys[0].cost
                # This then calculates current safety order size
                stake_amount = stake_amount
                return stake_amount
            except Exception as exception:
                return None
        return Nonecat: GodStraNew_SMAonly.py: No such file or directory
