为何Higher Low功能错误标记K线?TradingView指标问题
TradingView 看涨隐藏背离指标误标记问题修复
问题背景
该指标用于识别TradingView中的看涨隐藏背离:当价格创出更高低点,但RSI同时创出更低低点时,将RSI线标记为红色。当前指标设置了4种级别(微小/中等/大/超大)的Pivot低点进行对比,但存在无理由的红色标记问题,需要排查修正。
原代码(含中文注释)
// 范围参数:仅对比最近5-300根K线内的Pivot rangeHigh = 300 rangeLow = 5 // 定义不同级别的价格Pivot低点 tinyLow = ta.pivotlow(5,5) midLow = ta.pivotlow(10,10) bigLow = ta.pivotlow(20,20) massiveLow = ta.pivotlow(40,40) // 定义不同级别的价格Pivot高点(未使用) tinyHigh = ta.pivothigh(5,5) midHigh = ta.pivothigh(10,10) bigHigh = ta.pivothigh(20,20) massiveHigh = ta.pivothigh(40,40) // 定义不同级别的RSI Pivot低点 RSItinyLow = ta.pivotlow(RSI,5,5) RSImidLow = ta.pivotlow(RSI,10,10) RSIbigLow = ta.pivotlow(RSI,20,20) RSImassiveLow = ta.pivotlow(RSI,40,40) // 定义不同级别的RSI Pivot高点(未使用) RSItinyHigh = ta.pivothigh(RSI,5,5) RSImidHigh = ta.pivothigh(RSI,10,10) RSIbigHigh = ta.pivothigh(RSI,20,20) RSImassiveHigh = ta.pivothigh(RSI,40,40) // 转换RSI Pivot为布尔值(存在则为true) tplF = na(RSItinyLow) ? false : true mplF = na(RSImidLow) ? false : true bplF = na(RSIbigLow) ? false : true maplF = na(RSImassiveLow) ? false : true tphF = na(RSItinyHigh) ? false : true mphF = na(RSImidHigh) ? false : true bphF = na(RSIbigHigh) ? false : true maphF = na(RSImassiveHigh) ? false : true // 转换价格Pivot为布尔值(存在则为true) tplFP = na(tinyLow) ? false : true mplFP = na(midLow) ? false : true bplFP = na(bigLow) ? false : true maplFP = na(massiveLow) ? false : true tphFP = na(tinyHigh) ? false : true mphFP = na(midHigh) ? false : true bphFP = na(bigHigh) ? false : true maphFP = na(massiveHigh) ? false : true // 判断指定条件是否发生在rangeLow-rangeHigh根K线内 _inRange(cond) => bars = ta.barssince(cond == true) rangeLow <= bars and bars <= rangeHigh // 看涨隐藏背离条件1:RSI创更低低点 rsiTLL = ta.lowest(RSI, 5) < ta.valuewhen(tplF,RSI[1],1) and _inRange(tplF[1]) rsiMLL = ta.lowest(RSI, 5) < ta.valuewhen(mplF,RSI[1],1) and _inRange(mplF[1]) rsiMLL2 = ta.lowest(RSI, 5) < ta.valuewhen(mplF,RSI[2],2) and _inRange(mplF[2]) rsiBLL = ta.lowest(RSI, 5) < ta.valuewhen(bplF,RSI[1],1) and _inRange(bplF[1]) rsiBLL2 = ta.lowest(RSI, 5) < ta.valuewhen(bplF,RSI[2],2) and _inRange(bplF[2]) rsiMALL = ta.lowest(RSI, 5) < ta.valuewhen(maplF,RSI[1],1) and _inRange(maplF[1]) rsiMALL2 = ta.lowest(RSI, 5) < ta.valuewhen(maplF,RSI[2],2) and _inRange(maplF[2]) // 看涨隐藏背离条件2:价格创更高低点 priceTHL = ta.lowest(low, 5) > ta.valuewhen(tplFP,low[1],1) and _inRange(tplFP[1]) // 此处存在BUG:误将mplFP的范围判断写成tplFP[1] priceMHL = ta.lowest(low, 5) > ta.valuewhen(mplFP,low[1],1) and _inRange(tplFP[1]) priceMHL2 = ta.lowest(low, 5) > ta.valuewhen(mplFP,low[2],2) and _inRange(mplFP[2]) priceBHL = ta.lowest(low, 5) > ta.valuewhen(bplFP,low[1],1) and _inRange(bplFP[1]) priceBHL2 = ta.lowest(low, 5) > ta.valuewhen(bplFP,low[2],2) and _inRange(bplFP[2]) priceMAHL = ta.lowest(low, 5) > ta.valuewhen(maplFP,low[1],1) and _inRange(maplFP[1]) // 组合所有看涨隐藏背离条件 hiddenBulltiny1 = rsiTLL and priceTHL hiddenBullmid1 = rsiMLL and priceMHL hiddenBullmid2 = rsiMLL2 and priceMHL2 hiddenBullbig1 = rsiBLL and priceBHL hiddenBullbig2 = rsiBLL2 and priceBHL2 hiddenBullmassive1 = rsiMALL and priceMAHL BullishHiddenDivergence = hiddenBulltiny1 or hiddenBullmid1 or hiddenBullmid2 or hiddenBullbig1 or hiddenBullbig2 or hiddenBullmassive1 rsiColor = color.white if BullishHiddenDivergence rsiColor := color.red plot(RSI,color=rsiColor)
问题根源分析
- 参数错误:
priceMHL中的_inRange(tplFP[1])应为_inRange(mplFP[1]),错误使用了微小级别Pivot的范围判断,导致中等级别条件被错误触发。 - 无低点限制:仅判断最近5根K线的RSI/价格极值,未限制当前K线是价格Pivot低点,导致非低点位置也可能满足条件触发标记。
- 范围判断漏洞:
_inRange函数未处理ta.barssince返回na的情况(当指定Pivot从未出现时),会导致条件误判为true。 - 极值对比逻辑模糊:
ta.lowest(RSI,5)取最近5根K线的最低RSI,未明确是当前K线的RSI创低,可能在区间内任意位置满足条件。
修正后的代码
// 范围参数:仅对比最近5-300根K线内的Pivot rangeHigh = 300 rangeLow = 5 // 定义不同级别的价格Pivot低点 tinyLow = ta.pivotlow(5,5) midLow = ta.pivotlow(10,10) bigLow = ta.pivotlow(20,20) massiveLow = ta.pivotlow(40,40) // 定义不同级别的RSI Pivot低点 RSItinyLow = ta.pivotlow(RSI,5,5) RSImidLow = ta.pivotlow(RSI,10,10) RSIbigLow = ta.pivotlow(RSI,20,20) RSImassiveLow = ta.pivotlow(RSI,40,40) // 转换为布尔值(存在则为true) tplF = not na(RSItinyLow) mplF = not na(RSImidLow) bplF = not na(RSIbigLow) maplF = not na(RSImassiveLow) tplFP = not na(tinyLow) mplFP = not na(midLow) bplFP = not na(bigLow) maplFP = not na(massiveLow) // 安全的范围判断:处理从未触发过的情况 _inRange(cond) => bars = ta.barssince(cond) not na(bars) and rangeLow <= bars and bars <= rangeHigh // 看涨隐藏背离核心逻辑:当前是价格低点,且与历史Pivot对比满足背离 // 微小级别 hiddenBulltiny = tplFP and RSI < ta.valuewhen(tplF, RSI, 1) and low > ta.valuewhen(tplFP, low, 1) and _inRange(tplF[1]) // 中等级别 hiddenBullmid1 = mplFP and RSI < ta.valuewhen(mplF, RSI, 1) and low > ta.valuewhen(mplFP, low, 1) and _inRange(mplF[1]) hiddenBullmid2 = mplFP and RSI < ta.valuewhen(mplF, RSI, 2) and low > ta.valuewhen(mplFP, low, 2) and _inRange(mplF[2]) // 大级别 hiddenBullbig1 = bplFP and RSI < ta.valuewhen(bplF, RSI, 1) and low > ta.valuewhen(bplFP, low, 1) and _inRange(bplF[1]) hiddenBullbig2 = bplFP and RSI < ta.valuewhen(bplF, RSI, 2) and low > ta.valuewhen(bplFP, low, 2) and _inRange(bplF[2]) // 超大级别 hiddenBullmassive1 = maplFP and RSI < ta.valuewhen(maplF, RSI, 1) and low > ta.valuewhen(maplFP, low, 1) and _inRange(maplF[1]) // 组合所有有效条件 BullishHiddenDivergence = hiddenBulltiny or hiddenBullmid1 or hiddenBullmid2 or hiddenBullbig1 or hiddenBullbig2 or hiddenBullmassive1 // 绘制RSI,满足条件时标记红色 plot(RSI, color=BullishHiddenDivergence ? color.red : color.white)
修正说明
- 修复参数错误:修正
priceMHL中的范围判断参数,确保每个级别使用对应Pivot的范围。 - 添加低点限制:所有背离条件都要求当前K线是对应级别的价格Pivot低点,避免非低点位置触发标记。
- 安全范围判断:在
_inRange中添加not na(bars)判断,防止从未出现过的Pivot导致误判。 - 明确对比逻辑:直接用当前K线的RSI和价格与历史Pivot对比,替代模糊的区间极值判断,确保背离条件精准。
内容的提问来源于stack exchange,提问作者Dev The Hall
相关产品推荐
相关产品推荐

