66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
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 |