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

Pine Script如何在M15/M5图表展示H1周期RSI背离(不可用study的resolution参数)

解决方案

核心修改逻辑

  • 通过security()函数提前获取H1周期的K线数据和对应计算好的RSI值,替换原有代码中当前周期的close/high/low/rsi变量即可,不需要改动原有背离检测的核心逻辑
  • 调用security()时添加barmerge.lookahead_off参数,避免未来函数问题,符合策略回测的准确性要求

完整修改后代码

//@version=4
study("rsi divergence", overlay=false)

//-----Input: divergence-----------------------------------------------------------\\
rsi_len      = input(title="rsi len",     type=input.integer, defval=14)    
pivot_right  = input(title="Pivot right", type=input.integer, defval=5)     
pivot_left   = input(title="Pivot left",  type=input.integer, defval=5)     
max_range    = input(title="Max range",   type=input.integer, defval=50)   
min_range    = input(title="Min range",   type=input.integer, defval=5)    
rsi_ob       = input(title="RSI OB",      type=input.integer, defval=70)   
rsi_os       = input(title="RSI OS",      type=input.integer, defval=30)
// 新增:目标周期输入,默认60分钟即H1
target_res   = input(title="目标周期", type=input.resolution, defval="60")

//-----Declaration: 获取H1周期数据-------------------------------------------------------------\\
[h1_close, h1_high, h1_low, h1_rsi] = security(syminfo.tickerid, target_res, [close, high, low, rsi(close, rsi_len)], barmerge.lookahead_off)

//-----Plot: rsi--------------------------------------------------------------------\\
plot(h1_rsi, title="H1 RSI", color=color.yellow)
hline(50, linestyle=hline.style_dotted)
ob_line = hline(rsi_ob, linestyle=hline.style_dotted, color=color.white)
os_line = hline(rsi_os, linestyle=hline.style_dotted, color=color.white)
fill(ob_line, os_line, color=color.fuchsia)

//-----Check: pivot low-------------------------------------------------------------\\
pivot_low  = na(pivotlow(h1_rsi, pivot_left, pivot_right))  ? false : true 
pivot_high = na(pivothigh(h1_rsi, pivot_left, pivot_right)) ? false : true 

//-----Declaration: range rsi-------------------------------------------------------------\\
confirm_range(x) =>
    bars = barssince(x == true) 
    min_range <= bars and bars <= max_range 

//-----Declaration: divergences-------------------------------------------------------------\\
//long
rsi_check_long   = h1_rsi[pivot_right] > valuewhen(pivot_low, h1_rsi[pivot_right], 1) and confirm_range(pivot_low[1])
price_check_long = h1_low[pivot_right] < valuewhen(pivot_low, h1_low[pivot_right], 1)

//short
rsi_check_short   = h1_rsi[pivot_right]  < valuewhen(pivot_high,  h1_rsi[pivot_right], 1) and confirm_range(pivot_high[1])
price_check_short = h1_high[pivot_right] > valuewhen(pivot_high, h1_high[pivot_right], 1)

//-----Conditions-------------------------------------------------------------\\
div_bullCond = price_check_long  and rsi_check_long  and pivot_low
div_bearCond = price_check_short and rsi_check_short and pivot_high

//-----Plot: divergences-------------------------------------------------------------\\
//long
plot(pivot_low ? h1_rsi[pivot_right] : na, offset=-pivot_right, linewidth=3, 
     color=(div_bullCond ? color.green : color.new(color.white, 100)))
plotshape(div_bullCond   ? h1_rsi[pivot_right] : na, offset=-pivot_right, text=" LONG ",
     style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white)
     
//short
plot(pivot_high ? h1_rsi[pivot_right] : na, offset=-pivot_right, linewidth=3, 
     color=(div_bearCond ? color.red : color.new(color.white, 100)))
plotshape(div_bearCond   ? h1_rsi[pivot_right] : na, offset=-pivot_right, text=" SHORT ",
     style=shape.labelup, location=location.absolute, color=color.red, textcolor=color.white)

注意事项

  • 由于背离检测需要pivot_right参数对应的K线数确认 pivot 点,信号会延迟对应数量的H1周期K线,属于正常的检测逻辑,无未来函数问题
  • 如果你需要切换其他大周期的背离检测,直接修改输入项里的「目标周期」参数即可,比如改成240就是H4周期的信号

内容的提问来源于stack exchange,提问作者stefano introini

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 14:54:01