A visual gap lookup marks every candle that opens outside the previous candle range, so you can see at a glance where a stock or index gapped and whether the gap has been filled. We published this AmiBroker AFL code snippet in October 2015 for daily and weekly charts. Below is the original 2015 code explained, plus an extended visual gap AFL that adds gap reference levels and a filled or open status.
Key takeaways
- The original 2015 code uses AmiBroker’s GapUp() and GapDown(): a gap bar is one whose low is above the previous high, or whose high is below the previous low.
- Gap bars are coloured yellow, which gives a one shot visual check of every gap on the chart.
- The extended version uses a looser open-based test and draws dashed lines at the nearest gap reference levels: the prior high for a gap up and the prior low for a gap down.
- In the extended version, the title bar tells you whether the latest gap up and gap down have been filled.
- To scan a whole universe of stocks for gaps, use an AmiBroker exploration instead of a chart.
What the visual gap lookup does
Here is a simple code snippet to check for gaps in the charts, especially in the daily and weekly charts. It looks for gaps where the open is out of the previous candle range and colours those bars yellow. It gives one shot visual access to check whether a gap is filled in a particular stock or index, and shows the nearest gap reference levels. If you need to scan the whole universe of stocks to identify gaps, an AmiBroker exploration is preferred.
Visual Gap Lookup – Amibroker AFL Code (original 2015 snippet)
https://gist.github.com/f7fd81d581949e0eb44a
Opening gap vs true gap
AmiBroker has built-in GapUp() and GapDown() functions. GapUp() is true only when the previous high is below today’s low, so the whole bar sits above yesterday. GapDown() is the mirror case. The original 2015 snippet uses these two functions. The extended version further down uses a looser test based on the open, which also catches opening gaps that get filled within the same session.
| Test | Gap up condition | What it catches |
|---|---|---|
| True gap (original 2015 code, GapUp) | Low > previous High | Only gaps not filled at any point during the bar |
| Opening gap (extended version) | Open > previous High | Every gap open, including ones filled the same day |
Original visual gap AFL code (2015)
This is the code from the 2015 gist linked above.
_SECTION_BEGIN("Visual Gap Lookup");
SetChartOptions(0,chartShowArrows|chartShowDates);
gap = GapUp() OR GapDown();
Plot( C, "Close", IIf(gap,colorYellow,IIf(O-C < 0, colorBlue, IIf(O-C >0,colorRed,colorBlack))), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
_SECTION_END();
gap = GapUp() OR GapDown()marks every bar that trades entirely above the previous high or entirely below the previous low.- The nested
IIfpicks the bar colour: yellow for a gap bar, blue when the close is above the open, red when the close is below the open, and black when they are equal. GetPriceStyle()plots price in whatever style you have selected (candles or bars), andParamStylelets you change it from the Parameters window.- The
Titleline shows the symbol, interval, date, OHLC and the one bar percentage change.
Extended visual gap AFL code
The 2015 code colours gap bars but does not draw the gap reference levels or tell you whether a gap has been filled. This extended version adds both and switches to an open-based gap test. Paste it into a new formula in AmiBroker and apply it to a daily or weekly chart.
_SECTION_BEGIN("Visual Gap Lookup");
SetChartOptions(0, chartShowArrows | chartShowDates);
// 1. Opening gaps: open outside the previous candle range
gapUpOpen = Open > Ref(High, -1);
gapDnOpen = Open < Ref(Low, -1);
gapBar = gapUpOpen OR gapDnOpen;
// 2. Colour gap candles yellow, others green or red
barCol = IIf(gapBar, colorYellow, IIf(Close > Open, colorGreen, colorRed));
SetBarFillColor(barCol);
Plot(Close, "Price", barCol, styleCandle);
// 3. Nearest gap reference levels
upRef = ValueWhen(gapUpOpen, Ref(High, -1));
dnRef = ValueWhen(gapDnOpen, Ref(Low, -1));
Plot(upRef, "Last Gap Up Ref", colorAqua, styleDashed | styleNoRescale);
Plot(dnRef, "Last Gap Down Ref", colorOrange, styleDashed | styleNoRescale);
// 4. Has the latest gap been filled?
upFilled = LowestSince(gapUpOpen, Low) <= upRef;
dnFilled = HighestSince(gapDnOpen, High) >= dnRef;
Title = Name() + " - Visual Gap Lookup" +
"\nGap up ref: " + NumToStr(upRef, 1.2) + WriteIf(upFilled, " (filled)", " (open)") +
"\nGap down ref: " + NumToStr(dnRef, 1.2) + WriteIf(dnFilled, " (filled)", " (open)");
_SECTION_END();
Extended code: line by line explanation
_SECTION_BEGINandSetChartOptionsname the section and show dates and arrows on the chart.gapUpOpenis true when today’s open is above the previous bar high.Ref(High, -1)reads the previous bar value.gapDnOpenis true when the open is below the previous bar low.gapBaris true for either case.barColusesIIfto pick yellow for gap bars, and green or red for normal up or down candles.SetBarFillColorandPlot(..., styleCandle)draw the candles with those colours.ValueWhenstores the previous high on the last gap up bar (upRef) and the previous low on the last gap down bar (dnRef). These are the nearest gap reference levels.- The two dashed
Plotcalls draw those levels.styleNoRescalestops an old level from squashing the price scale. LowestSincechecks if price has traded back down to the gap up reference since the gap.HighestSincedoes the same for the gap down reference.Titleprints both reference levels with “filled” or “open” so you can read the status without measuring.
How to use the visual gap chart
- Open the Formula Editor, paste either version of the code and save it, for example as Visual Gap Lookup.afl.
- Apply it to a daily chart of a stock such as TCS, or to Nifty or Bank Nifty.
- Switch to the weekly timeframe to see the larger gaps that swing traders watch.
- With the extended version, read the title: an “open” gap reference is a level price has not yet revisited.
Scan for gaps with an exploration
For a watchlist or the full NSE universe, run this short exploration in the Analysis window.
// Exploration: list today's opening gaps across a watchlist
gapUpOpen = Open > Ref(High, -1);
gapDnOpen = Open < Ref(Low, -1);
Filter = gapUpOpen OR gapDnOpen;
AddColumn(Close, "Close", 1.2);
AddTextColumn(WriteIf(gapUpOpen, "Gap Up", "Gap Down"), "Type");
AddColumn(100 * (Open - Ref(Close, -1)) / Ref(Close, -1), "Gap %", 1.2);
Visual gap levels for traders
Unfilled gaps often act as reference levels on later pullbacks, but they are not guaranteed to be filled. Treat an open gap level as a zone to watch, together with prior day levels from our prior daily OHLC AFL, consolidation zones from the range identifier AFL, and recent extremes from the N bar high-low dots AFL.
Frequently Asked Questions
What is a visual gap lookup in AmiBroker?
It is a chart formula that colours gap bars yellow so gaps are easy to spot. The original 2015 version uses GapUp() and GapDown(); the extended version also plots the nearest gap reference levels and shows whether each has been filled.
How do I know if a gap is filled?
A gap up is filled when price trades back down to the previous high from before the gap. A gap down is filled when price trades back up to the previous low. The extended AFL checks this with LowestSince and HighestSince.
Can I use the code on intraday charts?
Yes, the logic works on any timeframe. On intraday charts most gaps appear on the first bar of each session, which is compared with the last bar of the previous day. Bars within a session rarely gap, so daily and weekly charts give more useful results.
How do I scan all stocks for gaps?
Use the exploration code above in the Analysis window and apply it to a watchlist or all symbols. It lists each gap with its type and gap percentage.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.



Rajendran,
Why do you use Spsquantityofshares=100 instead of spspercentofequity=100?
Is it too risky to use 100% of equity to trade?
Yes it is risky to bet your 100% of your money especially if you are trading with leverage!
dear sir , thanks for giving afl ….
please can you add options – scan or explore …..