# source: https://raw.githubusercontent.com/assinscreedFC/trading_strategie/d000eccf8c5ffd4c115ace404c4250ce433203ed/freqtrade/strategies/HeikinAshiTrend.py
# ══════════════════════════════════════════════════════════════
# anis solidscale - Elite Spot Trading Suite
# STRATEGIE : Github_assinscreedFC_trading_strategie__HeikinAshiTrend__20260321_144032
# CATEGORIE : Tendance — Heikin Ashi Momentum
# ══════════════════════════════════════════════════════════════
#
# LOGIQUE :
# Heikin Ashi lisse le bruit des chandeliers classiques pour
# identifier les tendances fortes.
# 1. Bougie HA verte + pas de meche basse → tendance haussiere forte
# 2. EMA en hausse → confirmation
# 3. Sortie : bougie HA rouge sans meche haute OU ha_close < EMA
# ══════════════════════════════════════════════════════════════

import sys
from pathlib import Path

import numpy as np
from pandas import DataFrame

from freqtrade.strategy import IStrategy, IntParameter, DecimalParameter

sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
from utils.indicators import CommonIndicators
from utils.logging_utils import TradeLogger
from utils.telegram_notifier import TelegramNotifier


class Github_assinscreedFC_trading_strategie__HeikinAshiTrend__20260321_144032(IStrategy):
    INTERFACE_VERSION = 3
    can_short = False
    timeframe = "4h"
    startup_candle_count = 80

    minimal_roi = {"0": 0.10, "240": 0.05, "720": 0.03, "1440": 0.01}
    stoploss = -0.06
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.03
    trailing_only_offset_is_reached = True

    # ── Buy params ──
    ema_period = IntParameter(30, 70, default=50, space="buy")
    lookback = IntParameter(1, 5, default=2, space="buy")
    wick_tolerance = DecimalParameter(0.0001, 0.005, default=0.001, decimals=4, space="buy")
    volume_period = IntParameter(10, 50, default=20, space="buy")

    # ── Sell params ──
    exit_lookback = IntParameter(1, 3, default=1, space="sell")

    _logger = None
    _notifier = None

    def __getstate__(self):
        state = self.__dict__.copy()
        state["_logger"] = None
        state["_notifier"] = None
        return state

    def __setstate__(self, state):
        self.__dict__.update(state)

    def _init_utils(self) -> None:
        if self._logger is None:
            self._logger = TradeLogger(strategy_name="Github_assinscreedFC_trading_strategie__HeikinAshiTrend__20260321_144032")
            self._notifier = TelegramNotifier()

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        self._init_utils()

        # Pre-calc EMA pour TOUTES les valeurs
        for p in range(self.ema_period.low, self.ema_period.high + 1):
            dataframe = CommonIndicators.add_ema(dataframe, period=p)

        # Pre-calc Volume SMA pour TOUTES les valeurs
        for p in range(self.volume_period.low, self.volume_period.high + 1):
            dataframe = CommonIndicators.add_volume_sma(dataframe, period=p)

        # Heikin Ashi calc vectorise
        ha_close = (dataframe["open"] + dataframe["high"] + dataframe["low"] + dataframe["close"]) / 4

        # ha_open est iteratif mais optimise avec numpy
        o = dataframe["open"].values.copy().astype(float)
        c = ha_close.values.copy()
        ha_o = np.empty(len(o))
        ha_o[0] = (o[0] + c[0]) / 2
        for i in range(1, len(o)):
            ha_o[i] = (ha_o[i - 1] + c[i - 1]) / 2

        dataframe["ha_close"] = ha_close
        dataframe["ha_open"] = ha_o
        dataframe["ha_high"] = dataframe[["high", "ha_open", "ha_close"]].max(axis=1)
        dataframe["ha_low"] = dataframe[["low", "ha_open", "ha_close"]].min(axis=1)

        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        tol = self.wick_tolerance.value
        ema_col = f"ema_{self.ema_period.value}"
        lb = self.lookback.value

        ema_rising = dataframe[ema_col] > dataframe[ema_col].shift(lb)

        conditions = (
            (dataframe["ha_close"] > dataframe["ha_open"])
            & ((dataframe["ha_low"] - dataframe["ha_open"]).abs() < tol * dataframe["ha_close"])
            & ema_rising
            & (dataframe["volume"] > 0)
        )

        dataframe.loc[conditions, "enter_long"] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        ema_col = f"ema_{self.ema_period.value}"
        tol = self.wick_tolerance.value

        conditions = (
            (
                (dataframe["ha_close"] < dataframe["ha_open"])
                & ((dataframe["ha_high"] - dataframe["ha_close"]).abs() < tol * dataframe["ha_close"])
            )
            | (dataframe["ha_close"] < dataframe[ema_col])
        )

        dataframe.loc[conditions, "exit_long"] = 1
        return dataframe
