# source: https://raw.githubusercontent.com/Zer0phucks/trading-lab/90a6eef67476f5a442f148722ac2f56de8ee2fd1/user_data/strategies/BreakoutGammaV1.py
from __future__ import annotations

from pandas import DataFrame
import talib.abstract as ta

from freqtrade.strategy import DecimalParameter, IStrategy


class Github_Zer0phucks_trading_lab__BreakoutGammaV1__20260505_075652(IStrategy):
    INTERFACE_VERSION = 3

    timeframe = "5m"
    can_short = False
    process_only_new_candles = True
    startup_candle_count = 120

    minimal_roi = {"0": 0.05, "120": 0.02, "360": 0.0}
    stoploss = -0.09
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.045
    trailing_only_offset_is_reached = True

    volume_mult = DecimalParameter(1.2, 3.0, default=1.6, decimals=1, space="buy")
    exit_rsi = DecimalParameter(45.0, 65.0, default=52.0, decimals=1, space="sell")

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe["high_20"] = dataframe["high"].rolling(20).max().shift(1)
        dataframe["ema_50"] = ta.EMA(dataframe, timeperiod=50)
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
        dataframe["atr"] = ta.ATR(dataframe, timeperiod=14)
        dataframe["atr_mean_50"] = dataframe["atr"].rolling(50).mean()
        dataframe["volume_mean_30"] = dataframe["volume"].rolling(30).mean()
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (dataframe["close"] > dataframe["high_20"])
                & (dataframe["close"] > dataframe["ema_50"])
                & (dataframe["atr"] > dataframe["atr_mean_50"])
                & (dataframe["volume"] > dataframe["volume_mean_30"] * self.volume_mult.value)
            ),
            "enter_long",
        ] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[
            (
                (dataframe["close"] < dataframe["ema_50"])
                | (dataframe["rsi"] < self.exit_rsi.value)
            ),
            "exit_long",
        ] = 1
        return dataframe
