MTF High Low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mohit_Kakkar08

//@version=5

indicator(title="MTF High Low", shorttitle="MTF HIGH LOW", overlay=true)
length = input.int(6, minval=1)
lower = ta.lowest(low[1], length)
upper = ta.highest(high[1], length)
basis = math.avg(upper, lower)


htf = input(title="Higher Time Frame", defval="D")
htf_lower = request.security(syminfo.tickerid, htf, ta.lowest(low[1], length))
htf_upper = request.security(syminfo.tickerid, htf, ta.highest(high[1], length))
htf_basis = math.avg(htf_upper, htf_lower)

k1 = plot(upper, "High", color=color.red, offset = 0, style=plot.style_line, show_last=1, linewidth=2, trackprice=true, display=display.all)
k2 = plot(lower, "Low", color=color.green, offset = 0, style=plot.style_line, show_last=1, linewidth=2, trackprice=true, display=display.all)
k3 = plot(basis, "Mid", color=color.black, offset = 0, style=plot.style_line, show_last=1, linewidth=2, trackprice=true, display=display.none)

H1 = plot(htf_upper, "High_HTF", color=color.red, offset = 0, style=plot.style_line, show_last=1, linewidth=4, trackprice=true, display=display.all)
H2 = plot(htf_lower, "Low_HTF", color=color.green, offset = 0, style=plot.style_line, show_last=1, linewidth=4, trackprice=true, display=display.all)
H3 = plot(htf_basis, "Mid_HTF", color=color.black, offset = 0, style=plot.style_line, show_last=1, linewidth=4, trackprice=true, display=display.none)

show_table = input(true,  "Show Table?")

tableposition=input.string("top_right",title="Table position", options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"])
tabpos= tableposition== "top_left"? position.top_left:tableposition== "top_center"?position.top_center : tableposition== "top_right"? position.top_right : tableposition== "middle_left"? position.middle_left : tableposition== "middle_center"? position.middle_center : tableposition== "middle_right"? position.middle_right : tableposition== "bottom_left"? position.bottom_left : tableposition== "bottom_center"? position.bottom_center: tableposition== "bottom_right"? position.bottom_right: position.top_right

t0=timeframe.period

var table indicatorTable = table.new(tabpos, 10, 10, border_width=1)
if show_table and (barstate.islast or not barstate.islast)
    // symbols
    table.cell(indicatorTable, 0, 0, text='TF' + ' ' + '=' + ' '+ t0, bgcolor=color.black, text_color=color.white, text_size=size.normal)
    table.cell(indicatorTable, 0, 1, text='High', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 0, 2, text='Low', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 0, 3, text='View', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 1, 1, str.tostring(math.round(upper,0)), text_halign=text.align_center, bgcolor=close < upper ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 1, 2, str.tostring(math.round(lower,0)), text_halign=text.align_center, bgcolor=close < lower ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 1, 3, close<basis?"Bearish":"Bullish", text_halign=text.align_center, bgcolor=close < basis ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.merge_cells(indicatorTable, 0, 0, 1, 0)
    table.cell(indicatorTable, 2, 0, text='TF' + ' ' + '=' + ' '+ htf, bgcolor=color.black, text_color=color.white, text_size=size.normal)
    table.cell(indicatorTable, 2, 1, text='High', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 2, 2, text='Low', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 2, 3, text='View', text_halign=text.align_center, bgcolor=color.silver, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 3, 1, str.tostring(math.round(htf_upper,0)), text_halign=text.align_center, bgcolor=close < htf_upper ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 3, 2, str.tostring(math.round(htf_lower,0)), text_halign=text.align_center, bgcolor=close < htf_lower ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.cell(indicatorTable, 3, 3, close<htf_basis?"Bearish":"Bullish", text_halign=text.align_center, bgcolor=close < htf_basis ? color.red : color.green, text_color=color.black, text_size=size.normal)
    table.merge_cells(indicatorTable, 2, 0, 3, 0)
