如何在Pine Script中向数组推入Box但不在图表显示?
解决方案:存储Box参数而非实际Box对象
你遇到的问题根源在于:只要调用box.new()创建对象,哪怕设置全透明,图表仍会渲染空框;而删除box会丢失后续计算需要的边界数据。正确的思路是不创建实际的box对象,而是将box的关键参数(位置、高低)存入数组,既保留计算逻辑所需的数据,又不会在图表上生成任何元素。
修改步骤:
- 替换box类型数组为存储参数的元组数组
- 检测到OB时,将边界参数存入数组而非创建box
- 调整
_controlBox函数,操作数组中的参数而非box对象 - 若需要显示OB,再根据数组参数动态创建box(可选)
修改后的完整代码:
//@version=5 //This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ indicator('Super OrderBlock / FVG / BoS Tools by makuchaku & eFe', overlay=true, max_boxes_count=500, max_lines_count=500 , max_labels_count=500) plotOB = input.bool(defval=true, title='Plot OB', group='Order Blocks') obBullColor = input.color(defval=color.new(color.green, 90), title='Bullish OB Color', inline='Set Custom Color', group='Order Blocks') obBearColor = input.color(defval=color.new(color.red, 90), title='Bearish OB Color', inline='Set Custom Color', group='Order Blocks') obBoxBorder = input.string(defval=line.style_solid, title='OB Box Border Style', options=[line.style_dashed, line.style_dotted, line.style_solid], group='Order Blocks', tooltip='To disable border, set Border Width below to 0') obBorderTransparency = input.int(defval=80, title='OB Border Box Transparency', minval=0, maxval=100, group='Order Blocks') obMaxBoxSet = input.int(defval=10, title='Maximum OB Box Displayed', minval=1, maxval=100, group='Order Blocks', tooltip='Minimum = 1, Maximum = 100') filterMitOB = input.bool(defval=true, title='Custom Color Mitigated OB', group='Order Blocks') mitOBColor = input.color(defval=color.new(color.yellow, 20), title='Mitigated OB Color', group='Order Blocks', inline='Set Custom Color Mit OB', tooltip='Set Transparency to 0 to make mitigated OB disappear') plotLabelOB = input.bool(defval=true, title='Plot OB Label', inline='OB label', group='Label Options') obLabelColor = input.color(defval=color.gray, title='Color', inline='OB label', group='Label Options') obLabelSize = input.string(defval=size.tiny, title="Size", options=[size.huge, size.large, size.small, size.tiny, size.auto, size.normal], inline='OB label', group='Label Options') //Box Types var int _ob = 1 var int _fvg = 2 var int _rjb = 3 var int _bos = 4 //Box Labels var string _obLabel = "OB" var string _fvgLabel = "FVG" var string _rjbLabel = "RJB" var string _bosLabel = "BoS" var string _plus = "+" var string _minus = "-" var string _empty = "" // 替换box数组为存储参数的元组数组:(left, top, right, bottom, isMitigated) var tuple[] _bearBoxesOB = array.new_tuple() var tuple[] _bullBoxesOB = array.new_tuple() //Functions isUp(index) => close[index] > open[index] isDown(index) => close[index] < open[index] isObUp(index) => isDown(index + 1) and isUp(index) and close[index] > high[index + 1] isObDown(index) => isUp(index + 1) and isDown(index) and close[index] < low[index + 1] isFvgUp(index) => (low[index] > high[index + 2]) isFvgDown(index) => (high[index] < low[index + 2]) // 修改_controlBox函数,操作元组参数而非box对象 _controlBox(_boxes, _high, _low, _type) => if array.size(_boxes) > 0 for i = array.size(_boxes) - 1 to 0 by 1 _params = array.get(_boxes, i) _boxLeft = tuple.get(_params, 0) _boxTop = tuple.get(_params, 1) _boxRight = tuple.get(_params, 2) _boxBottom = tuple.get(_params, 3) _isMitigated = tuple.get(_params, 4) if (_type == _ob) if not _isMitigated and (bar_index == _boxRight and not((_high > _boxBottom and _low < _boxBottom) or (_high > _boxTop and _low < _boxTop))) // 更新right参数 array.set(_boxes, i, tuple.new(_boxLeft, _boxTop, bar_index + 1, _boxBottom, false)) else // 标记为已触发 array.set(_boxes, i, tuple.new(_boxLeft, _boxTop, _boxRight, _boxBottom, true)) // 检测到Bullish OB时,存入参数而非创建box if isObUp(1) _left = bar_index - 2 _top = high[2] _right = bar_index _bottom = math.min(low[2], low[1]) // 存入元组:(left, top, right, bottom, isMitigated) if array.size(_bullBoxesOB) > obMaxBoxSet array.shift(_bullBoxesOB) array.push(_bullBoxesOB, tuple.new(_left, _top, _right, _bottom, false)) // 检测到Bearish OB时,存入参数而非创建box if isObDown(1) _left = bar_index - 2 _top = math.max(high[2], high[1]) _right = bar_index _bottom = low[2] if array.size(_bearBoxesOB) > obMaxBoxSet array.shift(_bearBoxesOB) array.push(_bearBoxesOB, tuple.new(_left, _top, _right, _bottom, false)) // 执行控制逻辑 _controlBox(_bearBoxesOB, high, low, _ob) _controlBox(_bullBoxesOB, high, low, _ob) // 可选:如果需要显示OB,根据数组参数动态创建box if plotOB // 绘制Bullish OB for i = 0 to array.size(_bullBoxesOB) - 1 _params = array.get(_bullBoxesOB, i) _left = tuple.get(_params, 0) _top = tuple.get(_params, 1) _right = tuple.get(_params, 2) _bottom = tuple.get(_params, 3) _isMitigated = tuple.get(_params, 4) _color = _isMitigated ? mitOBColor : obBullColor box.new(left=_left, top=_top, right=_right, bottom=_bottom, border_color=color.new(_color, obBorderTransparency), border_style=obBoxBorder, border_width=1, bgcolor=_color, text=plotLabelOB ? _obLabel + _plus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=obLabelSize, text_color=obLabelColor) // 绘制Bearish OB for i = 0 to array.size(_bearBoxesOB) - 1 _params = array.get(_bearBoxesOB, i) _left = tuple.get(_params, 0) _top = tuple.get(_params, 1) _right = tuple.get(_params, 2) _bottom = tuple.get(_params, 3) _isMitigated = tuple.get(_params, 4) _color = _isMitigated ? mitOBColor : obBearColor box.new(left=_left, top=_top, right=_right, bottom=_bottom, border_color=color.new(_color, obBorderTransparency), border_style=obBoxBorder, border_width=1, bgcolor=_color, text=plotLabelOB ? _obLabel + _minus : _empty, text_halign=text.align_right, text_valign=text.align_bottom, text_size=obLabelSize, text_color=obLabelColor)
关键改动说明:
- 用
tuple[]数组替代box[]数组,存储box的left/top/right/bottom以及是否触发的状态,完全避免创建实际box对象 _controlBox函数改为操作数组中的元组参数,更新边界和触发状态,保留原计算逻辑- 若需要显示OB,通过
plotOB开关控制,根据数组中的参数动态创建box,不显示时则不执行创建逻辑
内容的提问来源于stack exchange,提问作者Mahdi yousefi
相关产品推荐
相关产品推荐

