GapDetector
Size of each day's opening gap against the prior close, and whether price has since traded back to it.
At the first bar of each new trading date, GapDetector compares that bar's open with the previous bar's close. If the gap is at least min_gap_pct (default 0.001, or 0.1%), it records the gap as a fraction of the prior close, sets a direction of 1 for up or -1 for down, and keeps the gap open until price trades back to the prior close. The gap % and direction hold for the whole session, even after the fill, and reset on the next date.
How to read it
- Gap % and Gap direction sit in panes below the price; both read 0 on a day with no gap of at least the threshold.
- A non-zero Gap % holds all session, so the pane shows the day's gap, not whether it has filled.
- Gap filled is drawn on candles where no gap is open. A stretch with no markers is an open gap, and the first marker after it is the fill bar.
- With defaults the markers sit on almost every candle, so treat them as noise until a real gap appears.
What it outputs
- Gap %
- Opening gap as a fraction of the prior close, positive for up (0.01 is 1%). Holds for the session; 0 when the gap is under min_gap_pct.
- Gap filled
- True on bars with no open gap: a day with no gap of at least min_gap_pct, or a gap that has traded back to the prior close. False while a gap is still open.
- Gap direction
- 1 for a gap up, -1 for a gap down, 0 for no gap of at least min_gap_pct. Holds for the session, like Gap %.
When it fires
Fires on bars where no gap is open: the day had no gap of at least min_gap_pct, or an open gap has just traded back to the prior close. The first marker after a stretch with none is the fill bar. On the default SPY sample it was True on every bar.
Settings
- min_gap_pct
- Smallest gap that counts, as a fraction of the prior close (default 0.001, or 0.1%). Raising it leaves the gap outputs at 0 on more days; lowering it counts smaller gaps on more days. At 0, a flat open counts as a gap down.
Pitfalls
- No gap registered on the default SPY sample: Gap % and direction stayed 0, Gap filled stayed True. Suspect the times input: day change uses t.date() and falls back to None. Unverified.
- The docstring calls Gap % a percentage, but the value is a fraction: 0.01 means 1%.
- Gap filled is a state, not a fill event: it is True on every candle with no open gap, so it marks most of the chart and only the first marker after a stretch marks a fill.