Market structure
Gap Marker
Shades the gap between one bar's close and the next bar's open for as long as it stays untraded, and stops shading the moment price trades back through it, so an open gap stays visible without being redrawn by hand.
indicator
//@version=6
indicator("Gap Marker", "Gaps", overlay = true)
minPercent = input.float(0.2, "Minimum gap in percent", minval = 0.0, step = 0.05, group = "Settings")
upCol = input.color(#26A69A, "Gap up", group = "Style")
downCol = input.color(#EF5350, "Gap down", group = "Style")
priorClose = close[1]
gapPercent = priorClose > 0 ? math.abs(open - priorClose) / priorClose * 100 : 0
bigEnough = gapPercent >= minPercent
gapUp = open > priorClose and low > priorClose and bigEnough
gapDown = open < priorClose and high < priorClose and bigEnough
var box openGap = na
var float gapEdge = na
var bool gapWasUp = true
newGap = gapUp or gapDown
filled = not na(gapEdge) and (gapWasUp ? low <= gapEdge : high >= gapEdge)
if newGap
box.delete(openGap)
openGap := box.new(bar_index - 1, math.max(open, priorClose), bar_index, math.min(open, priorClose), border_color = gapUp ? upCol : downCol, bgcolor = color.new(gapUp ? upCol : downCol, 82))
gapEdge := priorClose
gapWasUp := gapUp
else if filled
box.delete(openGap)
openGap := na
gapEdge := na
else if not na(openGap)
box.set_right(openGap, bar_index)
plotshape(gapUp, "Gap up", location = location.belowbar, style = shape.triangleup, color = upCol, size = size.tiny)
plotshape(gapDown, "Gap down", location = location.abovebar, style = shape.triangledown, color = downCol, size = size.tiny)
alertcondition(gapUp, "Gap up", "The bar opened above the previous close and has not traded back into the gap")
alertcondition(gapDown, "Gap down", "The bar opened below the previous close and has not traded back into the gap")
alertcondition(filled, "Gap filled", "Price has traded back through the open gap")
This runs in TradingView, not here
Pine Script only executes inside TradingView. Paste the source into the Pine Editor and add it to a chart to see it plotted.
What it will not do
- Only the most recent unfilled gap is shaded. An older gap still sitting untraded further back is not tracked.
- A gap is filled here the first time price touches the previous close. That is one definition of filled; a close beyond it is another.
- On instruments that trade continuously there is no session break, so almost nothing qualifies and the script will look inert.
- The minimum size is a percentage of price, so the same setting behaves differently across instruments. Set it per chart.
Written from this description
Mark a gap between one bar's close and the next bar's open when the new bar never trades back into it, shade the untraded area, keep extending it to the right while it stays open, and remove the shading once price fills it.
Educational only, not financial advice. The maths is simple arithmetic on the numbers you enter; it knows nothing about your broker, fees, slippage or the market. Something wrong with it? Say so in Site Feedback.