# source: https://raw.githubusercontent.com/DerSalvador/freqtrade-helm-chart/f80d4d8b77c53435e9c0a9045636f1bfb2b8c539/chart/deployed_strategies/binance-michael-k8s-namespace/XtraThicc.py
# -*- coding: utf-8 -*-
# --- Do not remove these libs ---
from freqtrade.strategy import IStrategy, merge_informative_pair
from pandas import DataFrame
import freqtrade.vendor.qtpylib.indicators as qtpylib
import talib.abstract as ta
import numpy as np
# --------------------------------
'\n\nGithub_DerSalvador_freqtrade_helm_chart__XtraThicc__20260416_224245 v69\n'

class Github_DerSalvador_freqtrade_helm_chart__XtraThicc__20260416_224245(IStrategy):
    INTERFACE_VERSION = 3
    minimal_roi = {'0': 100}
    # Stoploss:
    stoploss = -0.1
    timeframe = '5m'
    inf_timeframe = '1h'
    use_exit_signal = True
    exit_profit_only = True
    ignore_roi_if_entry_signal = True
    trailing_stop = False
    trailing_stop_positive = 0.002
    trailing_stop_positive_offset = 0.02
    trailing_only_offset_is_reached = True
    startup_candle_count: int = 72
    process_only_new_candles = False

    def informative_pairs(self):
        # add all whitelisted pairs on informative timeframe
        pairs = self.dp.current_whitelist()
        informative_pairs = [(pair, self.inf_timeframe) for pair in pairs]
        return informative_pairs

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Base Thiccness
        dataframe['color'] = np.where(dataframe['close'] > dataframe['open'], 'green', 'red')
        dataframe['how-thicc'] = (dataframe['close'] - dataframe['open']).abs()
        dataframe['avg-thicc'] = dataframe['how-thicc'].abs().rolling(36).mean()
        dataframe['not-thicc'] = dataframe['how-thicc'] < dataframe['avg-thicc']
        dataframe['rly-thicc'] = dataframe['how-thicc'] > dataframe['avg-thicc']
        dataframe['xtra-thicc'] = np.where(dataframe['rly-thicc'].rolling(8).sum() >= 5, 1, 0)
        dataframe['roc'] = ta.ROC(dataframe, timeperiod=6)
        informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe=self.inf_timeframe)
        informative['3d-low'] = informative['close'].rolling(72).min()
        informative['3d-high'] = informative['close'].rolling(72).max()
        dataframe = merge_informative_pair(dataframe, informative, self.timeframe, self.inf_timeframe, ffill=True)
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(dataframe['color'] == 'green') & (dataframe['color'].shift(1) == 'green') & (dataframe['color'].shift(2) == 'red') & (dataframe['color'].shift(3) == 'red') & (dataframe['xtra-thicc'] == 1) & (dataframe['rly-thicc'] == 0) & (dataframe['close'] > dataframe[f'3d-low_{self.inf_timeframe}']) & (dataframe['close'] < dataframe[f'3d-high_{self.inf_timeframe}']), 'entry'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['exit'] = 0
        return dataframe

    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=pair, timeframe=self.timeframe)
        last_candle = dataframe.iloc[-1].squeeze()
        if current_profit > 0.01 and last_candle['roc'] < 0.5:
            return 'rode_that_ass'
        return None