Market structure
Inside and Outside Bars
Labels each bar whose range sits inside the previous bar's range, and each bar whose range swallows it, which is the smallest readable statement about whether the last two bars agreed or argued.
indicator
//@version=6
indicator("Inside and Outside Bars", "In Out", overlay = true)
markInside = input.bool(true, "Mark inside bars", group = "Settings")
markOutside = input.bool(true, "Mark outside bars", group = "Settings")
shade = input.bool(true, "Shade the bar background", group = "Settings")
insideCol = input.color(#787B86, "Inside bar", group = "Style")
outsideCol = input.color(#EF5350, "Outside bar", group = "Style")
insideBar = high <= high[1] and low >= low[1]
outsideBar = high > high[1] and low < low[1]
plotshape(markInside and insideBar, "Inside bar", location = location.abovebar, style = shape.labeldown, text = "IB", color = color.new(insideCol, 20), textcolor = color.white, size = size.tiny)
plotshape(markOutside and outsideBar, "Outside bar", location = location.abovebar, style = shape.labeldown, text = "OB", color = color.new(outsideCol, 20), textcolor = color.white, size = size.tiny)
bgcolor(shade and insideBar ? color.new(insideCol, 90) : na, title = "Inside bar")
bgcolor(shade and outsideBar ? color.new(outsideCol, 90) : na, title = "Outside bar")
alertcondition(insideBar, "Inside bar", "This bar's range sits inside the previous bar's range")
alertcondition(outsideBar, "Outside bar", "This bar's range covers the whole of the previous bar's range")
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
- An inside bar is a pause and an outside bar is an argument. Neither says which way the next bar resolves.
- Equal highs or lows count as inside here. A stricter definition requiring a lower high and a higher low marks far fewer bars.
- Both patterns are extremely common on low timeframes, so the marks crowd the chart without adding information.
Written from this description
Label inside bars, where the high is not above and the low is not below the previous bar, and outside bars, where the high is above and the low is below the previous bar. Shade the background for each and alert on both.
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.