如何显示box.new缺口点数并隐藏已回补缺口的标签?
Pine Script 缺口脚本优化方案
问题现状
现有缺口显示脚本可通过box.new正常绘制缺口,但存在两个未实现的需求:
- 已回补的缺口标签无法自动隐藏
- 无法显示缺口上下沿的点数(pips)
核心优化点及实现代码
1. 自动隐藏已回补缺口的标签
解决思路:将每个缺口的标签与对应的box绑定存储在数组中,当检测到缺口回补并删除box时,同步删除对应标签。
2. 显示缺口上下沿点数
解决思路:通过syminfo.mintick获取品种最小变动单位,结合品种类型(JPY/非JPY)计算缺口点数,将其添加到标签文本中。
修改后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ArthurBuyStock //@version=5 indicator("Gap", overlay = true) b = request.security(syminfo.tickerid, 'W', bar_index, lookahead = barmerge.lookahead_on) h = request.security(syminfo.tickerid, 'D', high[1], lookahead = barmerge.lookahead_on) l = request.security(syminfo.tickerid, 'D', low[1], lookahead = barmerge.lookahead_on) // 新增:存储缺口标签的数组,与box数组一一对应 var label[] gapLabels = array.new_label() string nintendo = input.string("Intuition", "Type", options = ["True", "Fix", "Intuition"]) var box[] bricks = array.new_box() // 适配点数计算:JPY品种1点=0.01,其他品种1点=0.0001 pipMultiplier = syminfo.mintick == 0.01 ? 1 : 100 switch nintendo "True" => if b != b[1] if open > high[1] boxId = box.new(bar_index - 1, open, bar_index, close[1], na, extend = extend.right, bgcolor = color.green) array.unshift(bricks, boxId) // 计算缺口点数并创建标签 gapPips = (open - close[1]) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index, na, str.format("Bull Gap: %.2f pips", gapPips), color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar) array.unshift(gapLabels, labelId) else if open < low[1] boxId = box.new(bar_index - 1, close[1], bar_index, open, na, extend = extend.right, bgcolor = color.red) array.unshift(bricks, boxId) gapPips = (close[1] - open) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index-1, na, str.format("Bear Gap: %.2f pips", gapPips), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar) array.unshift(gapLabels, labelId) "Fix" => if b != b[1] if open > h boxId = box.new(bar_index - 1, open, bar_index, close[1], na, extend = extend.right, bgcolor = na) array.unshift(bricks, boxId) gapPips = (open - close[1]) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index, na, str.format("Bull Gap: %.2f pips", gapPips), color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar) array.unshift(gapLabels, labelId) else if open < l boxId = box.new(bar_index - 1, close[1], bar_index, open, na, extend = extend.right, bgcolor = na) array.unshift(bricks, boxId) gapPips = (close[1] - open) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index-1, na, str.format("Bear Gap: %.2f pips", gapPips), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar) array.unshift(gapLabels, labelId) "Intuition" => if open > high[1] boxId = box.new(left= bar_index - 1, top= open, right= bar_index, bottom = high[1], border_width=1, extend = extend.right, border_color = color.new(color.green, 100)) array.unshift(bricks, boxId) gapPips = (open - high[1]) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index, na, str.format("Bull Gap: %.2f pips", gapPips), color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar) array.unshift(gapLabels, labelId) else if open < low[1] boxId = box.new(left= bar_index - 1, top= low[1], right=bar_index, bottom= open, border_width=1, extend = extend.right, border_color = color.new(color.red, 100)) array.unshift(bricks, boxId) gapPips = (low[1] - open) / syminfo.mintick / pipMultiplier labelId = label.new(bar_index-1, na, str.format("Bear Gap: %.2f pips", gapPips), color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar) array.unshift(gapLabels, labelId) // 缺口回补检测:同步删除box和对应标签 if array.size(bricks) > 0 if array.size(bricks) == 1 boxId = array.get(bricks,0) if high >= box.get_top(boxId) and low <= box.get_bottom(boxId) box.delete(boxId) label.delete(array.get(gapLabels,0)) array.remove(bricks,0) array.remove(gapLabels,0) else if b == b[1] if (low[1] <= box.get_bottom(boxId) and high >= box.get_top(boxId)) or (high[1] >= box.get_top(boxId) and low <= box.get_bottom(boxId)) box.delete(boxId) label.delete(array.get(gapLabels,0)) array.remove(bricks,0) array.remove(gapLabels,0) else t = 0 for i = 0 to array.size(bricks) - 1 boxId = array.get(bricks,i - t) labelId = array.get(gapLabels,i - t) if high >= box.get_top(boxId) and low <= box.get_bottom(boxId) box.delete(boxId) label.delete(labelId) array.remove(bricks,i - t) array.remove(gapLabels,i - t) t := t + 1 else if i > 0 if (low[1] <= box.get_bottom(boxId) and high >= box.get_top(boxId)) or (high[1] >= box.get_top(boxId) and low <= box.get_bottom(boxId)) box.delete(boxId) label.delete(labelId) array.remove(bricks,i - t) array.remove(gapLabels,i - t) t := t + 1 // 缺口box更新逻辑(保留原逻辑) if array.size(bricks) > 0 r = 0 g = 0 for i = 0 to array.size(bricks) - 1 boxId = array.get(bricks,i) if high < box.get_top(boxId) and low > box.get_bottom(boxId) if high[1] <= box.get_bottom(boxId) box.set_bottom(boxId, high) if low[1] >= box.get_top(boxId) box.set_top(boxId, low) else if box.get_top(boxId) > low and low > box.get_bottom(boxId) box.set_top(boxId, low) if box.get_top(boxId) > high and high > box.get_bottom(boxId) box.set_bottom(boxId, high) if close >= box.get_top(boxId) box.set_bgcolor(boxId, color.new(color.green, 50)) g := g + 20 else if close <= box.get_bottom(boxId) box.set_bgcolor(boxId, color.new(color.red, 50)) r := r + 20
关键优化说明
- 标签与box绑定:新增
gapLabels数组,每个box创建时同步生成对应标签并加入数组,删除box时同步删除标签 - 点数适配计算:通过
pipMultiplier自动适配JPY与非JPY品种的点数规则,用str.format格式化点数显示精度 - 回补同步处理:在所有缺口回补的判断分支中,均添加了标签删除逻辑,确保已回补缺口的标签自动隐藏
内容的提问来源于stack exchange,提问作者tomcraziest
相关产品推荐
相关产品推荐

