Immediate Trend - VHX
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vulnerable_human_x

//@version=5
indicator("Immediate Trend - VHX", format=format.price, precision=0, overlay=true, max_lines_count=500)


breakType = input.string("Body",title="Fractal Break Type:",options=["Wick+Body","Body"])
n = input.int(title="Periods", defval=1, minval=1) 

bubreakline = input.color(color.lime,title="Bullish Break", inline="1")
bebreakline = input.color(color.red,title="Bullish Break", inline="1")

sthcolor = input.color(color.rgb(255, 255, 255, 47),title="STH", inline="2")
stlcolor = input.color(color.rgb(255, 255, 255, 47),title="STL", inline="2")


showstructure = input.bool(true,"Show Structure Table")
string  i_tableYpos = input.string("bottom", "Table Position", inline = "12", options = ["top", "middle", "bottom"])
string  i_tableXpos = input.string("right", "", inline = "12", options = ["left", "center", "right"])
var table table1 = table.new(i_tableYpos + "_" + i_tableXpos , 1, 2, frame_color = color.rgb(165, 165, 165, 66), frame_width = 2, border_width=1, border_color=color.rgb(255, 255, 255, 79))
size1 = input.string("small", "Text Size", options = ["tiny", "small", "normal", "large", "huge", "auto"])



var int x = 0

// STH
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

STH = (upflagDownFrontier and flagUpFrontier)

//STL
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

STL = (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
    x := 1
    line.set_x2(topLine,bar_index)
    line.set_color(topLine,bubreakline)
    
    
//Bottom break
if ta.crossunder(bottomBreakCheckSource,bottomValue) and bottomBreakBlock == false
    bottomBreakBlock := true
    isShortBreak := true
    x := 0
    line.set_x2(bottomLine,bar_index)
    line.set_color(bottomLine,bebreakline)

//New up fractal
if STH
    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=3)
    
//New down fractal
if STL
    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=3)


//PLOTS
plotshape(STL ,style=shape.triangleup, location=location.belowbar,title="STL", offset=-n, color=stlcolor, size = size.tiny)
plotshape(STH, style=shape.triangledown,   location=location.abovebar,title="STH", offset=-n, color=sthcolor, size = size.tiny)


//table
if barstate.islast and showstructure
    table.cell(table1, 0, 0, "STRUCTURE", width=0,text_valign=text.align_center , text_color = color.white,  text_size=size1, text_halign=text.align_center)
    table.cell(table1, 0, 1, x==1?"Bullish":"Bearish", width=0,text_valign=text.align_center , text_color = x==1?color.green:color.red,  text_size=size1, text_halign=text.align_center)



//IMBALANCE
show_IMB = input.bool(true, "Imbalance", group='IMBALANCE')
imbalancecolor = input.color(color.yellow, title="Imbalance Color", group='IMBALANCE')
fvgTransparency  = input(title="Transparency", defval=60, group='IMBALANCE')
fvgboxLength = input.int(title='Length', defval=0, group='IMBALANCE')

fvgisUp(index) =>
    close[index] > open[index]

fvgisDown(index) =>
    close[index] < open[index]


fvgisObUp(index) =>
    fvgisDown(index + 1) and fvgisUp(index) and close[index] > high[index + 1]


fvgisObDown(index) =>
    fvgisUp(index + 1) and fvgisDown(index) and close[index] < low[index + 1]




bullishFvg = low[0] > high[2]
bearishFvg = high[0] < low[2]
if bullishFvg and show_IMB
    box.new(left=bar_index - 1, top=low[0], right=bar_index + fvgboxLength, bottom=high[2], bgcolor=color.new(imbalancecolor, fvgTransparency), border_color=color.new(imbalancecolor, fvgTransparency))
if bearishFvg and show_IMB
    box.new(left=bar_index - 1, top=low[2], right=bar_index + fvgboxLength, bottom=high[0], bgcolor=color.new(imbalancecolor, fvgTransparency), border_color=color.new(imbalancecolor, fvgTransparency))
//////////////////// FVG //////////////////
