Open Gap Percentile Rank
A gap of one percent means different things on different symbols, and a gap up is often sized differently from a gap down. This script labels each session open with the gap and shows where that gap's size sits among the recent gaps in the same direction.

What it shows
- A label at the first bar of each session whose gap is at least the label size, 1 percent by default. It shows the gap in percent, the percentile of its size, and how many earlier gaps in that direction it was compared with.
- Gap up labels are green and sit under the bar. Gap down labels are red and sit over the bar.
- While fewer than 10 earlier gaps in that direction are stored, the label says "building" with the count so far.
- A small table at the bottom right with how many up gaps and down gaps are stored.
- Two alerts: "Gap up at open" and "Gap down at open".
How it works
On the first bar of each session, the gap is the open divided by the previous bar's close, minus one, in percent. The size is the absolute value of that gap. The script keeps two lists of sizes, one for up gaps and one for down gaps. Each list holds the latest gaps up to "Gaps kept per direction" (default 250). The percentile is the share of stored sizes in the same list that are smaller than the new one, with ties counted as half. The new gap is added to its list after it is ranked, once the bar has closed. Gaps of exactly zero are skipped. Every other gap joins its list, but only gaps at least as large as "Smallest gap to label (percent)" get a label and an alert. The default of 1 keeps a daily chart readable, and 0 labels every gap. "Label size" (default small) changes the text size.
How to use it
- Add it to a stock, index or futures chart. A daily chart ranks every day's gap. An intraday chart ranks the gap between sessions.
- Read the percentile on each label. A high number means the gap was large compared with recent gaps in its own direction.
- Raise "Smallest gap to label" to thin out the labels, or lower it to see smaller gaps. The ranking uses every gap either way. Change "Gaps kept per direction" to widen or narrow the comparison window.
Why it is different
Up gaps and down gaps are ranked in separate lists, so a down gap is compared only with earlier down gaps. The rank is also taken before the new gap joins the list, so a gap never counts itself.
Limits
- Lists start empty when the chart loads and fill only from the bars loaded, so early labels have a small sample.
- The chart keeps at most 500 labels, so on a long history only the latest ones stay visible. The lists still use every loaded bar.
- The first bar of a session is read from the symbol's session settings. Extended hours settings change which bar counts as the open and which close it is compared with.
- On markets that trade around the clock, the first bar of each day opens close to the previous close, so most gaps are small.
- The open and the previous close are known when the session bar opens, so the label text does not change while the bar forms. The new gap joins its list when the bar closes, so the stored counts update then.
- The percentile is a description of past gap sizes on the loaded chart. It says nothing about what comes next.
- It draws on the chart only. It places no orders and is not advice about any trade.