Filters out tiny gaps where (high - low) / atr[i] < minGapAtr. Computes Consequent Encroachment ce = (low + high) / 2.
Lifecycle & inversion: Walks forward k = i + 2 .. n - 1:
Wick mitigation: Tracks maximum penetration fillPct and whether price touched ce (ceTested). If a wick fills 100% without closing through, the gap is fully mitigated and dropped; partial fills transition status to 'partial'.
Close-through → IFVG: When a close breaks the far edge (c[k] < low for bull, c[k] > high for bear): if includeInverted = 0, the gap is dropped; if includeInverted = 1, it flips to status = 'inverted', reverses effectiveDir = -dir, records invertedAt = k, and remains active until a subsequent close breaks back through its new far edge.
Balances selection with up to ceil(max / 2) gaps below close and ceil(max / 2) above close, backfilled to max, sorted descending by high.
Drawings & Facts
Drawings: Up to maxzone boxes from createdAt to the latest closed bar with a dashed CE midpoint line (mid). Fresh gaps render normal weight; partial and inverted gaps render 'faint'. Color follows effectiveDir ('up' / 'down'). Labels show IFVG ↑|↓ or FVG ↑|↓ · fresh|N%.
Facts (FvgFacts): lastClose, activeBull, activeBear, invertedCount, and gaps[].
Activation
Studies menu: Fair value gaps: imbalances and IFVGs (FVG)