SuperTrend Support & Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VonnyFX
//@version=5
indicator("SuperTrend Support & Resistance", "SuperTrend [S & R]", overlay=true, max_bars_back=5000, max_lines_count=500, max_labels_count=500)
import VonnyFX/VonnyPublicLibrary/1 as von

// User inputs
factor        = input.float(0.99, "Factor", step = 0.05, group="Smart Momentum", inline="h")
atrPeriod     = input.int(2, "ATR Period", step = 1, group="Smart Momentum", inline="h")
bar_DEXX      = input.int(300,title="[BAR_INDEX]", group="Smart Momentum", inline="z")
highNLowPiv   = input.bool(defval=false, title="[High & Low]", group="Smart Momentum", inline="z")
closePiv      = input.bool(defval=false, title="[Close]", group="Smart Momentum", inline="z")

// Time Frame #1 Display Settings 

TrendLabel      = input.bool(true,"Trend or Basic", group="Time Frame #1 Display Inputs", inline="j")
noLabel      = input.bool(false,"No Label", group="Time Frame #1 Display Inputs", inline="j")
highColor       = input.color(#00ff99, "High Color", group="Time Frame #1 Display Inputs", inline="y")
lowColor        = input.color(#a97809, "Low Color", group="Time Frame #1 Display Inputs", inline="y")
labelColor      = input.color(color.white,"Label Color", group="Time Frame #1 Display Inputs", inline="y")
lineLengthLeft  = input.int(5, "Line Length Left", group="Time Frame #1 Display Inputs", inline="x")
lineLengthRight = input.int(5, "Length Right", group="Time Frame #1 Display Inputs", inline="x")
lineWidth       = input.int(2, "lineWidth", group="Time Frame #1 Display Inputs", inline="x")
lineStylex      = input.string("Solid", title="|       -----Line Style--|", options=["Solid","Dotted","Dashed"],group="Time Frame #1 Display Inputs", inline="x")
labelDistance   = input.float(0.45, "Label Distance", step=0.05, group="Time Frame #1 Display Inputs", inline="x")
labelSizex      = input.string("Small", title="|--Label Size|", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #1 Display Inputs", inline="x")
labelBgSizex      = input.string("Tiny", title="Label BackGround Size", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #1 Display Inputs", inline="x")
labelBgColor      = input.color(color.new(#000000,0),"BackGround Color", group="Time Frame #1 Display Inputs", inline="x")
// Time Frame #2 Display Settings 
TrendLabel2      = input.bool(true,"Trend or Basic", group="Time Frame #2 Display Inputs", inline="j")
noLabel2      = input.bool(false,"No Label", group="Time Frame #2 Display Inputs", inline="j")
highColor2       = input.color(#22ad22, "High Color", group="Time Frame #2 Display Inputs", inline="y")
lowColor2        = input.color(#e10000, "Low Color", group="Time Frame #2 Display Inputs", inline="y")
labelColor2     = input.color(color.white,"Label Color", group="Time Frame #2 Display Inputs", inline="y")
lineLengthLeft2  = input.int(11, "Line Length Left", group="Time Frame #2 Display Inputs", inline="x")
lineLengthRight2 = input.int(11, "Length Right", group="Time Frame #2 Display Inputs", inline="x")
lineWidth2       = input.int(4, "lineWidth", group="Time Frame #2 Display Inputs", inline="x")
lineStylex2     = input.string("Solid", title="|       -----Line Style--|", options=["Solid","Dotted","Dashed"],group="Time Frame #2 Display Inputs", inline="x")
labelDistance2   = input.float(0.51, "Label Distance", step=0.05, group="Time Frame #2 Display Inputs", inline="x")
labelSizex2      = input.string("Normal", title="|--Label Size|", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #2 Display Inputs", inline="x")
labelBgSizex2      = input.string("Small", title="Label BackGround Size", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #2 Display Inputs", inline="x")
labelBgColor2      = input.color(#000000,"BackGround Color", group="Time Frame #2 Display Inputs", inline="x")
// Time Frame #2 Display Settings 
TrendLabel3      = input.bool(true,"Trend or Basic", group="Time Frame #3 Display Inputs", inline="j")
noLabel3      = input.bool(false,"No Label", group="Time Frame #3 Display Inputs", inline="j")
highColor3       = input.color(#ff1c69, "High Color", group="Time Frame #3 Display Inputs", inline="y")
lowColor3        = input.color(#9c27b0, "Low Color", group="Time Frame #3 Display Inputs", inline="y")
labelColor3     = input.color(color.white,"Label Color", group="Time Frame #3 Display Inputs", inline="y")
lineLengthLeft3  = input.int(20, "Line Length Left", group="Time Frame #3 Display Inputs", inline="x")
lineLengthRight3 = input.int(20, "Length Right", group="Time Frame #3 Display Inputs", inline="x")
lineWidth3       = input.int(6, "lineWidth", group="Time Frame #3 Display Inputs", inline="x")
lineStylex3     = input.string("Solid", title="|       -----Line Style--|", options=["Solid","Dotted","Dashed"],group="Time Frame #3 Display Inputs", inline="x")
labelDistance3   = input.float(0.6, "Label Distance", step=0.05, group="Time Frame #3 Display Inputs", inline="x")
labelSizex3      = input.string("Large", title="|--Label Size|", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #3 Display Inputs", inline="x")
labelBgSizex3      = input.string("Normal", title="Label BackGround Size", options=["Tiny","Small","Normal","Large","Huge"],group="Time Frame #3 Display Inputs", inline="x")
labelBgColor3      = input.color(#000000,"BackGround Color", group="Time Frame #3 Display Inputs", inline="x")
//Time Frame inputs
timeFrameAmount = input.int(1,title="Amount of Time Frames", tooltip="How much time frames to show", group="Higher Time Frame [S & R]", maxval=3, minval=0)
res         = input.timeframe(title= "TimeFrame#1", defval="1", group="Higher Time Frame [S & R]")
res2         = input.timeframe(title= "TimeFrame#2", defval="3", group="Higher Time Frame [S & R]")
res3         = input.timeframe(title= "TimeFrame#3", defval="5", group="Higher Time Frame [S & R]")
chartPositionx = input.string(title="[Chart Position]",group="Trend Chart", defval="Middle Right",
     options=["Top Right", "Top Center", "Top Left", "Middle Right", "Middle Center", "Middle Left", "Bottom Right", "Bottom Center", "Bottom Left"], inline="j")
chartSizex      = input.string("Small", title="Chart Size", options=["Tiny","Small","Normal","Large","Huge"],group="Trend Chart", inline="j")
chartOn      = input.bool(true,"Chart [On/Off]", group="Trend Chart", inline="j")
//get ATR
atr = ta.atr(20)

//chart size Dynamic Size input Setup
var chartSize =  size.normal

if chartSizex == "Tiny"
    chartSize := size.tiny

if chartSizex == "Small"
    chartSize := size.small

if chartSizex == "Normal"
    chartSize := size.normal

if chartSizex == "Large"
    chartSize := size.large

if chartSizex == "Huge"
    chartSize := size.huge

//Label size Dynamic Size input Setup
var labelSize =  size.normal

if labelSizex == "Tiny"
    labelSize := size.tiny

if labelSizex == "Small"
    labelSize := size.small

if labelSizex == "Normal"
    labelSize := size.normal

if labelSizex == "Large"
    labelSize := size.large

if labelSizex == "Huge"
    labelSize := size.huge
//222222222222222222222222222  
var labelSize2 =  size.normal

if labelSizex2 == "Tiny"
    labelSize2 := size.tiny

if labelSizex2 == "Small"
    labelSize2 := size.small

if labelSizex2 == "Normal"
    labelSize2 := size.normal

if labelSizex2 == "Large"
    labelSize2 := size.large

if labelSizex2 == "Huge"
    labelSize2 := size.huge
//3333333333333333333333333333
var labelSize3 =  size.normal

if labelSizex3 == "Tiny"
    labelSize3 := size.tiny

if labelSizex3 == "Small"
    labelSize3 := size.small

if labelSizex3 == "Normal"
    labelSize3 := size.normal

if labelSizex3 == "Large"
    labelSize3 := size.large

if labelSizex3 == "Huge"
    labelSize3 := size.huge
//Label Dynamic Size input Setup
var labelBgSize =  size.normal

if labelBgSizex == "Tiny"
    labelBgSize := size.tiny

if labelBgSizex == "Small"
    labelBgSize := size.small

if labelBgSizex == "Normal"
    labelBgSize := size.normal

if labelBgSizex == "Large"
    labelBgSize := size.large

if labelBgSizex == "Huge"
    labelBgSize := size.huge
//22222222222222222222222222222
var labelBgSize2 =  size.normal

if labelBgSizex2 == "Tiny"
    labelBgSize2 := size.tiny

if labelBgSizex2 == "Small"
    labelBgSize2 := size.small

if labelBgSizex2 == "Normal"
    labelBgSize2 := size.normal

if labelBgSizex2 == "Large"
    labelBgSize2 := size.large

if labelBgSizex2 == "Huge"
    labelBgSize2 := size.huge
//333333333333333333333333333333
var labelBgSize3 =  size.normal

if labelBgSizex3 == "Tiny"
    labelBgSize3 := size.tiny

if labelBgSizex3 == "Small"
    labelBgSize3 := size.small

if labelBgSizex3 == "Normal"
    labelBgSize3 := size.normal

if labelBgSizex3 == "Large"
    labelBgSize3 := size.large

if labelBgSizex3 == "Huge"
    labelBgSize3 := size.huge
// Dynamic Style input setup
var lineStyle = line.style_solid

if lineStylex == "Solid"
    lineStyle := line.style_solid

if lineStylex == "Dotted"
    lineStyle := line.style_dotted

if lineStylex == "Dashed"
    lineStyle := line.style_dashed
//2222222222222222222222222222222
var lineStyle2 = line.style_solid

if lineStylex2 == "Solid"
    lineStyle2 := line.style_solid

if lineStylex2 == "Dotted"
    lineStyle2 := line.style_dotted

if lineStylex2 == "Dashed"
    lineStyle2 := line.style_dashed
//33333333333333333333333333333333
var lineStyle3 = line.style_solid

if lineStylex3 == "Solid"
    lineStyle3 := line.style_solid

if lineStylex3 == "Dotted"
    lineStyle3 := line.style_dotted

if lineStylex3 == "Dashed"
    lineStyle3 := line.style_dashed
//Wait for candle to close before you show signal or change in realtime
closeRrealtime = true
barState = (closeRrealtime == true) ? barstate.isconfirmed : barstate.isrealtime
    
[htf_High, htf_Low, htf_Close, htf_Open]  = request.security(syminfo.tickerid, res, [high[barState ? 0 : 1],low[barState ? 0 : 1], close[barState ? 0 : 1], open[barState ? 0 : 1]]) 
[htf_High2, htf_Low2, htf_Close2, htf_Open2]  = request.security(syminfo.tickerid, res2, [high[barState ? 0 : 1],low[barState ? 0 : 1], close[barState ? 0 : 1], open[barState ? 0 : 1]])
[htf_High3, htf_Low3, htf_Close3, htf_Open3]  = request.security(syminfo.tickerid, res3, [high[barState ? 0 : 1],low[barState ? 0 : 1], close[barState ? 0 : 1], open[barState ? 0 : 1]])

HTF_hRC = closePiv and not highNLowPiv ? htf_Close : htf_High
HTF_lRC = closePiv and not highNLowPiv ? htf_Close : htf_Low
HTF_hRO = closePiv and not highNLowPiv ? htf_Open : htf_High
HTF_lRO = closePiv and not highNLowPiv ? htf_Open : htf_Low

HTF_hRC2 = closePiv and not highNLowPiv  ? htf_Close2 : htf_High2
HTF_lRC2 = closePiv and not highNLowPiv  ? htf_Close2 : htf_Low2
HTF_hRO2 = closePiv and not highNLowPiv  ? htf_Open2 : htf_High2
HTF_lRO2 = closePiv and not highNLowPiv  ? htf_Open2 : htf_Low2

HTF_hRC3 = closePiv and not highNLowPiv  ? htf_Close3 : htf_High3
HTF_lRC3 = closePiv and not highNLowPiv  ? htf_Close3 : htf_Low3
HTF_hRO3 = closePiv and not highNLowPiv  ? htf_Open3 : htf_High3
HTF_lRO3 = closePiv and not highNLowPiv  ? htf_Open3 : htf_Low3

// To get around runtime issue
bar_DEX =(bar_index > bar_DEXX)

// Get SuperTrend Values
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

// Get Higher time Frame and insert SuperTrend
htfSuperTrend  = barstate.isconfirmed ? request.security(syminfo.tickerid, res, supertrend[barState ? 0 : 1]) : na
htfSuperTrend2  = barstate.isconfirmed ? request.security(syminfo.tickerid, res2, supertrend[barState ? 0 : 1]) : na
htfSuperTrend3  = barstate.isconfirmed ? request.security(syminfo.tickerid, res3, supertrend[barState ? 0 : 1]) : na

// Entry signal
bullEC = von.EntryBull(htf_Close,htfSuperTrend)
bearEC = von.EntryBear(htf_Close,htfSuperTrend)
bullEC2 = von.EntryBull(htf_Close2,htfSuperTrend2)
bearEC2 = von.EntryBear(htf_Close2,htfSuperTrend2)
bullEC3 = von.EntryBull(htf_Close3,htfSuperTrend3)
bearEC3 = von.EntryBear(htf_Close3,htfSuperTrend3)
min3Bull = timeFrameAmount < 3 ? true : ta.barssince(bullEC3) > 4
min3Bear = timeFrameAmount < 3 ? true : ta.barssince(bearEC3) > 4
// get lookback
lookBack = von.lookBack(bullEC, bearEC)
lookBack2 = von.lookBack(bullEC2, bearEC2)
lookBack3 = von.lookBack(bullEC3, bearEC3)

//Detect pivots
highPiv = von.HighestPivot(bullEC, bearEC, bar_DEX, htf_High, htf_High) 
highPivClose = von.HighestPivot(bullEC, bearEC, bar_DEX, htf_Close, htf_Open) 
lowPiv  = von.LowestPivot(bullEC, bearEC, bar_DEX, htf_Low, htf_Low) 
lowPivClose = von.LowestPivot(bullEC, bearEC, bar_DEX, htf_Close, htf_Open)
highPiv2 = von.HighestPivot(bullEC2, bearEC2, bar_DEX, htf_High2, htf_High2)
highPivClose2 = von.HighestPivot(bullEC2, bearEC2, bar_DEX, htf_Close2, htf_Open2)
lowPiv2  = von.LowestPivot(bullEC2, bearEC2, bar_DEX, htf_Low2, htf_Low2) 
lowPivClose2 = von.LowestPivot(bullEC2, bearEC2, bar_DEX, htf_Close2, htf_Open2)
highPiv3 = von.HighestPivot(bullEC3, bearEC3, bar_DEX, htf_High3, htf_High3) 
highPivClose3 = von.HighestPivot(bullEC3, bearEC3, bar_DEX, htf_Close3, htf_Open3)
lowPiv3  = von.LowestPivot(bullEC3, bearEC3, bar_DEX, htf_Low3, htf_Low3) 
lowPivClose3 = von.LowestPivot(bullEC3, bearEC3, bar_DEX, htf_Close3, htf_Open3)

myPivotHigh = closePiv == true and highNLowPiv != true ? highPivClose : highPiv 
myPivotLow = closePiv == true and highNLowPiv != true  ? lowPivClose : lowPiv
myPivotHigh2 = closePiv == true and highNLowPiv != true ? highPivClose2 : highPiv2
myPivotLow2= closePiv == true and highNLowPiv != true  ? lowPivClose2 : lowPiv2
myPivotHigh3 = closePiv == true and highNLowPiv != true ? highPivClose3 : highPiv3
myPivotLow3 = closePiv == true and highNLowPiv != true  ? lowPivClose3 : lowPiv3

// Create an array for the last 2 low levels
var l_ = array.new_float(2)
if bullEC
    array.push(l_,lowPiv)
    array.remove(l_,0)

low0 = math.round(array.get(l_,0),5)
low1 = math.round(array.get(l_,1),5)
//222222222222222222222222222
var l_2 = array.new_float(2)
if bullEC2
    array.push(l_2,lowPiv2)
    array.remove(l_2,0)

low02 = math.round(array.get(l_2,0),5)
low12 = math.round(array.get(l_2,1),5)
//33333333333333333333333333
var l_3 = array.new_float(2)
if bullEC3
    array.push(l_3,lowPiv3)
    array.remove(l_3,0)

low03 = math.round(array.get(l_3,0),5)
low13 = math.round(array.get(l_3,1),5)
////////////////////////////
// Create an array for the last 2 low levels
var l_Close = array.new_float(2)
if bullEC
    array.push(l_Close,lowPivClose)
    array.remove(l_Close,0)

lowClose0 = math.round(array.get(l_Close,0),5)
lowClose1 = math.round(array.get(l_Close,1),5)
//222222222222222222222222222
var l_Close2 = array.new_float(2)
if bullEC2
    array.push(l_Close2,lowPivClose2)
    array.remove(l_Close2,0)

lowClose02 = math.round(array.get(l_Close2,0),5)
lowClose12 = math.round(array.get(l_Close2,1),5)
//33333333333333333333333333
var l_Close3 = array.new_float(2)
if bullEC3
    array.push(l_Close3,lowPivClose3)
    array.remove(l_Close3,0)

lowClose03 = math.round(array.get(l_Close3,0),5)
lowClose13 = math.round(array.get(l_Close3,1),5)
////////////////////////////
// Create Labels based on Array and basic LL and HL Trend detection
lowType = low1 < low0 ? "LL" : "HL"
lowCloseType = lowClose1 < lowClose0 ? "LL" : "HL"
lowNCloseType = lowClose1 < low0 ? "LL" : "HL"

lowMultiPiv = closePiv == true and highNLowPiv != true ? lowCloseType : closePiv == false and highNLowPiv == true ? lowType : lowNCloseType

lowLabelColor= lowMultiPiv == "LL" ? lowColor : highColor
labelColorL = TrendLabel ? lowLabelColor : labelColor
//2222222222222222222222222222
lowType2 = low12 < low02 ? "LL" : "HL"
lowCloseType2 =  lowClose12 < lowClose02 ? "LL" : "HL"
lowNCloseType2 = lowClose12 < low02 ? "LL" : "HL"

lowMultiPiv2 = closePiv == true and highNLowPiv != true ? lowCloseType2 : closePiv == false and highNLowPiv == true ? lowType2 : lowNCloseType2

lowLabelColor2= lowMultiPiv2 == "LL" ? lowColor2 : highColor2
labelColorL2 = TrendLabel2 ? lowLabelColor2 : labelColor2
//333333333333333333333333333
lowType3 = low13 < low03 ? "LL" : "HL"
lowCloseType3 =  lowClose13 < lowClose03 ? "LL" : "HL"
lowNCloseType3 = lowClose13 < low03 ? "LL" : "HL"

lowMultiPiv3 = closePiv == true and highNLowPiv != true ? lowCloseType3 : closePiv == false and highNLowPiv == true ? lowType3 : lowNCloseType3 



lowLabelColor3= lowMultiPiv3 == "LL" ? lowColor3 : highColor3
labelColorL3 = TrendLabel3 ? lowLabelColor3 : labelColor3

// Create an array for the last 2 high levels
var h_ = array.new_float(2)
if bearEC
    array.push(h_,highPiv)
    array.remove(h_,0)

high0 = math.round(array.get(h_,0),5)
high1 = math.round(array.get(h_,1),5)
//222222222222222222222222
var h_2 = array.new_float(2)
if bearEC2
    array.push(h_2,highPiv2)
    array.remove(h_2,0)

high02 = math.round(array.get(h_2,0),5)
high12 = math.round(array.get(h_2,1),5)
//33333333333333333333333
var h_3 = array.new_float(2)
if bearEC3
    array.push(h_3,highPiv3)
    array.remove(h_3,0)

high03 = math.round(array.get(h_3,0),5)
high13 = math.round(array.get(h_3,1),5)
////////////////////////////
// Create an array for the last 2 highest close levels
var h_Close = array.new_float(2)
if bearEC
    array.push(h_Close,highPivClose)
    array.remove(h_Close,0)

highClose0 = math.round(array.get(h_Close,0),5)
highClose1 = math.round(array.get(h_Close,1),5)
//222222222222222222222222
var h_Close2 = array.new_float(2)
if bearEC2
    array.push(h_Close2,highPivClose2)
    array.remove(h_Close2,0)

highClose02 = math.round(array.get(h_Close2,0),5)
highClose12 = math.round(array.get(h_Close2,1),5)
//33333333333333333333333
var h_Close3 = array.new_float(2)
if bearEC3
    array.push(h_Close3,highPivClose3)
    array.remove(h_Close3,0)

highClose03 = math.round(array.get(h_Close3,0),5)
highClose13 = math.round(array.get(h_Close3,1),5)
////////////////////////////
// Create Labels based on Array and basic HH and LH Trend detection
highType = high1 > high0 ? "HH" : "LH"
highCloseType = highClose1 > highClose0 ? "HH" : "LH"
highNCloseType =  highClose1 > high0 ? "HH" : "LH"


highMultiPiv = highNLowPiv and not closePiv ? highType : closePiv and not highNLowPiv ? highCloseType : highNCloseType

highLabelColor = highMultiPiv == "HH" ? highColor: lowColor
labelColorH = TrendLabel ? highLabelColor : labelColor
//22222222222222222222222
highType2 = high12 > high02 ? "HH" : "LH"
highCloseType2 = highClose12 > highClose02 ? "HH" : "LH"
highNCloseType2 = highClose12 > high02 ? "HH" : "LH"

highMultiPiv2 = highNLowPiv and not closePiv ? highType2 : closePiv and not highNLowPiv ? highCloseType2 : highNCloseType2


highLabelColor2 = highMultiPiv2 == "HH" ? highColor2: lowColor2
labelColorH2 = TrendLabel2 ? highLabelColor2 : labelColor2
//33333333333333333333333
highType3 = high13 > high03 ? "HH" : "LH"
highCloseType3 = highClose13 > highClose03 ? "HH" : "LH"
highNCloseType3 = highClose13 > high03 ? "HH" : "LH"

highMultiPiv3 = highNLowPiv and not closePiv ? highType3 : closePiv and not highNLowPiv ? highCloseType3 : highNCloseType3

highLabelColor3 = highMultiPiv3 == "HH" ? highColor3: lowColor3
labelColorH3 = TrendLabel3 ? highLabelColor3 : labelColor3

// Create a point to refrence your High and Low lines
highCandle  = bar_DEX ? (ta.highestbars(high,lookBack) * -1) : na
lowCandle   = bar_DEX ? (ta.lowestbars(low,lookBack) * -1) : na
highCandle2  = bar_DEX ? (ta.highestbars(high,lookBack2) * -1) : na
lowCandle2   = bar_DEX ? (ta.lowestbars(low,lookBack2) * -1) : na
highCandle3  = bar_DEX ? (ta.highestbars(high,lookBack3) * -1) : na
lowCandle3   = bar_DEX ? (ta.lowestbars(low,lookBack3) * -1) : na

// Color for Time Frame Chart
var myTrendColor = color.white

if high1 > high0 or low1 > low0
    myTrendColor := highColor

if high1 < high0 or low1 < low0 
    myTrendColor := lowColor


if htf_Close < low1
    myTrendColor := lowColor

if htf_Close > high1
    myTrendColor := highColor

///////////////////////////////
var myTrendColor2 = color.white

if high12 > high02 or  low12 > low02
    myTrendColor2 := highColor2

if low12 < low02 or high12 < high02 
    myTrendColor2 := lowColor2

if htf_Close2 < low12
    myTrendColor2 := lowColor2

if htf_Close2 > high12
    myTrendColor2 := highColor2


/////////////////////////////////
var myTrendColor3 = color.white

if high13 > high03 or low13 > low03 
    myTrendColor3 := highColor3

if low13 < low03 or high13 < high03 
    myTrendColor3 := lowColor3

if htf_Close3 < low13
    myTrendColor3 := lowColor3

if htf_Close3 > high13
    myTrendColor3 := highColor3
    

//plot lines using lowPiv and highPiv values
labelHighsB = bearEC and barstate.isconfirmed and not noLabel and timeFrameAmount >= 1 ? label.new(bar_index[highCandle], (myPivotHigh + (atr*labelDistance)), text = highMultiPiv, style=label.style_label_center, color=labelBgColor, textcolor= color.new(color.gray,100), size=labelBgSize) : na
labelLowsB  = bullEC and barstate.isconfirmed and not noLabel and timeFrameAmount >= 1 ? label.new(bar_index[lowCandle], (myPivotLow - (atr*labelDistance)), text = lowMultiPiv,  style=label.style_label_center, color=labelBgColor,  textcolor= color.new(color.gray,100), size=labelBgSize) : na

labelHighs = bearEC  and barstate.isconfirmed and not noLabel and timeFrameAmount >= 1 ? label.new(bar_index[highCandle], (myPivotHigh + (atr*labelDistance)), text = highMultiPiv, style=label.style_label_center, color=color.new(color.green,100), textcolor= labelColorH, size=labelSize) : na
labelLows  = bullEC  and barstate.isconfirmed and not noLabel and timeFrameAmount >= 1 ? label.new(bar_index[lowCandle], (myPivotLow - (atr*labelDistance)), text = lowMultiPiv,  style=label.style_label_center, color=color.new(color.red,100) ,  textcolor= labelColorL, size=labelSize) : na

mylineLow   = bullEC and barstate.isconfirmed and timeFrameAmount >= 1 ?  line.new(x1=bar_index[lowCandle]  + lineLengthRight, y1=myPivotLow,  x2=bar_index[lowCandle]  - lineLengthLeft, y2=myPivotLow, color=TrendLabel ? labelColorL: lowColor  , width=lineWidth, style=lineStyle) : na 
mylineHigh  = bearEC and barstate.isconfirmed and timeFrameAmount >= 1 ?  line.new(x1=bar_index[highCandle] + lineLengthRight, y1=myPivotHigh, x2=bar_index[highCandle] - lineLengthLeft, y2=myPivotHigh, color=TrendLabel ? labelColorH : highColor  , width=lineWidth, style=lineStyle) : na 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
labelHighsB2 = bearEC2 and barstate.isconfirmed and not noLabel2  and timeFrameAmount >= 2 ? label.new(bar_index[highCandle2], (myPivotHigh2 + (atr*labelDistance2)), text = highMultiPiv2, style=label.style_label_center, color=labelBgColor2, textcolor= color.new(color.gray,100), size=labelBgSize2) : na
labelLowsB2  = bullEC2 and barstate.isconfirmed and not noLabel2  and timeFrameAmount >= 2 ? label.new(bar_index[lowCandle2], (myPivotLow2 - (atr*labelDistance2)), text = lowMultiPiv2,  style=label.style_label_center, color=labelBgColor2,  textcolor= color.new(color.gray,100), size=labelBgSize2) : na

labelHighs2 = bearEC2  and barstate.isconfirmed and not noLabel2 and  timeFrameAmount >= 2 ? label.new(bar_index[highCandle2], (myPivotHigh2 + (atr*labelDistance2)), text = highMultiPiv2, style=label.style_label_center, color=color.new(color.green,100), textcolor= labelColorH2, size=labelSize2) : na
labelLows2  = bullEC2  and barstate.isconfirmed and not noLabel2 and  timeFrameAmount >= 2 ? label.new(bar_index[lowCandle2], (myPivotLow2 - (atr*labelDistance2)), text = lowMultiPiv2,  style=label.style_label_center, color=color.new(color.red,100) ,  textcolor= labelColorL2, size=labelSize2) : na

mylineLow2   = bullEC2 and barstate.isconfirmed and timeFrameAmount >= 2 ?  line.new(x1=bar_index[lowCandle2]  + lineLengthRight2, y1=myPivotLow2,  x2=bar_index[lowCandle2]  - lineLengthLeft2, y2=myPivotLow2, color= TrendLabel2 ? labelColorL2 : lowColor2, width=lineWidth2, style=lineStyle2) : na 
mylineHigh2  = bearEC2 and barstate.isconfirmed and timeFrameAmount >= 2 ?  line.new(x1=bar_index[highCandle2] + lineLengthRight2, y1=myPivotHigh2, x2=bar_index[highCandle2] - lineLengthLeft2, y2=myPivotHigh2, color=TrendLabel2 ? labelColorH2 : highColor2, width=lineWidth2, style=lineStyle2) : na 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
labelHighsB3 = bearEC3 and barstate.isconfirmed and not noLabel3 and timeFrameAmount >= 3 ? label.new(bar_index[highCandle3], (myPivotHigh3 + (atr*labelDistance3)), text = highMultiPiv3, style=label.style_label_center, color=labelBgColor3, textcolor= color.new(color.gray,100), size=labelBgSize3) : na
labelLowsB3  = bullEC3 and barstate.isconfirmed and not noLabel3 and timeFrameAmount >= 3 ? label.new(bar_index[lowCandle3], (myPivotLow3 - (atr*labelDistance3)), text = lowMultiPiv3,  style=label.style_label_center, color=labelBgColor3,  textcolor= color.new(color.gray,100), size=labelBgSize3) : na

labelHighs3 = bearEC3  and barstate.isconfirmed and not noLabel3 and timeFrameAmount >= 3 ? label.new(bar_index[highCandle3], (myPivotHigh3 + (atr*labelDistance3)), text = highMultiPiv3, style=label.style_label_center, color=color.new(color.green,100), textcolor= labelColorH3, size=labelSize3) : na
labelLows3  = bullEC3  and barstate.isconfirmed and not noLabel3 and timeFrameAmount >= 3 ? label.new(bar_index[lowCandle3], (myPivotLow3 - (atr*labelDistance3)), text = lowMultiPiv3,  style=label.style_label_center, color=color.new(color.red,100) ,  textcolor= labelColorL3, size=labelSize3) : na

mylineLow3   = bullEC3 and barstate.isconfirmed and timeFrameAmount >= 3 ?  line.new(x1=bar_index[lowCandle3]  + lineLengthRight3, y1=myPivotLow3,  x2=bar_index[lowCandle3]  - lineLengthLeft3, y2=myPivotLow3, color= TrendLabel3 ? labelColorL3: lowColor3  , width=lineWidth3, style=lineStyle3) : na 
mylineHigh3  = bearEC3 and barstate.isconfirmed and timeFrameAmount >= 3 ?  line.new(x1=bar_index[highCandle3] + lineLengthRight3, y1=myPivotHigh3, x2=bar_index[highCandle3] - lineLengthLeft3, y2=myPivotHigh3, color= TrendLabel3 ? labelColorH3: highColor3  , width=lineWidth3, style=lineStyle3) : na 

// Dynamic Table Position using user input string
var chartPosition =  position.top_right

if chartPositionx == "Top Right"
    chartPosition := position.top_right

if chartPositionx == "Top Center"
    chartPosition := position.top_center

if chartPositionx == "Top Left"
    chartPosition := position.top_left

if chartPositionx == "Middle Right"
    chartPosition := position.middle_right

if chartPositionx == "Middle Center"
    chartPosition := position.middle_center

if chartPositionx == "Middle Left"
    chartPosition := position.middle_left

if chartPositionx == "Bottom Right"
    chartPosition := position.bottom_right

if chartPositionx == "Bottom Center"
    chartPosition := position.bottom_center

if chartPositionx == "Bottom Left"
    chartPosition := position.bottom_left

//Prepare a table
var table myHTFTable = table.new(chartPosition, 3, 1, border_width=1)

//Draw table
if barstate.isconfirmed and chartOn and timeFrameAmount >= 1
    table.cell(myHTFTable, column = 0, row = 0 ,text=res , text_size=chartSize, text_color=color.black, 
     bgcolor=myTrendColor )
if barstate.isconfirmed and chartOn and timeFrameAmount >= 2    
    table.cell(myHTFTable, column = 1, row = 0 ,text=res2 , text_size=chartSize, text_color=color.black, 
     bgcolor=myTrendColor2 )
if barstate.isconfirmed and chartOn and timeFrameAmount >= 3
    table.cell(myHTFTable, column = 2, row = 0 ,text=res3 , text_size=chartSize, text_color=color.black, 
     bgcolor=myTrendColor3)
