Pine Script V5:request.security()获取高时间框架数据绘图偏移问题
修正Pine Script V5中request.security()跨时间框架指标偏移问题
问题根源
你遇到的偏移是request.security()的默认行为导致的:默认情况下,它会把更高时间框架的指标值提前分配到当前时间框架中属于该高时间周期的第一根K线。比如1H周期的RSI,会在30分钟图的02:30(1H周期的第一根30分K线)就显示,而不是等到1H周期结束的03:00 K线。
修正方案
给request.security()添加barmerge.gaps_on和barmerge.lookahead_off参数,强制指标值只在对应高时间框架K线闭合后,显示在当前时间框架的对应位置:
//@version=5 indicator("跨时间框架RSI", overlay=false) rsiLengthInput = input.int(14, title="RSI长度") // 添加barmerge参数修正偏移 rsi_1W = request.security(syminfo.tickerid, "W", ta.rsi(close, rsiLengthInput), barmerge.gaps_on, barmerge.lookahead_off) rsi_1D = request.security(syminfo.tickerid, "D", ta.rsi(close, rsiLengthInput), barmerge.gaps_on, barmerge.lookahead_off) rsi_4H = request.security(syminfo.tickerid, "240", ta.rsi(close, rsiLengthInput), barmerge.gaps_on, barmerge.lookahead_off) rsi_1H = request.security(syminfo.tickerid, "60", ta.rsi(close, rsiLengthInput), barmerge.gaps_on, barmerge.lookahead_off) // 绘图 plot(rsi_1W, "RSI (1W)", color.rgb(0, 100, 255)) plot(rsi_1D, "RSI (1D)", color.rgb(0, 100, 200)) plot(rsi_4H, "RSI (4H)", color.rgb(0, 100, 150)) plot(rsi_1H, "RSI (1H)", color.rgb(0, 100, 100))
参数说明
barmerge.gaps_on:保留不同时间框架K线对齐的间隙,不让高时间框架的指标值提前填充到当前时间框架的早期K线barmerge.lookahead_off:完全关闭前瞻功能,确保只使用已闭合的高时间框架K线数据,避免提前显示未完成周期的指标值
这样修改后,1H的RSI会在30分钟图的03:00 K线(对应1H周期闭合的时间点)显示,完全匹配你需要的位置,同时不会引入未来数据破坏绘图逻辑。
内容的提问来源于stack exchange,提问作者alexeyprog
相关产品推荐
相关产品推荐

