You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Pine Script中基于RSI与EMA交叉的条件延迟下单策略的技术问询

关于Pine Script中基于RSI与EMA交叉的条件延迟下单策略的技术问询

嘿,我完全明白你想要实现的策略逻辑——在RSI触发超卖/超买信号后,等待接下来1-3根K线内出现EMA的交叉信号再执行开仓操作对吧?这是个很实用的过滤型策略,我来帮你把代码补全并拆解清楚每个环节:

首先,先明确你的核心需求(我再复述一遍确保理解无误):

  • 做多逻辑:当RSI来到30(超卖区间)后,在随后的第1到第3根K线收盘时,若出现EMA20上穿EMA50的信号,就执行多单入场
  • 做空逻辑:当RSI来到70(超买区间)后,在随后的第1到第3根K线收盘时,若出现EMA20下穿EMA50的信号,就执行空单入场

接下来是修改后的完整Pine Script代码,我会给关键部分加上注释:

//@version=5
strategy("RSI-EMA Delay Entry Strategy", overlay=true, margin_long=100, margin_short=100)

// 定义EMA指标
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
emalong = ta.crossover(ema20, ema50)  // EMA20上穿EMA50
emashort = ta.crossunder(ema20, ema50) // EMA20下穿EMA50

// 定义RSI指标(保留你原来的RSI自定义逻辑)
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

// 关键:跟踪RSI触发超卖/超买后的K线计数
// 使用var变量确保只初始化一次,保存最近的触发K线索引
var int rsiOversoldBar = 0  // 记录RSI进入超卖(<=30)的K线位置
var int rsiOverboughtBar = 0 // 记录RSI进入超买(>=70)的K线位置

// 更新超卖触发点:每次RSI<=30时,更新为当前K线索引
if (rsi <= 30)
    rsiOversoldBar = bar_index

// 更新超买触发点:每次RSI>=70时,更新为当前K线索引
if (rsi >= 70)
    rsiOverboughtBar = bar_index

// 计算当前K线距离触发点的间隔数
barsSinceOversold = bar_index - rsiOversoldBar
barsSinceOverbought = bar_index - rsiOverboughtBar

// 定义最终的开仓条件
longCondition = emalong and (barsSinceOversold >= 1) and (barsSinceOversold <= 3)
shortCondition = emashort and (barsSinceOverbought >= 1) and (barsSinceOverbought <= 3)

// 执行策略入场
if (longCondition)
    strategy.entry("Long Entry", strategy.long)

if (shortCondition)
    strategy.entry("Short Entry", strategy.short)

// 可选:绘制EMA方便观察
plot(ema20, color=color.new(color.blue, 0), title="EMA20")
plot(ema50, color=color.new(color.red, 0), title="EMA50")

关键逻辑解释:

  1. 触发点跟踪:用var类型的变量rsiOversoldBar和rsiOverboughtBar来记录最近一次RSI进入超卖/超买区间的K线索引,这样每次新的触发信号会覆盖旧的,确保我们只跟踪最新的一次机会。
  2. 间隔K线计算:通过bar_index - 触发点索引得到当前距离触发信号的K线数,判断是否在1-3的范围内。
  3. 条件组合:把EMA交叉信号和K线间隔条件结合起来,只有同时满足时才触发开仓,完美匹配你想要的延迟入场逻辑。

如果你需要调整RSI的触发阈值(比如改成RSI下穿30才触发),只需要把rsi <=30改成ta.crossunder(rsi, 30)即可,同理超买可以改成ta.crossover(rsi,70),根据你的实际交易习惯调整就行。

备注:内容来源于stack exchange,提问作者STH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 09:29:33