结合rsi回调止损
This commit is contained in:
parent
b049c3c1ce
commit
9140e25e57
@ -285,6 +285,19 @@ class OKXRegressionStrategy(IStrategy):
|
||||
if dataframe is None:
|
||||
dataframe = DataFrame()
|
||||
dataframe['ATR_{}'.format(self.atr_period.value)] = ta.ATR(dataframe['high'], dataframe['low'], dataframe['close'], timeperiod=self.atr_period.value)
|
||||
# 添加 RSI 和布林带指标
|
||||
dataframe['rsi'] = ta.RSI(dataframe['close'], timeperiod=14)
|
||||
upper, middle, lower = ta.BBANDS(dataframe['close'], timeperiod=20, nbdevup=2, nbdevdn=2)
|
||||
dataframe['bb_upper'] = upper
|
||||
dataframe['bb_middle'] = middle
|
||||
dataframe['bb_lower'] = lower
|
||||
|
||||
# 添加高时间框架数据(例如 1h)
|
||||
if self.config['timeframe'] != '1h':
|
||||
dataframe_1h = self.dp.get_analyzed_dataframe(metadata['pair'], '1h')[0]
|
||||
dataframe['trend_1h'] = dataframe_1h['close'].rolling(window=20).mean()
|
||||
else:
|
||||
dataframe['trend_1h'] = dataframe['close'].rolling(window=20).mean()
|
||||
return dataframe
|
||||
|
||||
|
||||
@ -560,3 +573,39 @@ class OKXRegressionStrategy(IStrategy):
|
||||
except Exception as e:
|
||||
logger.error(f"FreqAI fit 失败:{str(e)}")
|
||||
raise
|
||||
def _callback_stop_loss(self, dataframe: DataFrame, metadata: dict, callback_percent: float = 0.015) -> DataFrame:
|
||||
"""
|
||||
回调止损逻辑:当价格从近期高点回撤超过指定百分比,
|
||||
并结合 RSI 或布林带信号,同时考虑高时间框架趋势。
|
||||
"""
|
||||
|
||||
# 计算滚动最高价(过去 N 根K线内的最高点)
|
||||
rolling_high_period = 20
|
||||
dataframe['rolling_high'] = dataframe['close'].rolling(window=rolling_high_period).max()
|
||||
|
||||
# 计算当前价格相对于最近高点的回撤比例
|
||||
dataframe['callback_ratio'] = (dataframe['close'] - dataframe['rolling_high']) / dataframe['rolling_high']
|
||||
|
||||
# 获取 RSI 和布林带信息
|
||||
rsi_overbought = 70
|
||||
dataframe['in_overbought'] = dataframe['rsi'] > rsi_overbought
|
||||
dataframe['below_bb_upper'] = dataframe['close'] < dataframe['bb_upper']
|
||||
|
||||
# 获取高时间框架趋势(1小时均线)
|
||||
dataframe['trend_up'] = dataframe['close'] > dataframe['trend_1h']
|
||||
dataframe['trend_down'] = dataframe['close'] < dataframe['trend_1h']
|
||||
|
||||
# 回调止损条件:
|
||||
# 1. 当前价格回撤超过设定的百分比
|
||||
# 2. RSI 处于超买状态 OR 价格跌破布林带上轨
|
||||
# 3. 当前处于下降趋势(高时间框架确认)
|
||||
callback_condition = (
|
||||
(dataframe['callback_ratio'] <= -callback_percent) &
|
||||
((dataframe['in_overbought'] | (~dataframe['below_bb_upper']))) &
|
||||
(dataframe['trend_down'])
|
||||
)
|
||||
|
||||
# 应用回调止损逻辑
|
||||
dataframe.loc[callback_condition, 'exit_long'] = 1
|
||||
|
||||
return dataframe
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user