Indicator · Pine Script v5 · On the chart

Gap Finder [KL]

by kevinhhl· code credited to DojiEmoji

This indicator detects price gaps between consecutive bars and plots them directly on the price chart.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code94
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago6 Oct 2023

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

94 lines of Pine Script v5 from Scripts/Gap Finder.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Gap Finder.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DojiEmoji

// Description:
// From perspective of time-based charts, A gap is a “jump” in a security’s price between the Open and the Prior Close.
// This script will draw lines whenever it locates discrepancies between Open and Close of two consecutive data points (candles).
// The lines will be removed when subsequent candles introduced into the data contain low/high values crossing the lines' price in
// direction opposite to the direction of the gaps.

//@version=5
indicator(title="Gap Finder [KL]", overlay=true)
var string TYPE_PERCENT = "Percentage"
var string TYPE_ABS     = "Absolute"
var string FILLED_DEL   = "Delete"
var string FILLED_KEEP  = "Show as dotted lines"
var int extend_len      = 100

// Settings
var string thres_type   = input.string(TYPE_PERCENT, options=[TYPE_ABS, TYPE_PERCENT], title="Threshold type")
var float thres_pcnt    = input.float(0, title="Threshold (%)", minval=0, maxval=100)/100
var float thres_point   = input.float(0, title="Threshold (points)", minval=0)
var color col_up        = input.color(color.new(#00ff0a, 0), title="Gap up", inline="Color")
var color col_down      = input.color(color.new(#ff160C, 0), title="Gap down", inline="Color")
var bool _extend_lns    = input.bool(false, title="Extend lines", tooltip="Suggested chart settings -> Scale price chart only")
var string extend_lns   = _extend_lns ? extend.right : extend.none
var bool wick_fill      = input.bool(true, title="Gap touched by wick considered as filled")
var string _choice      = input.string(FILLED_DEL, options=[FILLED_DEL, FILLED_KEEP], title="Lines for filled gaps")
var bool del_lns        = _choice == FILLED_DEL

// For comparison: p1,p2,p3,p4
float p1_upGap = math.max(open, close)[1]
float p2_upGap = low
float p3_dnGap = math.min(open, close)[1]
float p4_dnGap = high

// Finding gaps
bool gap_up = false
if thres_type == TYPE_PERCENT
    gap_up := (p2_upGap > p1_upGap and math.abs(p2_upGap/p1_upGap - 1) > thres_pcnt)
else if thres_type == TYPE_ABS
    gap_up := (p2_upGap > p1_upGap and math.abs(p2_upGap - p1_upGap) > thres_point)

bool gap_down = false
if thres_type == TYPE_PERCENT
    gap_down := (p4_dnGap < p3_dnGap and math.abs(p4_dnGap/p3_dnGap - 1) > thres_pcnt)
else if thres_type == TYPE_ABS
    gap_down := (p4_dnGap < p3_dnGap and math.abs(p4_dnGap - p3_dnGap) > thres_point)

// Inserting lines
var line[] arr_upgap = array.new_line() 
var line[] arr_dngap = array.new_line()

if gap_up
    float _p = p1_upGap
    line ln = line.new(bar_index, _p, bar_index+extend_len, _p, xloc.bar_index, extend_lns, col_up)
    array.push(arr_upgap, ln)
else if gap_down
    float _p = p3_dnGap
    line ln = line.new(bar_index, _p, bar_index+extend_len, _p, xloc.bar_index, extend_lns, col_down)
    array.push(arr_dngap, ln)

// Search for filled gaps, remove them from array
if array.size(arr_upgap) > 0
    for i=0 to array.size(arr_upgap) - 1
        line ln = array.pop(arr_upgap)
        float _y = line.get_y1(ln)
        float _src_fill = wick_fill ? low : close
        if _src_fill < _y and high[1] > _y
            if del_lns
                line.delete(ln)
            else 
                line.set_style(ln, line.style_dotted)
                line.set_x2(ln, bar_index)
                line.set_extend(ln, extend.none)
        else
            array.unshift(arr_upgap, ln) 

if array.size(arr_dngap) > 0
    for i=0 to array.size(arr_dngap) - 1
        ln = array.pop(arr_dngap)
        _y = line.get_y1(ln)
        _src_fill = wick_fill ? high : close
        if _src_fill > _y and low[1] < _y
            if del_lns
                line.delete(ln)
            else 
                line.set_style(ln, line.style_dotted)
                line.set_x2(ln, bar_index)
                line.set_extend(ln, extend.none)
        else
            array.unshift(arr_dngap, ln)

// Alerts
alertcondition(gap_up or gap_down, title = "Gap up/down", message = "{{ticker}} just gapped.")

Overview

Gaps are identified when current price ranges separate from the prior bar candle body beyond a user-defined absolute point or percentage threshold. It projects horizontal lines across unfilled gaps, with separate colors for gap up and gap down events. When subsequent price action fills a gap using wicks or closes, the script either deletes the line or converts it into a dotted segment.

Details

Published by

kevinhhl on GitHub

Code credit

DojiEmoji, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance

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.

Settings

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

SettingTypeDefaultVariable
Threshold typestringTYPE_PERCENTthres_type
Threshold (%)float0thres_pcnt
Threshold (points)float0thres_point
Gap upcolorcolor.new(#00ff0a, 0)col_up
Gap downcolorcolor.new(#ff160C, 0)col_down
Extend linesboolfalse_extend_lns
Gap touched by wick considered as filledbooltruewick_fill
Lines for filled gapsstringFILLED_DEL_choice
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

948 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 10 days ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 2 years ago · None stated GitHub repository of BZ-tracker

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - 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