Indicator · Pine Script v6 · On the chart

[innoobwetrust] Key Gaps

by InNoobWeTrust

This indicator detects large price gaps by comparing each candle’s high-to-low range with the previous ATR and requiring a minimum body percentage.

View on GitHub

GitHub stars4on the repository
Pine versionv6current
Lines of code171
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last updatetoday28 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

171 lines of Pine Script v6 from .innoobwetrust/life_hacks/tradingview/key_gaps.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

key_gaps.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©  InNoobWeTrust

//@version=6

// @description Detect and plot key value-gaps as price zones and heatmap
indicator("[innoobwetrust][indi] key gaps", overlay = true)
import TradingView/ta/9
import forbidden_gun/commonlib/5
import forbidden_gun/plot_lib/3


//===============================================================================
// Types
//===============================================================================

type KeyGap
    plot_lib.TfBarZone zone
    float spread
    float relative_spread

//===============================================================================
// Input
//===============================================================================

key_gap_atr_len = input.int(16, 'base ATR length')
key_gap_threshold = input.float(2.618, 'ATR mult')
key_gap_cap = input.float(4.236, 'ATR upper cap')
key_gap_body_percent = input.float(0.618, 'Min body percent', minval=0, maxval=1)
key_gap_buffer = input.int(512, 'Buffer', minval = 0)
key_gap_visible_range_tolerance = input.float(0.1, "Visible range tolerance (for drawings)")
key_gap_heatmap_grp = '► Gap heatmap'
key_gap_heatmap_offset = input.int(4, 'Heatmap offset', group = key_gap_heatmap_grp)
key_gap_heatmap_width = input.int(6, 'Heatmap width (0 to disable)', group = key_gap_heatmap_grp)
key_gap_zone_count = input.int(5, 'Draw most recent gaps (0 to disable)', minval = 0)
key_gap_gradient_grp = '► Gradient'
key_gap_gradient_low = input.color(color.new(color.aqua, 90), key_gap_gradient_grp, group = key_gap_gradient_grp)
key_gap_gradient_high = input.color(color.new(color.teal, 85), '', group = key_gap_gradient_grp)
key_gap_inverse_gradient = input.bool(true, "Inverse gradient based on candle color", group = key_gap_gradient_grp)

//===============================================================================
// Global vars
//===============================================================================

var array<KeyGap> key_gaps = array.new<KeyGap>(key_gap_buffer, na)
var array<float> key_gap_highs = array.new<float>(key_gap_buffer, na)
var array<float> key_gap_lows = array.new<float>(key_gap_buffer, na)
var array<plot_lib.TfBarZonePlot> key_gaps_plt = array.new<plot_lib.TfBarZonePlot>(key_gap_zone_count, na)
var array<box> key_wicks_plt = array.new<box>(key_gap_zone_count, na)
var array<box> key_level_boxes = array.new<box>()

//===============================================================================
// Calculation
//===============================================================================

[visible_low, visible_high] = commonlib.visible_price_range()
visible_range_delta = key_gap_visible_range_tolerance * (visible_high - visible_low)
visible_tolerance_low = visible_low - visible_range_delta
visible_tolerance_high = visible_high + visible_range_delta
key_gap_atr = ta.atr(key_gap_atr_len)
key_gap_candle_spread = high - low
key_gap_candle_spread_mult = key_gap_candle_spread / key_gap_atr[1]
candle_is_key = key_gap_candle_spread_mult >= key_gap_threshold and math.abs(commonlib.bar_body_percent()) >= key_gap_body_percent

if candle_is_key and key_gap_buffer > 0
    // Remove old gaps
    key_gaps.shift()
    key_gap_highs.shift()
    key_gap_lows.shift()

    tf_secs = timeframe.in_seconds()
    bar = plot_lib.TfBar.new(open, high, low, close, time, time_close)
    bar_zone = plot_lib.TfBarZone.new(tf_secs, bar)
    key_gaps.push(KeyGap.new(bar_zone, key_gap_candle_spread, key_gap_candle_spread_mult))
    key_gap_highs.push(high)
    key_gap_lows.push(high)

//===============================================================================
// Plot
//===============================================================================

if barstate.islast
    if key_gap_zone_count > 0
        // Remove and clear old plots
        while key_gaps_plt.size() > 0
            oldest_zone_plt = key_gaps_plt.shift()
            if not na(oldest_zone_plt)
                plot_lib.clear_tf_zone(oldest_zone_plt)
            oldest_wick_plt = key_wicks_plt.shift()
            if not na(oldest_wick_plt)
                oldest_wick_plt.delete()

    for b in key_level_boxes
        b.delete()
    key_level_boxes.clear()

    // Calculated indices of gaps with valid highs
    inclusive_high_indices = array.new<int>()
    sorted_indices = key_gap_highs.sort_indices(order.ascending)
    sorted_vals = key_gap_highs.copy()
    sorted_vals.sort(order.ascending)
    min_idx_idx = sorted_vals.binary_search_leftmost(visible_tolerance_low)
    for idx_idx = min_idx_idx to sorted_indices.size() -1
        idx = sorted_indices.get(idx_idx)
        inclusive_high_indices.push(idx)
    // Calculated indices of gaps with valid lows
    inclusive_low_indices = array.new<int>()
    sorted_indices := key_gap_lows.sort_indices(order.ascending)
    sorted_vals := key_gap_lows.copy()
    sorted_vals.sort(order.ascending)
    max_idx_idx = sorted_vals.binary_search_rightmost(visible_tolerance_high) - 1
    for idx_idx = 0 to max_idx_idx
        idx = sorted_indices.get(idx_idx)
        inclusive_low_indices.push(idx)
    
    inclusive_indices = array.new<int>()
    if inclusive_low_indices.size() < inclusive_high_indices.size()
        for idx in inclusive_low_indices
            // Assume indexing is faster than looping due to compiler optimizations
            if inclusive_high_indices.indexof(idx) != -1
                inclusive_indices.push(idx)
    else
        for idx in inclusive_high_indices
            if inclusive_low_indices.indexof(idx) != -1
                inclusive_indices.push(idx)
    // Sort indices from latest to oldest
    inclusive_indices.sort(order.descending)

    for idx in inclusive_indices
        gap = key_gaps.get(idx)
        // Skip empty slots in buffer
        if na(gap)
            continue

        // Plot zones
        if key_gaps_plt.size() <= key_gap_zone_count
            clr = color.from_gradient(gap.relative_spread, key_gap_threshold, key_gap_cap, key_gap_gradient_low, key_gap_gradient_high)
            if key_gap_inverse_gradient and gap.zone.bar.c < gap.zone.bar.o
                // Invert the color
                clr := color.rgb(255 - color.r(clr), 255 - color.g(clr), 255 - color.b(clr), color.t(clr))
            plt = plot_lib.plot_tf_zone(gap.zone, clr)
            key_gaps_plt.push(plt)
            wick_box = box.new(left = gap.zone.bar.t_o,
                                 top = gap.zone.bar.c > gap.zone.bar.o ? gap.zone.bar.o : gap.zone.bar.h,
                                 right = time_close,
                                 bottom = gap.zone.bar.c > gap.zone.bar.o ? gap.zone.bar.l : gap.zone.bar.o,
                                 border_color = clr,
                                 border_width = 2,
                                 border_style = line.style_solid,
                                 extend = extend.none,
                                 xloc = xloc.bar_time,
                                 bgcolor = clr,
                                 force_overlay = true)
            key_wicks_plt.push(wick_box)

        // Plot heatmap
        clr = color.from_gradient(gap.relative_spread, key_gap_threshold, key_gap_cap, key_gap_gradient_low, key_gap_gradient_high)
        if key_gap_inverse_gradient and gap.zone.bar.c < gap.zone.bar.o
            // Invert the color
            clr := color.rgb(255 - color.r(clr), 255 - color.g(clr), 255 - color.b(clr), color.t(clr))
        b = box.new(left = bar_index + key_gap_heatmap_offset,
                     top = gap.zone.bar.h,
                     right = bar_index + key_gap_heatmap_offset + key_gap_heatmap_width,
                     bottom = gap.zone.bar.l,
                     border_color = color(na),
                     border_width = 2,
                     border_style = line.style_solid,
                     xloc = xloc.bar_index,
                     bgcolor = clr,
                     force_overlay = true)
        key_level_boxes.push(b)

Overview

Qualifying candles are stored in a rolling buffer and drawn as price zones on the chart, limited to the most recent count. A side heatmap uses an aqua-to-teal gradient that can invert with candle color. Settings change ATR length, gap multiplier and cap, body size, buffer, zone count, heatmap size, and drawing tolerance. It overlays the price chart and does not place trades.

Details

Published by

InNoobWeTrust on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Support & resistance, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

dotfiles

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
base ATR lengthint16key_gap_atr_len
ATR multfloat2.618key_gap_threshold
ATR upper capfloat4.236key_gap_cap
Min body percentfloat0.618key_gap_body_percent
Bufferint512key_gap_buffer
Visible range tolerance (for drawings)float0.1key_gap_visible_range_tolerance
Heatmap offsetint4key_gap_heatmap_offset
Heatmap width (0 to disable)int6key_gap_heatmap_width
Draw most recent gaps (0 to disable)int5key_gap_zone_count
key_gap_gradient_lowcolorcolor.new(color.aqua, 90)key_gap_gradient_low
key_gap_gradient_highcolorcolor.new(color.teal, 85)key_gap_gradient_high
Inverse gradient based on candle colorbooltruekey_gap_inverse_gradient
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

ICT Liquidity Void Fill

by ArunKBhaskar
575 stars

This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Liquidity Void Fill

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

Snag - VWAP

by dearvn
257 stars

This indicator plots a custom Anchored Volume-Weighted Average Price (VWAP) directly on the price chart along with optional standard deviation bands. Users can anchor the VWAP to standard time periods like sessions, weeks, or months, or to corporate events suc

Indicator v5 Moving averagesVolatility & bands Repaints
Updated 6 weeks ago · None stated GitHub repository of Snag - VWAP

Candlestick Patterns

by harryguiacorn
219 stars

This indicator plots optional candlestick pattern signals on the price chart. It detects engulfing bars, inside bars, HOLP and LOHP, kicker patterns, body-mass extremes, higher highs and lower lows, ATR warnings, and four-bar fractals. Optional pivot marks use

Indicator v5 Candlestick patternsVolatility & bands
Updated 1 year ago · None stated GitHub repository of Candlestick Patterns

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers