Concept 17 / 52·Structure·Price gaps
‹ Gann BoxGolden Cross & Death Cross ›

Gaps in Pine Script

price gap · gap up · gap down · opening gap · gap fill · gap and go

An empty space on the chart where a bar opens beyond the previous bar's range, showing that price adjusted between sessions without trading, and the level it leaves behind until it is filled.

Reads best on: Stocks and commodity futures on daily charts, where sessions close and news arrives overnight. Index futures at the daily halt; rarely useful on crypto or spot forex.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

A gap is a bar that opens above the previous bar's high or below its low, leaving a stretch of prices at which nothing traded. It belongs to the price-gaps family of structure, and it is mostly a feature of markets that close: news, earnings and overnight order flow move the opening price away from yesterday's range. A gap up opens above the previous high, a gap down below the previous low, and the untraded space between them is the gap itself. On 24-hour markets true gaps are rare and appear mainly at weekend opens.

Gaps matter because they show an imbalance that could not be resolved by trading. Everyone who wanted to deal at the prices inside the gap never got the chance, and everyone holding a position through it has a sudden profit or loss. Two questions follow. Does the move continue, with the gap acting as support or resistance, or does price return to fill it? And what volume came with the gap, because a gap on heavy volume out of a base is sponsored, while a gap on ordinary volume within a range is usually noise.

The classic taxonomy has four types. A common gap occurs inside a range on no particular news and fills quickly. A breakaway gap opens out of a base or a consolidation on heavy volume and starts a new trend. A runaway or measuring gap appears in the middle of a trend, often near its halfway point, on continued strong volume. An exhaustion gap comes after a long move, on the heaviest volume of all, and fills within days as the last buyers are met by sellers. The type is only certain in hindsight, but volume and location narrow it early.

The implementation marks a gap when the open is beyond the previous bar's high or low by at least a minimum percentage, 0.5 percent by default, so tiny gaps on volatile names are ignored. It marks a gap up as long and a gap down as short, the gap-and-go reading, and keeps the level the gap opened from on the chart until price trades back through it, which is the fill. Expect most small gaps to fill within a session or two and most breakaway gaps to hold for weeks, with the first return to the gap edge the place to judge which one you have.

02

Reading the chart

A gap is read by its size, its volume and where it sits in the larger move.

  1. Confirm a true gap. The open must be beyond the previous bar's high or low, not just beyond its close; a bar that opens inside yesterday's range has no empty space behind it.
  2. Measure it. As a percentage of price, and against the average daily range: a gap of half an average range is an event, a gap of a few ticks is not.
  3. Read the volume of the first bar or the first session. Heavy volume with follow-through in the gap's direction is sponsorship; heavy volume that reverses is absorption; light volume says the gap is open to a fade.
  4. Locate it. Out of a base or a range it is a breakaway candidate; in the middle of a trend it is a runaway; after a long run with a climactic bar it is likely exhaustion; inside a range on no news it is common.
  5. Mark the gap edges. The previous bar's high for a gap up, or its low for a gap down, is the near edge and the first support or resistance; the previous close is the full fill target.
  6. Watch the first return. A test of the near edge that holds confirms the gap as structure; a close back inside the gap says the fill is likely and the previous close is the next stop.
03

The maths

A gap is defined by the open against the previous bar's extremes, scaled by a minimum size, and the fill is the return of price to the level the gap opened from.

GapUpg={1Og>Hg−1(1+m100)0otherwise\mathrm{GapUp}_g = \begin{cases} 1 & O_g > H_{g-1} \left(1 + \frac{m}{100}\right) \\ 0 & \text{otherwise} \end{cases}
GapDowng={1Og<Lg−1(1−m100)0otherwise\mathrm{GapDown}_g = \begin{cases} 1 & O_g < L_{g-1} \left(1 - \frac{m}{100}\right) \\ 0 & \text{otherwise} \end{cases}
GapSizeg={Og−Hg−1Hg−1×100gap upLg−1−OgLg−1×100gap down\mathrm{GapSize}_g = \begin{cases} \frac{O_g - H_{g-1}}{H_{g-1}} \times 100 & \text{gap up} \\ \frac{L_{g-1} - O_g}{L_{g-1}} \times 100 & \text{gap down} \end{cases}
Filledt={1t>g∧Lt≤Hg−1(gap up)1t>g∧Ht≥Lg−1(gap down)0otherwise\mathrm{Filled}_t = \begin{cases} 1 & t > g \land L_t \le H_{g-1} \quad \text{(gap up)} \\ 1 & t > g \land H_t \ge L_{g-1} \quad \text{(gap down)} \\ 0 & \text{otherwise} \end{cases}
ggthe bar that gaps
OgO_gthe open of the gap bar
Hg−1,Lg−1H_{g-1}, L_{g-1}high and low of the bar before the gap, the near edge of the gap
mmthe minimum gap size in percent, 0.5 by default
GapSizeg\mathrm{GapSize}_gthe size of the gap as a percentage of the previous high (or low, for a gap down)
tta later bar
Lt,HtL_t, H_tlow and high of the later bar
GapUpg,GapDowng\mathrm{GapUp}_g, \mathrm{GapDown}_gtrue when the gap bar opens beyond the previous high or low by at least the minimum size
Filledt\mathrm{Filled}_ttrue on the later bar whose low (gap up) or high (gap down) trades back through the gap level, which counts as the fill

The implementation compares the open with the previous bar's high and low, so it detects true gaps beyond the range and not the more common close-to-open offsets that open inside yesterday's bar.

The gap level kept on the chart is the previous bar's high for a gap up or its low for a gap down, and it is removed when a later bar's low or high trades through it, which counts as the fill.

Only one open gap is tracked at a time; a new gap replaces the old level even if the old one was never filled, so on a chart with several open gaps draw the older ones by hand.

Full fill in most traders' usage means a return to the previous close, deeper than the previous extreme; a partial fill means the near edge was tested and held.

04

Inputs that matter

InputTypicalWhat changing it does
Minimum gap (%)0.5A higher minimum keeps only the gaps that matter on volatile names; a lower one marks the small overnight offsets of quiet stocks and indices.
Referenceprevious bar's high or lowMeasuring against the previous close instead marks many more gaps, including ones inside yesterday's range, which suits index and futures traders who read every overnight offset.
TimeframedailyOn intraday charts the only gaps are at the session open; on weekly bars gaps are rare and each one is significant.
05

In practice

  • As gap-and-go entries: a gap up out of a base on heavy volume that holds its near edge on the first pullback is a long entry with that edge as the stop; a gap down out of a top is the mirror short the script marks.
  • As gap-fill fades: a small gap inside a range on light volume is faded back towards the previous close, with the stop beyond the gap bar's extreme.
  • As structure: an unfilled gap edge acts as support or resistance on later returns, and the script keeps the level on the chart until the fill.
  • As a trend read: a runaway gap on strong volume mid-trend suggests the move is about half done; an exhaustion gap after a long run on the heaviest volume warns that it is ending.
  • As a scan: mark gaps above a minimum size across a watchlist at the open, then read the volume of the first bars to sort sponsored gaps from noise.
  • As alert triggers: the script raises a long alert on a gap up and a short alert on a gap down, so the first minutes after the open can be watched instead of searched.
06

Where it goes wrong

  • Fading every gap. Most small gaps fill, but breakaway gaps on heavy volume do not, and fading them is shorting a new trend; check volume and location first.
  • Ignoring the news. An earnings or guidance gap has a reason that will not be undone by a fill; treat it as structure rather than as a mispricing.
  • Confusing close-to-open offsets with gaps. An open inside yesterday's range is not a gap and leaves no empty space; the script only marks opens beyond the previous extreme.
  • Applying daily gap logic to 24-hour markets. Crypto and spot forex rarely gap except over weekends, and the gap statistics from stocks do not transfer.
07

Compared with

Breakout. A breakout is a close beyond a range that was reached by trading through the level; a gap jumps over the level without trading. A gap out of a base is a breakout that happened overnight, and it is read with the same volume and follow-through rules.

Opening Range Breakout. The opening range is the high and low of the first minutes after the open; a gap is where that open sits relative to yesterday. On a gap day the opening range often forms above or below yesterday's range, and the two tools together describe the session.

Previous Day High & Low. The previous day's high and low are the levels a gap opens beyond, and the previous close is the fill target. The gap script watches the same prices but only reacts when the open jumps past them.

Relative Volume. Relative volume measures how unusual a bar's size is; a gap measures the price offset between two bars. Volume decides what kind of gap you have, so the two are read together at every open, and a gap without volume is treated as noise.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Open Gaps on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • Do all gaps get filled?

    No. Most common gaps fill within days because nothing changed, and exhaustion gaps fill quickly as the move ends. Breakaway gaps on heavy volume out of a base can stay open for months, because the prices inside the gap were never fair value again. The saying that gaps always fill is true only if you wait long enough to be wrong.

  • What is a gap fill?

    Price trading back through the level the gap opened from. The near edge is the previous bar's extreme, and the implementation counts the fill when a later bar's low or high trades through it. Many traders use the previous close as the full fill target, which is deeper, and treat a test of the near edge that holds as a partial fill.

  • How do I tell a breakaway gap from a common gap?

    Location and volume. A breakaway gap opens out of a base or a consolidation, on volume well above average, with a reason behind it, and holds its near edge on the first pullback. A common gap opens inside a range on ordinary volume and no news, and fills quickly. The volume of the first session is the earliest tell.

  • Is a gap up a long entry?

    Only when it is a breakaway or runaway gap: out of a base or mid-trend, on heavy volume, holding its near edge. A long on a gap up into resistance after a long run is a long into an exhaustion gap. The implementation marks every gap up as long because that is the gap-and-go premise, but the volume and the location decide whether the mark carries weight.

  • Do gaps exist on crypto and forex?

    Rarely. Both trade almost continuously, so the open of one bar is the close of the last, and true gaps appear only at weekend opens on forex or during exchange outages. Gap analysis is a feature of markets that close: stocks, index futures during the daily halt, and commodities.

  • What does the minimum gap setting do?

    It ignores gaps smaller than the chosen percentage of price, 0.5 percent by default. Volatile names open a few tenths of a percent beyond yesterday's range most days, and without the filter every one would be marked. Raise it on volatile stocks, lower it on quiet indices where a 0.3 percent gap is already unusual.