You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何让r1在longEntryFilter为true时停止绘制?

解决Pine Script中根据过滤器控制绘图显示的问题

问题分析

你原来的代码中,fixnan(ta.pivothigh(15, 15)[1])会将ta.pivothigh返回的na替换为前一个有效值,这导致即使后续给highUsePivot赋值na,之前的bar仍然会保留fixnan处理后的非na值,所以绘图依然会显示。此外,plot函数的color参数判断也可能干扰显示逻辑。

解决方案1:直接在plot函数中控制显示(推荐)

最简洁的方式是直接在plot的第一个参数中,根据longEntryFilter决定传入的值:当过滤器为true时传入na,此时plot不会绘制任何内容;过滤器为false时传入计算好的highUsePivot。

修改后的代码:

var bool longEntryFilter = na
var bool shortEntryFilter = na

if longEntry
    longEntryFilter := true
    shortEntryFilter := false
else if shortEntry
    longEntryFilter := false
    shortEntryFilter := true

highUsePivot = fixnan(ta.pivothigh(15, 15)[1])

// 根据longEntryFilter控制是否绘制
r1 = plot(longEntryFilter ? na : highUsePivot, 
          color=ta.change(highUsePivot) ? na : #00bfff81, 
          linewidth=1, 
          offset=-(15 + 1), 
          style=plot.style_circles)

解决方案2:调整fixnan的处理顺序

如果你坚持要通过修改highUsePivot的值来实现,需要调整fixnan的执行时机,确保只在过滤器为false时才补全na值:

var bool longEntryFilter = na
var bool shortEntryFilter = na

if longEntry
    longEntryFilter := true
    shortEntryFilter := false
else if shortEntry
    longEntryFilter := false
    shortEntryFilter := true

// 先获取原始pivothigh值
highUsePivot = ta.pivothigh(15, 15)[1]

// 过滤器生效时设为na,否则用fixnan补全
if longEntryFilter
    highUsePivot := na
else
    highUsePivot := fixnan(highUsePivot)

r1 = plot(highUsePivot, 
          color=ta.change(highUsePivot) ? na : #00bfff81, 
          linewidth=1, 
          offset=-(15 + 1), 
          style=plot.style_circles)

说明

  • 方案1更高效直接,利用Pine Script中plot函数遇到na值不绘制的特性,避免了额外的变量赋值逻辑。
  • 方案2通过调整fixnan的执行顺序,确保过滤器生效时highUsePivot始终为na,从而实现不绘制的效果。

内容的提问来源于stack exchange,提问作者Parabolic1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:47:26