# 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 RSIReversalStrategy(IStrategy): """ RSI反转策略 - 基于RSI极值反转 """ # Strategy interface version INTERFACE_VERSION = 3 # 策略时间框架 timeframe = "15m" # 是否支持做空 can_short: bool = False # 最小ROI设置 minimal_roi = { "0": 0.01 } # 止损设置 stoploss = -0.08 # 只处理新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) # 计算RSI的移动平均 dataframe["rsi_sma"] = dataframe["rsi"].rolling(window=5).mean() # 计算价格变化 dataframe["price_change"] = dataframe["close"].pct_change() # 计算EMA dataframe["ema_20"] = ta.EMA(dataframe, timeperiod=20) return dataframe def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ 基于RSI反转的买入信号 """ dataframe.loc[ ( # RSI从超卖区域反弹 (dataframe["rsi"] < 45) & (dataframe["rsi"] > dataframe["rsi"].shift(1)) & # RSI上升 # 价格开始上涨 (dataframe["close"] > dataframe["close"].shift(1)) & # 有成交量 (dataframe["volume"] > 0) & # 确保指标已计算 (dataframe["rsi"].notna()) ), "enter_long" ] = 1 return dataframe def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ 基于RSI反转的卖出信号 """ dataframe.loc[ ( # RSI从超买区域回落 (dataframe["rsi"] > 55) & (dataframe["rsi"] < dataframe["rsi"].shift(1)) & # RSI下降 # 价格开始下跌 (dataframe["close"] < dataframe["close"].shift(1)) & # 有成交量 (dataframe["volume"] > 0) & # 确保指标已计算 (dataframe["rsi"].notna()) ), "exit_long" ] = 1 return dataframe