如何实现2分钟EMA交叉后结合10分钟RSI均线交叉输出对应信号?
问题描述
我编写的Pine Script在2分钟K线图上,当9周期EMA与50周期EMA交叉时会交替生成多/空信号,该功能运行正常。现在需要实现:
- 仅当10分钟K线图上的RSI向上交叉其均线时,才输出2分钟图的多单信号
- 仅当10分钟K线图上的RSI向下交叉其均线时,才输出2分钟图的空单信号
我尝试多种方法均未成功,已定义rsi_trigger变量,但不知如何将其整合到longsignal/shortsignal的判断逻辑中,原代码如下:
//EMAs ema9 = ta.ema(close, input.int(9, 'EMA9')) ema50 = ta.ema(close, input.int(50, 'EMA50')) //RSI rsi15 = ta.rsi(close, 15) //RSI Moving Average rsi20_moving_avg = ta.rma(close, 20) ten_min_rsi_15 = request.security(syminfo.tickerid, '10', rsi15) ten_min_rsi_ma_20 = request.security(syminfo.tickerid, '10', rsi20_moving_avg) // we create a variable that "saves" and doesnt calc on each bar var cross_trigger = 0 var rsi_trigger = 0 // we save it to a new number when long happens. Long can be 1 if ta.crossover(ema9, ema50) and cross_trigger <= 0 cross_trigger := 1 // we save it again when short happens. Short can be -1 if ta.crossover(ema50, ema9) and cross_trigger >= 0 cross_trigger := -1 if (ten_min_rsi_15 > ten_min_rsi_ma_20) and rsi_trigger <= 0 rsi_trigger := 1 if (ten_min_rsi_15 < ten_min_rsi_ma_20) and rsi_trigger >= 0 rsi_trigger := -1 // here we check if we have a newly detetected change from another number to our pos number this bar // Is pos equal to 1 and was it not equal to 1 one bar ago longsignal = cross_trigger == 1 and (cross_trigger != 1)[1] shortsignal = cross_trigger == -1 and (cross_trigger != -1)[1] //Plot // we change our plot shape to coincide with the change detection plotshape(longsignal, style=shape.triangleup, color=#26a69a, text="LONG", textcolor=#ffffff, location=location.bottom, size=size.small) plotshape(shortsignal, style=shape.triangledown, color=#ef5350, text="SHORT", textcolor=#ffffff, location=location.top, size=size.small) // Alerts alertcondition(longsignal, title='CALL', message='CALL') alertcondition(shortsignal, title='PUT', message='PUT')
解决方案
核心问题是当前的rsi_trigger仅记录了10分钟RSI与均线的位置关系,未捕捉交叉动作,且未将EMA交叉信号与对应的RSI交叉条件绑定。以下是修改后的代码:
//EMAs ema9 = ta.ema(close, input.int(9, 'EMA9')) ema50 = ta.ema(close, input.int(50, 'EMA50')) //RSI与均线计算 rsi15 = ta.rsi(close, 15) rsi20_moving_avg = ta.rma(rsi15, 20) // 修正:原代码用close计算RSI均线,应改用RSI值本身 // 获取10分钟周期的RSI交叉信号 ten_min_rsi_cross_up = request.security(syminfo.tickerid, '10', ta.crossover(rsi15, rsi20_moving_avg)) ten_min_rsi_cross_down = request.security(syminfo.tickerid, '10', ta.crossunder(rsi15, rsi20_moving_avg)) // 记录EMA交叉的触发状态,仅在对应RSI交叉条件满足时更新 var cross_trigger = 0 // 多单触发:EMA9上穿EMA50 + 10分钟RSI上穿均线 if ta.crossover(ema9, ema50) and cross_trigger <= 0 and ten_min_rsi_cross_up cross_trigger := 1 // 空单触发:EMA50上穿EMA9 + 10分钟RSI下穿均线 if ta.crossover(ema50, ema9) and cross_trigger >= 0 and ten_min_rsi_cross_down cross_trigger := -1 // 检测状态变化,生成最终信号 longsignal = cross_trigger == 1 and cross_trigger[1] != 1 shortsignal = cross_trigger == -1 and cross_trigger[1] != -1 // 绘图 plotshape(longsignal, style=shape.triangleup, color=#26a69a, text="LONG", textcolor=#ffffff, location=location.bottom, size=size.small) plotshape(shortsignal, style=shape.triangledown, color=#ef5350, text="SHORT", textcolor=#ffffff, location=location.top, size=size.small) // 警报 alertcondition(longsignal, title='CALL', message='CALL') alertcondition(shortsignal, title='PUT', message='PUT')
关键修改说明
- 修正RSI均线计算:原代码误用
close计算RSI的均线,改为用RSI指标本身的值rsi15计算,逻辑更合理 - 捕捉10分钟RSI交叉事件:直接在跨周期请求中调用
ta.crossover和ta.crossunder,精准获取10分钟周期上的RSI交叉信号 - 绑定EMA与RSI交叉条件:更新
cross_trigger时,必须同时满足EMA交叉和对应的10分钟RSI交叉条件,确保两个周期信号共振才触发 - 保留原有信号去重逻辑:继续通过
cross_trigger的状态变化检测最终信号,避免连续输出重复信号
内容的提问来源于stack exchange,提问作者user12351824
相关产品推荐
相关产品推荐

