为何仅能绘制highestValue?盘前90分钟高低值捕获问题求助
问题分析与解决
你的代码中最低值无法显示的核心原因是初始值为na时的条件判断逻辑错误:
当lowestValue初始为na时,low < lowestValue的运算结果是na,而Pine Script中na or na的逻辑判断结果仍为na,导致第一个K线进入时间范围时,lowestValue无法被赋值,始终保持na,最终无法绘制。
解决方法1:修正条件判断逻辑
调整lowestValue的赋值条件,优先判断是否为na(利用逻辑或的短路特性,避免触发无效的数值比较):
//@version=4 study(title="Range before open", overlay=true) inTimeRange = time("1", "1359-1530:23456", "GMT+1") var float highestValue = na var float lowestValue = na if inTimeRange // 最高值逻辑保持不变 if na(highestValue) or high > highestValue highestValue := high // 调整最低值判断顺序,先检查是否为na if na(lowestValue) or low < lowestValue lowestValue := low high_low_range_middle = (highestValue + lowestValue) / 2 high_low_range = highestValue - lowestValue range_offset = high_low_range / 3 upper_bound = highestValue + range_offset lower_bound = lowestValue - range_offset plot(highestValue, title="High of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(lowestValue, title="Low of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(high_low_range_middle, title="Middle of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(upper_bound, title="Upper bound of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(lower_bound, title="Lower bound of the Day (14:00-15:30)", color=color.white, linewidth=1)
解决方法2:使用内置函数简化逻辑
直接利用Pine Script的ta.highest()和ta.lowest()函数,自动维护时间范围内的高低值,代码更简洁且不易出错:
//@version=4 study(title="Range before open", overlay=true) // 定义时间范围 inTimeRange = time("1", "1359-1530:23456", "GMT+1") // 仅在时间范围内计算高低值,超出范围后保留最后值 var float highestValue = na var float lowestValue = na if inTimeRange highestValue := ta.highest(high, barssince(not inTimeRange) + 1) lowestValue := ta.lowest(low, barssince(not inTimeRange) + 1) high_low_range_middle = (highestValue + lowestValue) / 2 high_low_range = highestValue - lowestValue range_offset = high_low_range / 3 upper_bound = highestValue + range_offset lower_bound = lowestValue - range_offset plot(highestValue, title="High of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(lowestValue, title="Low of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(high_low_range_middle, title="Middle of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(upper_bound, title="Upper bound of the Day (14:00-15:30)", color=color.white, linewidth=1) plot(lower_bound, title="Lower bound of the Day (14:00-15:30)", color=color.white, linewidth=1)
内容的提问来源于stack exchange,提问作者Tristan de Roo
相关产品推荐
相关产品推荐

