Consolidation and Breakout (Inside Bars)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator('Consolidation and Breakout (Inside Bars)', overlay=true)
markbreakout = input(true, 'Mark Breaker Candle')

var bubreaker = input(color.green, title="Bullish Break Color", inline="2")
var bebreaker = input(color.red, title="Bearish Break Color", inline="2")

colorinside = input(true, 'Colorize inside bars')
var insidecolor = input(#000000, title="Inside Bar(s) Color")
minbars = 1
maxbars = 250
var highlowcolor = input(#e91e63, title="Candle H/L Line Color", tooltip="Set transparency to 0 to make high and low invisible")
var hllinewidth = input(2, title="High/Low Line Width")

up = high 
down = low 
childup = open > close ? open : close
childdown = open > close ? close : open

var mainIndex = 0


upControl = childup > up[bar_index - mainIndex] and bar_index - mainIndex > minbars and bar_index - mainIndex <= maxbars ? true : na
downControl = childdown < down[bar_index - mainIndex] and bar_index - mainIndex > minbars and bar_index - mainIndex <= maxbars ? true : na


//marking candles that closed above/below the zone
plotshape(markbreakout == true and upControl ? 1 : na, style=shape.triangleup, location=location.belowbar, color=bubreaker)
plotshape(markbreakout == true and downControl ? -1 : na, style=shape.triangledown, location=location.abovebar, color=bebreaker)


mainIndex := childdown <= up[bar_index - mainIndex] and childdown >= down[bar_index - mainIndex] and childup <= up[bar_index - mainIndex] and childup >= down[bar_index - mainIndex] and bar_index > 1 ? mainIndex[1] : bar_index

//plotting high and low of the main candle
plot(mainIndex == mainIndex[1] ? up[bar_index - mainIndex] : na, color=highlowcolor, style=plot.style_linebr, linewidth=hllinewidth)
plot(mainIndex == mainIndex[1] ? down[bar_index - mainIndex] : na, color=highlowcolor, style=plot.style_linebr, linewidth=hllinewidth)

barcolor(colorinside == true and bar_index - mainIndex > 0 ? insidecolor : na)

