Plotting option for "GAP" overlay

Viewed 19

I like to add this new "Gaps" indicator with my combined overlay as a support and resistance tool, and I need it as an optional tool so that I can turn it on and off as required. Therefore I need to add this following line of code somewhere in the main code -

PlotGAP = input.bool(defval=false, title="Plot Gaps?")

Below is the code of the "Gaps" overlay -

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

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)

// 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.")

Please help me to identify where inside the code I should add the function to turn it on and off as I need. Thanks for your time. Regards.

1 Answers

Could be like this

...
isGap = PlotGAP and (isGapDown or isGapUp)
...
Related