# source: https://raw.githubusercontent.com/IISweetHeartII/freqtrade-strategies-lab/fee95ce63a145ad5d15d53c7c3af5d8acbebc3e2/user_data/strategies/E0V1EVNN.py
from datetime import datetime, timedelta
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
from freqtrade.strategy import DecimalParameter, IntParameter
from functools import reduce
import warnings

warnings.simplefilter(action="ignore", category=RuntimeWarning)


class Github_IISweetHeartII_freqtrade_strategies_lab__E0V1EVNN__20260216_081316(IStrategy):
    """
    E0V1EN candle-close exit variant
    - Entry: real-time
    - Exit: candle close (trailing stop disabled)
    - Suitable for mean reversion
    """
    minimal_roi = {
        "0": 1
    }
    timeframe = '5m'
    process_only_new_candles = True
    startup_candle_count = 20
    order_types = {
        'entry': 'market',
        'exit': 'market',
        'emergency_exit': 'market',
        'force_entry': 'market',
        'force_exit': "market",
        'stoploss': 'market',
        'stoploss_on_exchange': False,
        'stoploss_on_exchange_interval': 60,
        'stoploss_on_exchange_market_ratio': 0.99
    }

    # Candle-close exit (trailing disabled)
    stoploss = -0.15
    trailing_stop = False

    is_optimize_32 = False
    buy_rsi_fast_32 = IntParameter(20, 70, default=40, space='buy', optimize=is_optimize_32)
    buy_rsi_32 = IntParameter(15, 50, default=42, space='buy', optimize=is_optimize_32)
    buy_sma15_32 = DecimalParameter(0.900, 1, default=0.973, decimals=3, space='buy', optimize=is_optimize_32)
    buy_cti_32 = DecimalParameter(-1, 1, default=0.69, decimals=2, space='buy', optimize=is_optimize_32)


    # Optimizable parameters: 24h price change percentage range
    buy_24h_min_pct = DecimalParameter(-30.0, 0.0, default=-15.0, decimals=1, space='buy', optimize=True)
    buy_24h_max_pct = DecimalParameter(0.0, 200.0, default=50.0, decimals=1, space='buy', optimize=True)

    buy_24h_min_pct1 = DecimalParameter(-30.0, 0.0, default=-15.0, decimals=1, space='buy', optimize=True)
    buy_24h_max_pct1 = DecimalParameter(0.0, 200.0, default=50.0, decimals=1, space='buy', optimize=True)



    sell_fastx = IntParameter(50, 100, default=84, space='sell', optimize=True)

    @property
    def protections(self):

        return [
                {
                "method": "CooldownPeriod",
                "stop_duration_candles": 96
                },
                {
                "method": "MaxDrawdown",
                "lookback_period_candles": 288,  # 24 hours (5min * 288)
                "trade_limit": 4,                # minimum 4 trades
                "stop_duration_candles": 288,    # stop for 24 hours
                "max_allowed_drawdown": 0.20     # 20% drawdown trigger
                }
               ]

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # buy_1 indicators
        dataframe['sma_15'] = ta.SMA(dataframe, timeperiod=15)
        dataframe['cti'] = pta.cti(dataframe["close"], length=20)
        dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
        dataframe['rsi_fast'] = ta.RSI(dataframe, timeperiod=4)
        dataframe['rsi_slow'] = ta.RSI(dataframe, timeperiod=20)

        dataframe['24h_change_pct'] = (dataframe['close'].pct_change(periods=288) * 100)


        # profit sell indicators
        stoch_fast = ta.STOCHF(dataframe, 5, 3, 0, 3, 0)
        dataframe['fastk'] = stoch_fast['fastk']
        dataframe['cci'] = ta.CCI(dataframe, timeperiod=20)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        conditions = []
        dataframe.loc[:, 'enter_tag'] = ''
        buy_1 = (
                (dataframe['rsi_slow'] < dataframe['rsi_slow'].shift(1)) &
                (dataframe['rsi_fast'] < self.buy_rsi_fast_32.value) &
                (dataframe['rsi'] > self.buy_rsi_32.value) &
                (dataframe['close'] < dataframe['sma_15'] * self.buy_sma15_32.value) &
                (dataframe['cti'] < self.buy_cti_32.value) &
                (dataframe['24h_change_pct'] > self.buy_24h_min_pct1.value) &
                (dataframe['24h_change_pct'] < self.buy_24h_max_pct1.value)
        )

        buy_new = (
                (dataframe['rsi_slow'] < dataframe['rsi_slow'].shift(1)) &
                (dataframe['rsi_fast'] < 34) &
                (dataframe['rsi'] > 28) &
                (dataframe['close'] < dataframe['sma_15'] * 0.96) &
                (dataframe['cti'] < self.buy_cti_32.value) &
                (dataframe['24h_change_pct'] > self.buy_24h_min_pct.value) &
                (dataframe['24h_change_pct'] < self.buy_24h_max_pct.value)
        )


        conditions.append(buy_1)
        dataframe.loc[buy_1, 'enter_tag'] += 'buy_1'

        conditions.append(buy_new)
        dataframe.loc[buy_new, 'enter_tag'] += 'buy_new'

        if conditions:
            dataframe.loc[
                reduce(lambda x, y: x | y, conditions),
                'enter_long'] = 1
        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)
        current_candle = dataframe.iloc[-1].squeeze()

        # 1. Stoploss (candle close) - replaces default stoploss
        if current_profit <= -0.15:
            return "candle_stoploss"

        # 2. Target take-profit (mean reversion target reached)
        if current_profit >= 0.05:
            return "target_profit_sell"

        # 3. FastK take-profit (buy_new only)
        if current_profit > 0 and "buy_new" == str(trade.enter_tag):
            if current_candle["fastk"] > self.sell_fastx.value:
                return "fastk_profit_sell"

        # 4. CCI-based exit
        if current_profit > -0.03:
            if current_candle["cci"] > 80:
                return "cci_loss_sell"

        # 5. Time-based stoploss
        if current_time - timedelta(hours=7) > trade.open_date_utc:
            if current_profit >= -0.05:
                return "time_loss_sell_7_5"

        if current_time - timedelta(hours=10) > trade.open_date_utc:
            if current_profit >= -0.10:
                return "time_loss_sell_10_10"

        return None

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[:, ['exit_long', 'exit_tag']] = (0, 'long_out')
        return dataframe
