Pine Script高低点识别指标无显示:高低比较逻辑问题求助
Pine Script 顶底识别指标修复:价格比较逻辑错误
问题原因
你代码中的价格高低比较逻辑存在根本性矛盾:
ta.highest(high, 12)计算的是最近12根K线的最高价,这个范围完全包含了最近3根K线(ta.highest(high, 3)的范围)- 因此
highest3 > highest12的条件永远无法成立(3周期最高价不可能大于包含它的12周期最高价),导致isTop和isBottom始终为false,图表无显示。
你的原意应该是最近3根K线创出了12周期内的新高/新低,而非“高于/低于”12周期最高价/最低价。
修正后的代码
以下是修复后的完整代码,调整了价格比较逻辑,同时保留你原本的其他条件:
//@version=5 indicator("My test indicator", overlay=true) // Volume MA maLength = 20 // Value for Volume MA volumeMA = ta.sma(volume, maLength) // 修正高低判断逻辑:最近3根K线创出12周期新高/新低 // 顶部条件1:最近3根最高价 > 前9根(12周期排除最近3根)的最高价 isHigher = ta.highest(high, 3) > ta.highest(high[3], 9) // 底部条件1:最近3根最低价 < 前9根(12周期排除最近3根)的最低价 isLower = ta.lowest(low, 3) < ta.lowest(low[3], 9) // 顶底识别条件 isTop = (close < low[1]) and (volume > volumeMA) and isHigher isBottom = (close > high[1]) and (volume > volumeMA) and isLower // Plotting plotshape(isTop, title="High", style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small) plotshape(isBottom, title="Low", style=shape.labelup, location=location.belowbar, color=color.green, size=size.small)
逻辑说明
- 修正后的
isHigher:比较最近3根K线的最高价,与**前9根K线(即12周期中排除最近3根的部分)**的最高价,确保最近3根确实创出了12周期内的新高 isLower对应底部逻辑,比较最近3根最低价与前9根最低价,确认12周期新低出现在最近3根- 保留了你原本的收盘价破前K线高低、成交量大于20均量的条件
内容的提问来源于stack exchange,提问作者quoctrungpt
相关产品推荐
相关产品推荐

