Non-Repainting Fair Value Gap Detector
Non-repainting three-bar Fair Value Gap detector for MetaTrader 5. It draws every three-bar imbalance as a rectangle, marks the 50% Consequent Encroachment level, and, in TRIM mode, settles the rectangle on the first closed bar that trades into the gap.
Detection is closed-bar only. A gap is derived from three closed bars – the bar before the impulse, the impulse bar itself, and the bar that leaves the imbalance behind – and the stored price levels are never rewritten afterwards. The gap lifecycle is an explicit state machine, so that in TRIM mode a gap that was filled long ago is drawn back to the bar that filled it instead of protruding into the future as though it were still open. Incremental scanning means only the bars that have just closed are tested, so a long history is not rebuilt on every tick.
Mitigation presentation is selectable: TRIM shortens the rectangle to the mitigation bar, DELETE removes it, DIM greys it, and NONE leaves it at full length. A built-in self-audit re-derives the gaps from the closed-bar series when the indicator is attached and compares the stored levels against the fresh ones, reporting the number of bars re-checked and any level that no longer matches. The audit runs on attach, and again whenever the chart history is rebuilt; set InpAuditDepth to 0 to switch it off.
Inputs, all documented in the source: InpLookbackBars (bars scanned on attach), InpMinGapPoints (0 = auto), InpMitigationMode, InpShowBullish, InpShowBearish, InpShowCE, InpExtendBars (forward extension of open gaps), InpAlertTouch, InpAuditDepth (0 = off) and eight colour inputs.
Pure native MQL5. No DLLs, no external dependencies, no include files.
Gap lifecycle: an open gap extends to the right, TRIM settles the rectangle at the first closed bar that entered the gap, and DIM keeps its length and greys it.