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

如何在Pine Script的Gaps指标中添加开关控制功能

实现Gaps指标的开关控制方案

1. 开关输入项的插入位置

将PlotGAP = input.bool(defval=false, title="Plot Gaps?")插入到所有输入参数定义的最顶部,也就是boxLimitInput变量定义之前、indicator()声明之后的位置。这样能让用户在指标设置面板的最上方快速找到开关选项。

2. 关键逻辑修改

仅添加开关输入项还不够,需要在所有与绘制、更新Gap盒子相关的代码块前加上PlotGAP的判断,确保关闭开关时停止所有Gap相关的渲染和计算;同时添加开关关闭时的清空逻辑,避免残留盒子。

修改后的完整代码

//@version=5
indicator("Gaps", overlay = true, max_boxes_count = 500)

// 新增:Gap开关控制
PlotGAP = input.bool(defval=false, title="Plot Gaps?")

var allBoxesArray = array.new<box>()
var boxIsActiveArray = array.new<bool>()
var boxIsBullArray = array.new<bool>()

boxLimitInput = input.int(15, "Max Number of Gaps", minval = 1, maxval = 500)
minimalDeviationTooltip = "Specifies the minimal size of detected gaps, as a percentage of the average high-low range for the last 14 bars."
minimalDeviationInput = nz(input.float(30.0, "Minimal Deviation (%)", tooltip = minimalDeviationTooltip, minval=1, maxval=100) / 100 * ta.sma(high-low, 14))
limitBoxLengthBoolInput = input.bool(false, "Limit Max Gap Trail Length (bars)", inline = "Length Limit")
limitBoxLengthIntInput = input.int(300, "", inline = "Length Limit", minval = 1)

groupName = "Border and fill colors"
colorUpBorderInput = input.color(color.green, "Up Gaps", inline = "Gap Up", group = groupName)
colorUpBackgroundInput = input.color(color.new(color.green, 85), "", inline = "Gap Up", group = groupName)
colorDownBorderInput = input.color(color.red, "Down Gaps", inline = "Gap Down", group = groupName)
colorDownBackgroundInput = input.color(color.new(color.red, 85), "", inline = "Gap Down", group = groupName)

// 仅当开关开启时执行Gap检测与绘制逻辑
if PlotGAP
    // Detect gaps.
    isGapDown = high < low[1] and low[1] - high >= minimalDeviationInput
    isGapUp = low > high[1] and low - high[1] >= minimalDeviationInput
    isGap = isGapDown or isGapUp
    isGapClosed = false

    // Detect covering of gaps.
    for [index, _box] in allBoxesArray
        if array.get(boxIsActiveArray, index)
            top = box.get_top(_box) 
            bot = box.get_bottom(_box)
            isBull = array.get(boxIsBullArray, index)
            box.set_right(_box, bar_index)
            if ((high > bot and isBull) or (low < top and not isBull)) or (limitBoxLengthBoolInput and bar_index - box.get_left(_box) >= limitBoxLengthIntInput)
                box.set_extend(_box, extend.none)
                array.set(boxIsActiveArray, index, false)
                isGapClosed := true

    // Add a box for each new gap, removing the oldest one if needed.
    if isGap
        box1 = box.new(
          bar_index[1],
          (isGapDown ? low[1] : low),
          bar_index,
          (isGapDown ? high : high[1]),
          border_color = isGapDown ? colorDownBorderInput : colorUpBorderInput,
          bgcolor = isGapDown ? colorDownBackgroundInput : colorUpBackgroundInput,
          extend = extend.right)

        array.push(allBoxesArray, box1)
        array.push(boxIsActiveArray, true)
        array.push(boxIsBullArray, isGapDown)
        if array.size(allBoxesArray) > boxLimitInput
            box.delete(array.shift(allBoxesArray))
            array.shift(boxIsActiveArray)
            array.shift(boxIsBullArray)
            
    if barstate.islastconfirmedhistory and array.size(allBoxesArray) == 0
        noGapText = "No gaps found on the current chart. \n The cause could be that some exchanges align the open of new bars on the close of the previous one, resulting in charts with no gaps."
        var infoTable = table.new(position.bottom_right, 1, 1)
        table.cell(infoTable, 0, 0, text = noGapText, text_color = chart.bg_color, bgcolor = chart.fg_color)
        
    alertcondition(isGap, "New Gap Appeared", "A new gap has appeared.")
    alertcondition(isGapClosed, "Gap Closed", "A gap was closed.")
else
    // 开关关闭时清空已绘制的Gap盒子
    if array.size(allBoxesArray) > 0
        for _box in allBoxesArray
            box.delete(_box)
        array.clear(allBoxesArray)
        array.clear(boxIsActiveArray)
        array.clear(boxIsBullArray)

逻辑说明

  • 开关默认关闭,用户手动开启后才会加载Gap检测逻辑
  • 关闭开关时,会自动清空已绘制的所有Gap盒子,避免界面残留
  • 所有Gap相关的计算、绘制、警报逻辑都被包裹在if PlotGAP分支内,确保关闭时完全停止相关操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:50:44