RSI SMA交叉策略虚假信号修复:连续蜡烛确认机制开发
修改后的RSI-SMA连续交叉策略代码
以下是调整后的Pine Script代码,实现了连续两根K线RSI收盘价均高于SMA才触发买入信号,连续两根K线RSI收盘价均低于SMA才触发卖出信号的逻辑,过滤原策略中的虚假信号:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0 //@version=5 strategy('RSI Crossover Strategy', shorttitle='RSI Cross', default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, calc_on_every_tick=true) // inputs rsiLength = input(title='RSI长度', defval=50) smaLength = input(title='SMA长度', defval=25) // data/series rsi = ta.rsi(close, rsiLength) sma = ta.sma(rsi, smaLength) // plot plotColor = rsi > sma ? color.green : color.red rsiPlot = plot(rsi, color=color.new(plotColor, 0), title='RSI') smaPlot = plot(sma, color=color.new(#cc1111, 0), title='SMA') fillColor = rsi > sma ? color.lime : color.red fill(rsiPlot, smaPlot, color=color.new(fillColor, 75)) oversoldLine = hline(30, title='超卖水平') overboughtLine = hline(70, title='超买水平') // 策略逻辑修改 rsi_above_sma = rsi > sma rsi_below_sma = rsi < sma // 连续两根K线RSI收盘价高于SMA,且当前无多头持仓时触发买入 buy = rsi_above_sma and ta.rsi(close[1], rsiLength) > ta.sma(rsi, smaLength)[1] and not strategy.position_size > 0 // 连续两根K线RSI收盘价低于SMA,且当前持有多头持仓时触发卖出 sell = rsi_below_sma and ta.rsi(close[1], rsiLength) < ta.sma(rsi, smaLength)[1] and strategy.position_size > 0 if buy strategy.entry('Long', strategy.long, comment='买入') if sell strategy.close('Long', comment='卖出')
关键修改说明
- 新增
rsi_above_sma和rsi_below_sma变量,简化条件判断逻辑 - 买入信号新增前一根K线RSI收盘价高于SMA的检查,同时加入持仓状态判断,避免重复开仓
- 卖出信号同理,要求连续两根K线RSI收盘价均低于SMA,且当前持有多头仓位才执行平仓
- 将原代码中的英文注释、标题翻译成中文,适配国内用户使用习惯
内容的提问来源于stack exchange,提问作者Jay Desai
相关产品推荐
相关产品推荐

