为何Pine Script中plot的show_last=true?na:10表现为show_last=0而非na?
Pine Script v5中
show_last参数动态赋值的异常问题 在Pine Script v5中,使用plot(..., show_last = showHistory ? na : 10)设置参数时,实际表现等同于show_last = 0(隐藏整条线),而非预期的show_last = na(显示全部历史数据)。请在1H周期运行以下代码并修改输入参数测试:
//@version=5 indicator(title='PLOT show_last测试', shorttitle='show_last测试', overlay=true) showHistory = input.bool(true, title='显示历史数据') // 请求4小时周期的EMA值 FHFastEMA = request.security(syminfo.tickerid, '240', ta.ema(close, 240)) // 实时显示参数值 var lbl = label.new(bar_index, low, text = "(showHistory ? na : 1) = " + str.tostring(showHistory ? na : 1) + "\nshowHistory = " + str.tostring(showHistory), style = label.style_label_up, color = color.yellow, xloc = xloc.bar_index) lbl.set_xy(bar_index, low) // 有问题的动态赋值plot plot(FHFastEMA, title='4小时快速EMA(白色)', style=plot.style_line, color=color.white, linewidth = 4, show_last = showHistory ? na : 10) // 正常的静态na赋值plot plot(FHFastEMA, title='4小时快速EMA(黄色)', style=plot.style_line, color=color.yellow, show_last = na)
问题原因
show_last是plot函数的静态配置参数,仅支持编译时确定的常量值,无法在运行时动态传入na这类动态计算值。
当你使用三元表达式showHistory ? na : 10时,Pine Script会将动态返回的na判定为无效输入,自动 fallback 到show_last = 0的行为,直接隐藏整条线条。而直接写show_last = na属于静态赋值,此时na会被正确解析为“显示全部历史数据”(等同于不设置show_last参数),这也是黄色线条能正常显示的原因。
解决方法
不要通过动态表达式给show_last传值,改用条件分支分别调用plot函数,确保show_last的取值是静态常量:
// 替换原有的白色线条plot代码 if showHistory // 不设置show_last,默认显示全部历史 plot(FHFastEMA, title='4小时快速EMA(白色)', style=plot.style_line, color=color.white, linewidth=4) else // 静态设置show_last=10,仅显示最后10根K线的数值 plot(FHFastEMA, title='4小时快速EMA(白色)', style=plot.style_line, color=color.white, linewidth=4, show_last=10)
内容的提问来源于stack exchange,提问作者Moebius
相关产品推荐
相关产品推荐

