# source: https://raw.githubusercontent.com/xielk/freqtrade-test/925e067f080172a4581bc71ae9ee64be247606f4/user_data/strategies/HarmonicDivergenceStrategy.py
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401
# isort: skip_file
# --- Do not remove these imports ---
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, timezone
from pandas import DataFrame
from typing import Dict, Optional, Union, Tuple

from freqtrade.strategy import (
    IStrategy,
    Trade,
    Order,
    PairLocks,
    informative,  # @informative decorator
    # Hyperopt Parameters
    BooleanParameter,
    CategoricalParameter,
    DecimalParameter,
    IntParameter,
    RealParameter,
    # timeframe helpers
    timeframe_to_minutes,
    timeframe_to_next_date,
    timeframe_to_prev_date,
    # Strategy helper functions
    merge_informative_pair,
    stoploss_from_absolute,
    stoploss_from_open,
    AnnotationType,
)

# --------------------------------
# Add your lib to import here
import talib.abstract as ta
from technical import qtpylib


class Github_xielk_freqtrade_test__HarmonicDivergenceStrategy__20250910_084214(IStrategy):
    """
    谐波背离策略 - 基于价格与指标背离的交易策略
    
    策略特点：
    1. 识别价格与RSI、MACD的背离
    2. 结合斐波那契回撤位确认
    3. 多时间框架分析
    4. 谐波模式识别
    """
    
    # Strategy interface version
    INTERFACE_VERSION = 3

    # 策略时间框架
    timeframe = "15m"

    # 是否支持做空
    can_short: bool = False

    # 最小ROI设置
    minimal_roi = {
        "180": 0.01,  # 3小时后1%收益
        "120": 0.02,  # 2小时后2%收益
        "60": 0.03,   # 1小时后3%收益
        "30": 0.04,   # 30分钟后4%收益
        "0": 0.06     # 立即6%收益
    }

    # 止损设置
    stoploss = -0.05  # 5%止损

    # 追踪止损
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.03
    trailing_only_offset_is_reached = True

    # 只处理新K线
    process_only_new_candles = True

    # 策略参数
    use_exit_signal = True
    exit_profit_only = False
    ignore_roi_if_entry_signal = False

    # 策略启动所需的K线数量
    startup_candle_count: int = 100

    # 背离策略参数
    rsi_period = IntParameter(10, 20, default=14, space="buy")
    macd_fast = IntParameter(8, 16, default=12, space="buy")
    macd_slow = IntParameter(20, 30, default=26, space="buy")
    macd_signal = IntParameter(7, 12, default=9, space="buy")
    divergence_lookback = IntParameter(10, 30, default=20, space="buy")
    fib_period = IntParameter(20, 60, default=30, space="buy")
    volume_threshold = DecimalParameter(0.5, 2.0, default=1.0, space="buy")

    # 订单类型
    order_types = {
        "entry": "limit",
        "exit": "limit",
        "stoploss": "market",
        "stoploss_on_exchange": False
    }

    # 订单时间
    order_time_in_force = {
        "entry": "GTC",
        "exit": "GTC"
    }

    @property
    def plot_config(self):
        return {
            "main_plot": {
                "fib_0.236": {"color": "red", "type": "line"},
                "fib_0.382": {"color": "orange", "type": "line"},
                "fib_0.5": {"color": "yellow", "type": "line"},
                "fib_0.618": {"color": "green", "type": "line"},
                "fib_0.786": {"color": "blue", "type": "line"},
            },
            "subplots": {
                "RSI": {
                    "rsi": {"color": "purple"},
                    "rsi_high": {"color": "red", "type": "line"},
                    "rsi_low": {"color": "green", "type": "line"},
                },
                "MACD": {
                    "macd": {"color": "blue"},
                    "macdsignal": {"color": "orange"},
                    "macdhist": {"color": "gray"},
                },
                "Divergence": {
                    "bullish_divergence": {"color": "green", "type": "scatter"},
                    "bearish_divergence": {"color": "red", "type": "scatter"},
                }
            }
        }

    def informative_pairs(self):
        """
        定义额外的信息性交易对
        """
        pairs = self.dp.current_whitelist()
        informative_pairs = []
        
        # 添加1小时和4小时数据用于多时间框架分析
        for pair in pairs:
            informative_pairs.append((pair, "1h"))
            informative_pairs.append((pair, "4h"))
            
        return informative_pairs

    @informative("1h")
    def populate_indicators_1h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算1小时时间框架的指标
        """
        # 1小时RSI
        dataframe["rsi_1h"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 1小时MACD
        macd_1h = ta.MACD(dataframe, fastperiod=self.macd_fast.value, 
                         slowperiod=self.macd_slow.value, signalperiod=self.macd_signal.value)
        dataframe["macd_1h"] = macd_1h["macd"]
        dataframe["macdsignal_1h"] = macd_1h["macdsignal"]
        
        return dataframe

    @informative("4h")
    def populate_indicators_4h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算4小时时间框架的指标
        """
        # 4小时RSI
        dataframe["rsi_4h"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 4小时MACD
        macd_4h = ta.MACD(dataframe, fastperiod=self.macd_fast.value, 
                         slowperiod=self.macd_slow.value, signalperiod=self.macd_signal.value)
        dataframe["macd_4h"] = macd_4h["macd"]
        dataframe["macdsignal_4h"] = macd_4h["macdsignal"]
        
        return dataframe

    def detect_divergence(self, dataframe: DataFrame, price_col: str, indicator_col: str, 
                         lookback: int, bullish: bool = True) -> DataFrame:
        """
        检测背离信号
        """
        result = pd.Series(False, index=dataframe.index)
        
        for i in range(lookback, len(dataframe)):
            # 获取当前窗口的数据
            price_window = dataframe[price_col].iloc[i-lookback:i+1]
            indicator_window = dataframe[indicator_col].iloc[i-lookback:i+1]
            
            if bullish:
                # 看涨背离：价格创新低，但指标没有创新低
                price_low_idx = price_window.idxmin()
                indicator_low_idx = indicator_window.idxmin()
                
                if (price_low_idx == price_window.index[-1] and  # 价格在最近创出新低
                    indicator_low_idx != indicator_window.index[-1]):  # 但指标没有在最近创新低
                    result.iloc[i] = True
            else:
                # 看跌背离：价格创新高，但指标没有创新高
                price_high_idx = price_window.idxmax()
                indicator_high_idx = indicator_window.idxmax()
                
                if (price_high_idx == price_window.index[-1] and  # 价格在最近创出新高
                    indicator_high_idx != indicator_window.index[-1]):  # 但指标没有在最近创新高
                    result.iloc[i] = True
        
        return result

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算主要技术指标
        """
        # 合并1小时数据
        if self.dp:
            inf_1h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe='1h')
            if not inf_1h.empty:
                dataframe = merge_informative_pair(dataframe, inf_1h, self.timeframe, "1h", ffill=True)
        
        # 合并4小时数据
        if self.dp:
            inf_4h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe='4h')
            if not inf_4h.empty:
                dataframe = merge_informative_pair(dataframe, inf_4h, self.timeframe, "4h", ffill=True)
        
        # 计算RSI
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 计算MACD
        macd = ta.MACD(dataframe, fastperiod=self.macd_fast.value, 
                      slowperiod=self.macd_slow.value, signalperiod=self.macd_signal.value)
        dataframe["macd"] = macd["macd"]
        dataframe["macdsignal"] = macd["macdsignal"]
        dataframe["macdhist"] = macd["macdhist"]
        
        # 计算斐波那契回撤位
        period = self.fib_period.value
        dataframe["highest"] = dataframe["high"].rolling(window=period).max()
        dataframe["lowest"] = dataframe["low"].rolling(window=period).min()
        dataframe["price_range"] = dataframe["highest"] - dataframe["lowest"]
        
        dataframe["fib_0.236"] = dataframe["highest"] - 0.236 * dataframe["price_range"]
        dataframe["fib_0.382"] = dataframe["highest"] - 0.382 * dataframe["price_range"]
        dataframe["fib_0.5"] = dataframe["highest"] - 0.5 * dataframe["price_range"]
        dataframe["fib_0.618"] = dataframe["highest"] - 0.618 * dataframe["price_range"]
        dataframe["fib_0.786"] = dataframe["highest"] - 0.786 * dataframe["price_range"]
        
        # 计算EMA
        dataframe["ema_20"] = ta.EMA(dataframe, timeperiod=20)
        dataframe["ema_50"] = ta.EMA(dataframe, timeperiod=50)
        
        # 计算成交量比率
        dataframe["volume_sma"] = dataframe["volume"].rolling(window=20).mean()
        dataframe["volume_ratio"] = dataframe["volume"] / dataframe["volume_sma"]
        
        # 检测背离信号
        lookback = self.divergence_lookback.value
        
        # RSI背离
        dataframe["bullish_rsi_divergence"] = self.detect_divergence(
            dataframe, "close", "rsi", lookback, bullish=True)
        dataframe["bearish_rsi_divergence"] = self.detect_divergence(
            dataframe, "close", "rsi", lookback, bullish=False)
        
        # MACD背离
        dataframe["bullish_macd_divergence"] = self.detect_divergence(
            dataframe, "close", "macd", lookback, bullish=True)
        dataframe["bearish_macd_divergence"] = self.detect_divergence(
            dataframe, "close", "macd", lookback, bullish=False)
        
        # 综合背离信号
        dataframe["bullish_divergence"] = (
            dataframe["bullish_rsi_divergence"] | dataframe["bullish_macd_divergence"]
        )
        dataframe["bearish_divergence"] = (
            dataframe["bearish_rsi_divergence"] | dataframe["bearish_macd_divergence"]
        )
        
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        基于背离的买入信号
        """
        # 基础条件
        base_conditions = (
            (dataframe["volume"] > 0) &
            (dataframe["price_range"] > 0) &
            (dataframe["highest"].notna()) &
            (dataframe["lowest"].notna()) &
            (dataframe["volume_ratio"] >= self.volume_threshold.value)
        )
        
        # 看涨背离条件
        divergence_conditions = (
            dataframe["bullish_divergence"]  # 检测到看涨背离
        )
        
        # 斐波那契支撑条件
        fib_conditions = (
            # 价格在关键斐波那契支撑位附近
            (
                ((dataframe["close"] <= dataframe["fib_0.618"] * 1.02) & 
                 (dataframe["close"] >= dataframe["fib_0.618"] * 0.98)) |
                ((dataframe["close"] <= dataframe["fib_0.786"] * 1.02) & 
                 (dataframe["close"] >= dataframe["fib_0.786"] * 0.98))
            )
        )
        
        # 趋势确认条件
        trend_conditions = (
            (dataframe["close"] > dataframe["ema_20"]) &  # 价格在EMA20之上
            (dataframe["ema_20"] > dataframe["ema_50"])   # 短期趋势向上
        )
        
        # 多时间框架确认
        multi_timeframe_conditions = True
        if "rsi_1h" in dataframe.columns:
            multi_timeframe_conditions = (
                (dataframe["rsi_1h"] < 70) &  # 1小时RSI不过度超买
                (dataframe["rsi"] < 60)       # 15分钟RSI不过度超买
            )
        
        # 综合买入条件
        dataframe.loc[
            base_conditions &
            divergence_conditions &
            fib_conditions &
            trend_conditions &
            multi_timeframe_conditions,
            "enter_long"
        ] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        基于背离的卖出信号
        """
        # 基础条件
        base_conditions = (
            (dataframe["volume"] > 0) &
            (dataframe["price_range"] > 0) &
            (dataframe["highest"].notna()) &
            (dataframe["lowest"].notna())
        )
        
        # 看跌背离条件
        divergence_conditions = (
            dataframe["bearish_divergence"]  # 检测到看跌背离
        )
        
        # 斐波那契阻力条件
        fib_conditions = (
            # 价格在关键斐波那契阻力位附近
            (
                ((dataframe["close"] >= dataframe["fib_0.382"] * 0.98) & 
                 (dataframe["close"] <= dataframe["fib_0.382"] * 1.02)) |
                ((dataframe["close"] >= dataframe["fib_0.236"] * 0.98) & 
                 (dataframe["close"] <= dataframe["fib_0.236"] * 1.02))
            )
        )
        
        # 趋势确认条件
        trend_conditions = (
            (dataframe["close"] < dataframe["ema_20"]) |  # 价格跌破EMA20
            (dataframe["macd"] < dataframe["macdsignal"])  # MACD死叉
        )
        
        # 多时间框架确认
        multi_timeframe_conditions = True
        if "rsi_1h" in dataframe.columns:
            multi_timeframe_conditions = (
                (dataframe["rsi_1h"] > 30) &  # 1小时RSI不过度超卖
                (dataframe["rsi"] > 40)       # 15分钟RSI不过度超卖
            )
        
        # 综合卖出条件
        dataframe.loc[
            base_conditions &
            divergence_conditions &
            fib_conditions &
            trend_conditions &
            multi_timeframe_conditions,
            "exit_long"
        ] = 1

        return dataframe

    def custom_stoploss(self, pair: str, trade: 'Trade', current_time: datetime,
                       current_rate: float, current_profit: float, **kwargs) -> float:
        """
        基于背离的动态止损
        """
        dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
        if len(dataframe) == 0:
            return self.stoploss
            
        last_candle = dataframe.iloc[-1].squeeze()
        
        # 如果检测到新的看跌背离，收紧止损
        if last_candle.get("bearish_divergence", False):
            return -0.02  # 2%止损
        
        # 如果价格突破关键斐波那契位，调整止损
        if current_rate >= last_candle.get("fib_0.5", 0):
            return -0.01  # 1%止损（接近保本）
        
        return self.stoploss

    def custom_exit(self, pair: str, trade: 'Trade', current_time: datetime, current_rate: float,
                   current_profit: float, **kwargs) -> Optional[Union[str, bool]]:
        """
        基于背离的自定义退出
        """
        dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
        if len(dataframe) == 0:
            return None
            
        last_candle = dataframe.iloc[-1].squeeze()
        
        # 如果检测到看跌背离且盈利，考虑退出
        if (last_candle.get("bearish_divergence", False) and 
            current_profit > 0.02):
            return "bearish_divergence_exit"
        
        # 如果价格突破0.236斐波那契位且盈利良好，考虑退出
        if (current_rate >= last_candle.get("fib_0.236", 0) and 
            current_profit > 0.03):
            return "fib_resistance_exit"
        
        return None
