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

如何修复Pine Script中plotshape超出箱体左边界显示的问题?

解决方案

问题出在你每次K线都更新箱体位置,导致箱体成为滑动窗口,时间过滤条件失效,使得历史信号显示在当前箱体之外。

修改代码如下:

//@version=5
indicator("Ex", "", true)

// 自定义函数
func_1() =>
    (close < hl2[1] and close[1] < close[2]) ? 1 : 0

// 创建箱体(仅初始化一次)
var box_level = box.new(na, na, na, na, extend=extend.right, bgcolor=color.new(color.gray, 90), border_color=na)

// 仅在最后一根K线设置箱体位置,固定箱体范围
if barstate.islast
    box.set_lefttop(box_level, bar_index[300], 25700)
    box.set_rightbottom(box_level, bar_index, 15000)

// 获取箱体的固定边界
high_ = box.get_top(box_level)
low_ = box.get_bottom(box_level)
left_ = box.get_left(box_level)
right_ = box.get_right(box_level)

// 过滤条件:价格在箱体内,且K线在箱体时间范围内
in_box = not na(left_) and close > low_ and close < high_ and bar_index >= left_ and bar_index <= right_

// 仅满足自定义函数+箱体条件时显示形状
plotshape(func_1() and in_box, style=shape.triangleup, color=color.lime, location=location.belowbar, title="Bull", size=size.small)

关键修改点:

  • 把箱体位置设置放到if barstate.islast中,确保箱体仅在最新K线时定位,位置固定不随K线推进滑动。
  • 新增in_box变量整合所有过滤条件,明确限制K线索引在箱体左右边界之间,避免信号出现在箱体时间范围外。
  • 直接用func_1() and in_box作为plotshape的触发条件,逻辑更简洁清晰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 23:44:54