# source: https://raw.githubusercontent.com/xielk/freqtrade-test/925e067f080172a4581bc71ae9ee64be247606f4/user_data/strategies/TestStrategy.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__TestStrategy__20250910_084214(IStrategy):
    """
    测试策略 - 用于验证数据和基础逻辑
    """
    
    # Strategy interface version
    INTERFACE_VERSION = 3

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

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

    # 最小ROI设置
    minimal_roi = {
        "0": 0.01
    }

    # 止损设置
    stoploss = -0.10

    # 只处理新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 = 30

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

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

    def informative_pairs(self):
        return []

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        计算基础指标
        """
        # 计算RSI
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
        
        # 计算简单移动平均
        dataframe["sma_20"] = ta.SMA(dataframe, timeperiod=20)
        
        # 计算价格变化
        dataframe["price_change"] = dataframe["close"].pct_change()
        
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        极简买入信号 - 只要RSI < 50就买入
        """
        dataframe.loc[
            (
                (dataframe["rsi"] < 50) &
                (dataframe["volume"] > 0)
            ),
            "enter_long"
        ] = 1

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        极简卖出信号 - 只要RSI > 50就卖出
        """
        dataframe.loc[
            (
                (dataframe["rsi"] > 50) &
                (dataframe["volume"] > 0)
            ),
            "exit_long"
        ] = 1

        return dataframe
