Supply and Demand - Order Block - Energy Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © geneclash

//@version=5
indicator("Supply and Demand - Order Block - Energy Candles", format=format.price, precision=0, overlay=true)

changeColor = input(false,title="Change Box Colors?")
breakType = input.string("Wick+Body",title="Fractal Break Type:",options=["Wick+Body","Body"])
n = input.int(title="Periods", defval=2, minval=2) 

transGreenClr = input.color(color.new(color.green,80),title="Bg:",inline="a_1")
greenClr = input.color(color.new(color.green,0),title="Border:",inline="a_1")

transRedClr = input.color(color.new(color.red,80),title="Bg:",inline="b_1")
redClr = input.color(color.new(color.red,0),title="Border:",inline="b_1")

//Fractals{
// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true

for i = 1 to n
    upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
    upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
    upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
    upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
    upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
    upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4

upFractal = (upflagDownFrontier and flagUpFrontier)

//DownFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true

for i = 1 to n
    downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
    downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
    downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
    downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
    downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
    downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4

downFractal = (downflagDownFrontier and flagDownFrontier)
//}

var float topValue = na, var float bottomValue = na
var int lastRedIndex = na, var float lastRedLow = na, var float lastRedHigh = na
var int lastGreenIndex = na, var float lastGreenLow = na, var float lastGreenHigh = na

var line topLine = na, var line bottomLine = na
var box demandBox = na, var box supplyBox = na

var topBreakBlock = false, var bottomBreakBlock = false
var isLongBreak = false, var isShortBreak = false



topBreakCheckSource = breakType == "Wick+Body" ? high : close
bottomBreakCheckSource = breakType == "Wick+Body" ? low : close

//Last red check
if close < open
    lastRedIndex := bar_index
    lastRedLow := low
    lastRedHigh := high
    
//Last green check
if close > open
    lastGreenIndex := bar_index
    lastGreenLow := low
    lastGreenHigh := high

//Top break
if ta.crossover(topBreakCheckSource,topValue) and topBreakBlock == false
    topBreakBlock := true
    isLongBreak := true
    line.set_x2(topLine,bar_index)
    demandBox := box.new(lastRedIndex-1, lastRedHigh,lastRedIndex+1,lastRedLow, bgcolor=transGreenClr, border_color=greenClr)
    alert("New Demand Zone Created!",freq=alert.freq_once_per_bar_close)

//Bottom break
if ta.crossunder(bottomBreakCheckSource,bottomValue) and bottomBreakBlock == false
    bottomBreakBlock := true
    isShortBreak := true
    line.set_x2(bottomLine,bar_index)
    supplyBox := box.new(lastGreenIndex-1, lastGreenHigh,lastGreenIndex+1,lastGreenLow, bgcolor=transRedClr, border_color=redClr)
    alert("New Supply Zone Created!",freq=alert.freq_once_per_bar_close)

//New up fractal
if upFractal
    topBreakBlock := false
    isLongBreak := false
    topValue := high[n]
    topLine := line.new(bar_index[n],topValue,bar_index,topValue, color=color.teal, style=line.style_dotted, width=2)
    if isLongBreak[1] == false
        line.delete(topLine[1])
    
//New down fractal
if downFractal
    bottomBreakBlock := false
    isShortBreak := false
    bottomValue := low[n]
    bottomLine := line.new(bar_index[n],bottomValue,bar_index,bottomValue, color=color.maroon, style=line.style_dotted, width=2)
    if isShortBreak[1] == false
        line.delete(bottomLine[1])

//Box state update
activeBoxes = box.all
if array.size(activeBoxes) > 0 and changeColor
	for i = 0 to array.size(activeBoxes) - 1
	    bVal = box.get_bottom(array.get(activeBoxes, i))
	    tVal = box.get_top(array.get(activeBoxes, i))
	    if close < bVal
	        box.set_bgcolor(array.get(activeBoxes, i),transRedClr)
	        box.set_border_color(array.get(activeBoxes, i),redClr)
	    if close > tVal
	        box.set_bgcolor(array.get(activeBoxes, i),transGreenClr)
	        box.set_border_color(array.get(activeBoxes, i),greenClr)

//PLOTS
plotshape(downFractal ,style=shape.triangleup, location=location.belowbar, offset=-n, color=color.new(color.gray,80), size = size.tiny)
plotshape(upFractal, style=shape.triangledown,   location=location.abovebar, offset=-n, color=color.new(color.gray,80), size = size.tiny)