# source: https://raw.githubusercontent.com/xielk/freqtrade-test/925e067f080172a4581bc71ae9ee64be247606f4/user_data/strategies/AdvancedFibonacciStrategy.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__AdvancedFibonacciStrategy__20250910_084214(IStrategy):
    """
    高级斐波那契回归策略 - 基于多时间框架分析（调整版）
    
    策略特点：
    1. 多时间框架斐波那契分析（15m + 1h + 4h）
    2. 动态斐波那契周期调整
    3. 结合资金费率分析
    4. 智能止损和止盈
    5. 市场情绪指标确认
    6. 放宽条件，增加交易机会
    """
    
    # Strategy interface version
    INTERFACE_VERSION = 3

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

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

    # 最小ROI设置 - 更保守的设置
    minimal_roi = {
        "180": 0.008,  # 3小时后0.8%收益
        "120": 0.015,  # 2小时后1.5%收益
        "60": 0.025,   # 1小时后2.5%收益
        "30": 0.035,   # 30分钟后3.5%收益
        "0": 0.05      # 立即5%收益
    }

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

    # 追踪止损
    trailing_stop = True
    trailing_stop_positive = 0.015  # 1.5%开始追踪
    trailing_stop_positive_offset = 0.025  # 2.5%偏移
    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

    # 斐波那契策略参数 - 可优化（放宽条件）
    fib_period_short = IntParameter(15, 40, default=20, space="buy")  # 短期斐波那契周期
    fib_period_long = IntParameter(40, 100, default=60, space="buy")  # 长期斐波那契周期
    rsi_period = IntParameter(10, 30, default=14, space="buy")        # RSI周期
    rsi_oversold = IntParameter(30, 50, default=45, space="buy")      # RSI超卖线（放宽）
    rsi_overbought = IntParameter(50, 70, default=55, space="sell")   # RSI超买线（放宽）
    volume_threshold = DecimalParameter(0.3, 1.5, default=0.8, space="buy")  # 成交量倍数（降低）
    funding_rate_threshold = DecimalParameter(-0.01, 0.01, default=0.0, space="buy")  # 资金费率阈值
    fib_tolerance = DecimalParameter(0.02, 0.15, default=0.08, 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"},
                "fib_1h_0.618": {"color": "lightgreen", "type": "line"},
                "fib_1h_0.382": {"color": "lightcoral", "type": "line"},
            },
            "subplots": {
                "RSI": {
                    "rsi": {"color": "purple"},
                    "rsi_oversold": {"color": "red", "type": "line"},
                    "rsi_overbought": {"color": "green", "type": "line"},
                },
                "Volume": {
                    "volume_ratio": {"color": "blue"},
                },
                "Funding": {
                    "funding_rate": {"color": "orange"},
                }
            }
        }

    def informative_pairs(self):
        """
        定义额外的信息性交易对 - 获取1小时和4小时数据
        """
        pairs = self.dp.current_whitelist()
        informative_pairs = []
        
        # 为每个交易对添加1小时和4小时数据
        for pair in pairs:
            informative_pairs.append((pair, "1h"))   # 1小时数据用于短期分析
            informative_pairs.append((pair, "4h"))   # 4小时数据用于长期趋势
            # 添加资金费率数据（8小时）
            informative_pairs.append((pair, "8h"))
            
        return informative_pairs

    @informative("1h")
    def populate_indicators_1h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算1小时时间框架的指标
        """
        # 短期斐波那契
        period_short = self.fib_period_short.value
        dataframe["highest_1h"] = dataframe["high"].rolling(window=period_short).max()
        dataframe["lowest_1h"] = dataframe["low"].rolling(window=period_short).min()
        dataframe["price_range_1h"] = dataframe["highest_1h"] - dataframe["lowest_1h"]
        
        # 1小时斐波那契水平
        dataframe["fib_1h_0.618"] = dataframe["highest_1h"] - 0.618 * dataframe["price_range_1h"]
        dataframe["fib_1h_0.382"] = dataframe["highest_1h"] - 0.382 * dataframe["price_range_1h"]
        
        # 1小时RSI
        dataframe["rsi_1h"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 1小时EMA
        dataframe["ema_20_1h"] = ta.EMA(dataframe, timeperiod=20)
        
        return dataframe

    @informative("4h")
    def populate_indicators_4h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算4小时时间框架的指标 - 用于长期趋势分析
        """
        # 长期斐波那契
        period_long = self.fib_period_long.value
        dataframe["highest_4h"] = dataframe["high"].rolling(window=period_long).max()
        dataframe["lowest_4h"] = dataframe["low"].rolling(window=period_long).min()
        dataframe["price_range_4h"] = dataframe["highest_4h"] - dataframe["lowest_4h"]
        
        # 4小时斐波那契水平
        dataframe["fib_4h_0.618"] = dataframe["highest_4h"] - 0.618 * dataframe["price_range_4h"]
        dataframe["fib_4h_0.382"] = dataframe["highest_4h"] - 0.382 * dataframe["price_range_4h"]
        
        # 4小时RSI
        dataframe["rsi_4h"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 4小时EMA
        dataframe["ema_50_4h"] = ta.EMA(dataframe, timeperiod=50)
        
        return dataframe

    @informative("8h")
    def populate_indicators_8h(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算8小时时间框架的指标 - 主要用于资金费率分析
        """
        # 这里可以添加资金费率相关的分析
        # 由于数据结构可能不同，这里先做基础处理
        if 'funding_rate' in dataframe.columns:
            dataframe["funding_rate_sma"] = dataframe["funding_rate"].rolling(window=3).mean()
        else:
            dataframe["funding_rate_sma"] = 0
            
        return dataframe

    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)
        
        # 合并8小时数据
        if self.dp:
            inf_8h = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe='8h')
            if not inf_8h.empty:
                dataframe = merge_informative_pair(dataframe, inf_8h, self.timeframe, "8h", ffill=True)
        
        # 计算RSI
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
        
        # 计算长期斐波那契回归水平
        period_long = self.fib_period_long.value
        dataframe["highest"] = dataframe["high"].rolling(window=period_long).max()
        dataframe["lowest"] = dataframe["low"].rolling(window=period_long).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["ema_100"] = ta.EMA(dataframe, timeperiod=100)
        
        # 计算MACD
        macd = ta.MACD(dataframe)
        dataframe["macd"] = macd["macd"]
        dataframe["macdsignal"] = macd["macdsignal"]
        dataframe["macdhist"] = macd["macdhist"]
        
        # 计算布林带
        bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
        dataframe["bb_lowerband"] = bollinger["lower"]
        dataframe["bb_middleband"] = bollinger["mid"]
        dataframe["bb_upperband"] = bollinger["upper"]
        dataframe["bb_percent"] = (
            (dataframe["close"] - dataframe["bb_lowerband"]) /
            (dataframe["bb_upperband"] - dataframe["bb_lowerband"])
        )
        
        # 计算成交量比率
        dataframe["volume_sma"] = dataframe["volume"].rolling(window=20).mean()
        dataframe["volume_ratio"] = dataframe["volume"] / dataframe["volume_sma"]
        
        # 计算ATR用于动态止损
        dataframe["atr"] = ta.ATR(dataframe, timeperiod=14)
        
        # 计算价格动量
        dataframe["momentum"] = dataframe["close"] / dataframe["close"].shift(5) - 1
        
        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)  # 成交量确认（降低要求）
        )
        
        # 斐波那契回归条件（放宽容忍度）
        tolerance = self.fib_tolerance.value
        fib_conditions = (
            # 价格在关键斐波那契支撑位附近（大幅放宽容忍度）
            (
                # 接近0.618斐波那契位（主要支撑）
                ((dataframe["close"] <= dataframe["fib_0.618"] * (1 + tolerance)) & 
                 (dataframe["close"] >= dataframe["fib_0.618"] * (1 - tolerance))) |
                # 或者接近0.786斐波那契位（强支撑）
                ((dataframe["close"] <= dataframe["fib_0.786"] * (1 + tolerance)) & 
                 (dataframe["close"] >= dataframe["fib_0.786"] * (1 - tolerance))) |
                # 或者接近0.5斐波那契位（中等支撑）
                ((dataframe["close"] <= dataframe["fib_0.5"] * (1 + tolerance)) & 
                 (dataframe["close"] >= dataframe["fib_0.5"] * (1 - tolerance)))
            )
        )
        
        # RSI条件（放宽）
        rsi_conditions = (
            (dataframe["rsi"] <= self.rsi_oversold.value)  # 移除RSI上升要求
        )
        
        # 趋势确认条件（简化）
        trend_conditions = (
            (dataframe["close"] > dataframe["ema_20"])  # 只要求价格在EMA20之上
        )
        
        # 多时间框架确认（可选）
        multi_timeframe_conditions = True
        if "fib_1h_0.618" in dataframe.columns:
            multi_timeframe_conditions = (
                # 1小时时间框架也显示支撑（放宽条件）
                (dataframe["close"] >= dataframe["fib_1h_0.618"] * (1 - tolerance)) |
                (dataframe["close"] <= dataframe["fib_1h_0.618"] * (1 + tolerance)) |
                True  # 如果1小时数据不可用，仍然允许交易
            )
        
        # 资金费率条件（可选）
        funding_conditions = True
        if "funding_rate_sma_8h" in dataframe.columns:
            funding_conditions = (
                dataframe["funding_rate_sma_8h"] <= self.funding_rate_threshold.value
            )
        
        # 综合买入条件（简化逻辑）
        dataframe.loc[
            base_conditions &
            fib_conditions &
            rsi_conditions &
            trend_conditions &
            multi_timeframe_conditions &
            funding_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())
        )
        
        # 斐波那契阻力条件（放宽容忍度）
        tolerance = self.fib_tolerance.value
        fib_conditions = (
            # 价格在关键斐波那契阻力位附近（放宽容忍度）
            (
                # 接近0.382斐波那契位（主要阻力）
                ((dataframe["close"] >= dataframe["fib_0.382"] * (1 - tolerance)) & 
                 (dataframe["close"] <= dataframe["fib_0.382"] * (1 + tolerance))) |
                # 或者接近0.236斐波那契位（强阻力）
                ((dataframe["close"] >= dataframe["fib_0.236"] * (1 - tolerance)) & 
                 (dataframe["close"] <= dataframe["fib_0.236"] * (1 + tolerance))) |
                # 或者接近0.5斐波那契位（中等阻力）
                ((dataframe["close"] >= dataframe["fib_0.5"] * (1 - tolerance)) & 
                 (dataframe["close"] <= dataframe["fib_0.5"] * (1 + tolerance)))
            )
        )
        
        # RSI条件（放宽）
        rsi_conditions = (
            (dataframe["rsi"] >= self.rsi_overbought.value)  # 移除RSI下降要求
        )
        
        # 趋势确认条件（简化）
        trend_conditions = (
            (dataframe["close"] < dataframe["bb_upperband"])  # 只要求价格不在布林带上轨之上
        )
        
        # 多时间框架确认（可选）
        multi_timeframe_conditions = True
        if "fib_1h_0.382" in dataframe.columns:
            multi_timeframe_conditions = (
                # 1小时时间框架也显示阻力（放宽条件）
                (dataframe["close"] >= dataframe["fib_1h_0.382"] * (1 - tolerance)) |
                (dataframe["close"] <= dataframe["fib_1h_0.382"] * (1 + tolerance)) |
                True  # 如果1小时数据不可用，仍然允许交易
            )
        
        # 综合卖出条件（简化逻辑）
        dataframe.loc[
            base_conditions &
            fib_conditions &
            rsi_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()
        
        # 基于ATR的动态止损
        atr = last_candle.get("atr", 0)
        if atr > 0:
            # 使用2倍ATR作为止损距离
            atr_stoploss = -2 * atr / current_rate
            if atr_stoploss > self.stoploss:
                return atr_stoploss
        
        # 基于斐波那契水平的止损调整
        if current_rate <= last_candle.get("fib_0.786", 0):
            return -0.03  # 3%止损（强支撑位跌破）
        
        if current_rate >= last_candle.get("fib_0.5", 0):
            return -0.01  # 1%止损（接近保本）
        
        # 基于盈利情况的止损调整
        if current_profit > 0.03:  # 盈利超过3%
            return -0.01  # 保本止损
        elif current_profit > 0.02:  # 盈利超过2%
            return -0.02  # 2%止损
        
        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()
        
        # 如果价格突破0.236斐波那契位且盈利良好，考虑部分止盈
        if (current_rate >= last_candle.get("fib_0.236", 0) and 
            current_profit > 0.02):
            return "fib_resistance_exit"
        
        # 如果RSI极度超买且盈利，考虑退出
        if (last_candle.get("rsi", 50) > 80 and 
            current_profit > 0.015):
            return "rsi_extreme_exit"
        
        return None
