Fair Value Gap (FVG) in Pine Script
A three-candle pattern where a fast middle candle leaves the first and third candles' ranges apart, marking a stretch traded mostly one way that price often comes back to.
Definition
A fair value gap is a three-candle pattern in which the middle candle moves so far that the first and third candles' ranges do not overlap. In a bullish gap the third candle's low sits above the first candle's high. In a bearish gap the third candle's high sits below the first candle's low. The space between those two edges is the gap. It belongs to the imbalances family of smart money concepts, and unlike a price gap it forms inside continuous trading: the middle candle did trade through the space, but quickly and mostly in one direction.
The idea behind it is that one side of the market was in a hurry. When buyers lift offers fast enough to leave a gap, the prices inside it saw little two-way trade, so few orders rest there and few positions were opened there. Traders read the gap as unfinished business. Price often comes back to trade through it more fully, and the gap's near edge is watched as the place where the original buyers or sellers may step in again. Whether that return finds support or simply fills the gap is the question each gap leaves open.
The term comes from smart money concepts teaching, where the same pattern is also called an imbalance or an inefficiency. There is no single standard setting. Most scripts mark every gap, while some require a minimum size as a percentage of price or a fraction of the average true range. Many stop drawing a gap once price has closed through it, or after a set number of bars. Gaps on a higher timeframe are usually treated as more important than gaps on the chart a trader works on.
The implementation shades the latest bullish gap in green and the latest bearish gap in red, from the candle that completes it. A gap stays on the chart until a candle closes through its far edge or it reaches 50 bars of age, and a new gap on the same side replaces it. The script marks the first return into a gap that does not close through it, long for a bullish gap and short for a bearish one. On 5-minute charts of liquid stocks expect several gaps a session, most of them small; on daily charts they are fewer and wider.
Reading the chart
A fair value gap is read by its direction, its size and what price does on the first return.
- Find three candles in a row whose first and third ranges do not overlap. For a bullish gap the third candle's low is above the first candle's high; for a bearish gap the third candle's high is below the first candle's low.
- Mark the two edges. The near edge is the third candle's low in a bullish gap or its high in a bearish one. The far edge is the first candle's high or low.
- Measure the gap against the usual size of a bar. A gap a small fraction of the average true range wide is noise on most charts; a gap as wide as a typical bar shows real urgency.
- Check the middle candle. A large body that closes near its extreme on above-average volume marks a forceful move. A long wick that gives most of the move back makes the gap less meaningful.
- Note the context: whether the gap points with the trend of a higher timeframe, and whether it formed just after price swept an earlier high or low. Traders give gaps that point with the larger trend more weight.
- Watch the first return. A candle that trades into the gap and closes without going through its far edge is the retest the script marks. A close through the far edge ends the gap.
The maths
A fair value gap compares the third candle's range with the first candle's range, and the retest compares later candles with the gap's two edges.
The size test uses percent of price, so one setting works across stocks. Many traders prefer a fraction of the average true range, which adapts to volatility.
Only the latest gap on each side is tracked. A new gap replaces the open one, so on a trending chart with stacked gaps the older ones need drawing by hand.
A gap ends when a candle closes through its far edge or after the set number of bars. Some traders end it as soon as price trades to the far edge, which removes gaps sooner.
Many traders use the middle of the gap, its 50 percent level, as the entry instead of the near edge. An entry there fills less often and starts closer to the stop.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Minimum gap (%) | 0 | Raising it keeps only gaps that show real urgency and removes the many tiny gaps of 1-minute and 5-minute charts. At 0 every gap is marked. |
| Bars a gap stays open | 50 | A shorter life keeps only fresh gaps on the chart, where a return still belongs to the original move. A longer one keeps older levels that may still attract price. |
| Timeframe | 5 to 15 minutes intraday, daily for swings | Gaps on a higher timeframe are wider and fewer, and traders treat them as more important than the gaps on the chart they trade from. |
In practice
- As retest entries: a long when price first returns into a bullish gap and holds, with the stop beyond the first candle and the target a multiple of that risk. The script's marks are that first return.
- As support and resistance: an open bullish gap below price is a zone buyers may defend, and an open bearish gap above is one sellers may defend, so the shaded areas double as levels for stops.
- As a trend read: gaps forming one after another on the same side show a one-sided market. A run of bullish gaps that hold their retests describes a strong advance better than any single candle.
- As targets: traders often take profit at the near edge of an open gap in the trade's direction, since the gap marks prices that saw little two-way trade on the way through.
- As a filter: allowing longs only while the latest gap is bullish, or only in the direction of a higher-timeframe gap, keeps another entry rule on the side the market has been pushing.
- As alert triggers: the script raises a long alert on the first held return into a bullish gap and a short alert for a bearish one, so retests can be watched rather than searched for.
Where it goes wrong
- Trading every gap. On 5-minute charts gaps form many times a session and most are too small to matter, so set a minimum size or require context such as the higher-timeframe trend.
- Ignoring costs on small gaps. A gap a few tenths of a percent tall puts the stop and target so close that commission and slippage take much of the edge, so test with costs first.
- Treating every gap as a magnet. Many gaps are never filled, especially in a strong trend, so decide what to do with a gap that holds before entering on its return.
- Confusing it with a price gap. A fair value gap needs three candles that traded continuously, while an open beyond the previous bar is a different pattern with different behaviour.
Compared with
Gaps. A price gap is an open beyond the previous bar's range, so nothing traded inside it, and it forms mostly between sessions. A fair value gap forms inside continuous trading across three candles: the middle candle did trade through the space, just quickly. The two are measured and filled differently.
Break of Structure (BOS). A break of structure is a close through the last confirmed swing high or low, a statement about trend. A fair value gap is often the footprint of the fast candle that makes that break. Traders pair them: the break sets the direction and the gap it leaves gives the retest level.
Support & Resistance. Support and resistance are levels where price turned before, often tested several times. A fair value gap is a zone price has barely traded in, defined by one fast move rather than by past reversals. A gap that holds several returns starts to behave like support or resistance.
Breakout. A breakout is a close beyond a defined range, and a fair value gap is often what a strong breakout candle leaves behind. The breakout says the range gave way; the gap left by the breakout candle marks where a pullback would be tested first.
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.
Questions
What is a fair value gap in trading?
A three-candle pattern where the middle candle moves so fast that the first and third candles' ranges do not overlap. In a bullish gap the third candle's low is above the first candle's high, and the space between them is the gap. Traders treat it as a stretch of price that was traded mostly one way and watch for price to return to it.
Does trading fair value gaps work?
Not reliably on its own. A backtest of the textbook retest, entry at the gap's near edge, stop beyond the first candle and target at twice the risk, ran on 5-minute bars of ten US stocks and ETFs from July to October 2026. It won 37 percent of 1,885 trades, the average trade was slightly below zero before costs, and every ticker lost at 0.025 percent a side in costs.
What is the difference between a bullish and a bearish fair value gap?
Direction. A bullish gap forms in a fast advance: the third candle's low is above the first candle's high, and the gap sits below price as a possible support zone. A bearish gap forms in a fast decline: the third candle's high is below the first candle's low, and the gap sits above price as a possible resistance zone.
What is an inverse fair value gap?
A fair value gap that price closes through. Traders then read it the other way round: a bullish gap broken to the downside is treated as resistance when price comes back to it, and a broken bearish gap as support. The implementation removes a gap once a candle closes through its far edge, which is where an inverse gap begins.
Which timeframe is best for fair value gaps?
Intraday traders mark them on 1-minute to 15-minute charts and give more weight to gaps from the 1-hour or 4-hour chart, while swing traders use daily gaps. Lower timeframes produce far more gaps, most of them small, so a minimum size matters more there. On any timeframe, the gap's size against the average bar decides whether it is noise.
Should I enter at the edge or the middle of the gap?
Both are common. The near edge fills most often, because it is the first price a return reaches, but it puts the entry furthest from a stop beyond the first candle. The middle, the 50 percent level, fills less often and starts closer to the stop. Test both with costs on the market you trade before choosing one.