Files
cta/user_data/strategies/SimpleRSIStrategyOptimized.py
2025-10-25 17:19:16 +08:00

66 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import talib.abstract as ta
import pandas as pd
import numpy as np
from freqtrade.strategy import IStrategy
import logging
logger = logging.getLogger(__name__)
class SimpleRSIStrategyOptimized(IStrategy):
INTERFACE_VERSION = 3
can_short: bool = False
stoploss = -0.05
minimal_roi = {"0": 100} # 由止盈逻辑替代
timeframe = '1d'
# === 策略参数(可优化)===
ema_short = 10
ema_long = 20
rsi_period = 6
rsi_oversold = 20
def populate_indicators(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
# EMA
dataframe['ema_short'] = ta.EMA(dataframe['close'], timeperiod=self.ema_short)
dataframe['ema_long'] = ta.EMA(dataframe['close'], timeperiod=self.ema_long)
# RSI
dataframe['rsi'] = ta.RSI(dataframe['close'], timeperiod=self.rsi_period)
# 辅助昨日值shift 1
dataframe['ema_short_prev'] = dataframe['ema_short'].shift(1)
dataframe['rsi_prev'] = dataframe['rsi'].shift(1)
return dataframe
def populate_entry_trend(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
dataframe.loc[
(
# 趋势条件EMA 短期 > 长期 且 短期 EMA 上升
(dataframe['ema_short'] > dataframe['ema_long']) &
(dataframe['ema_short'] > dataframe['ema_short_prev']) &
# RSI 超卖后反弹
(dataframe['rsi_prev'] <= self.rsi_oversold) &
(dataframe['rsi'] > dataframe['rsi_prev'])
),
'enter_long',
] = 1
return dataframe
def populate_exit_trend(self, dataframe: pd.DataFrame, metadata: dict) -> pd.DataFrame:
dataframe.loc[
(
# 趋势破坏EMA 死叉
(dataframe['ema_short'] < dataframe['ema_long']) |
# 或价格跌破长期 EMA支撑失效
(dataframe['close'] < dataframe['ema_long'])
),
'exit_long',
] = 1
return dataframe