SuperTrend Momentum Table
// 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 Momentum Table", shorttitle="SuperTrend Momentum Table", max_bars_back=5000)

// User inputs
factor = input.float(0.99, "Factor", step = 0.05, tooltip="Momentum Sensitivity", group="Smart Momentum")
atrLength = input.int(2, "ATR Length", step = 1, tooltip="Momentum Sensitivity", group="Smart Momentum")
bullColor1      = input.color(#00ff99, title="Time Frame #1 Bull color", inline="1", group="Time Frame Colors")
bearColor1      = input.color(#f0ba00, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor2      = input.color(#22ad22, title="Time Frame #2 Bull color", inline="1", group="Time Frame Colors")
bearColor2      = input.color(#e10000, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor3      = input.color(#ff1c69, title="Time Frame #3 Bull color", inline="1", group="Time Frame Colors")
bearColor3      = input.color(#9c27b0, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor4      = input.color(#00bcd4, title="Time Frame #4 Bull color", inline="1", group="Time Frame Colors")
bearColor4      = input.color(#fb6417, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor5      = input.color(#f0ba00, title="Time Frame #5 Bull color", inline="1", group="Time Frame Colors")
bearColor5      = input.color(#0c3299, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor6      = input.color(#00ff00, title="Time Frame #6 Bull color", inline="1", group="Time Frame Colors")
bearColor6      = input.color(#4a148c, title="------------------  Bear color", inline="1", group="Time Frame Colors")
bullColor7      = input.color(#faa1a4, title="Time Frame #7 Bull color", inline="1", group="Time Frame Colors")
bearColor7      = input.color(#801922, title="------------------  Bear color", inline="1", group="Time Frame Colors")
momentumSwitch  = input.bool(false, title="[Momentum Switch]", inline="1", group="Momentum Switch")
momentumSwitchBull      = input.color(#ffe900, title="[Momentum Bull]", inline="1", group="Momentum Switch")
momentumSwitchBear      = input.color(#0006ff, title="[Momentum Bear]", inline="1", group="Momentum Switch")
timeFrameAmount = input.int(7,title="Amount of Time Frames", tooltip="How much time frames to fill the table with", group="Higher Time Frame Inputs", maxval=7, minval=1)
res         = input.timeframe(title= "TimeFrame#1", defval="1", tooltip="Choose your desired time frame to fill your table", group="Higher Time Frame Inputs")
res2        = input.timeframe(title= "TimeFrame#2", defval="3", group="Higher Time Frame Inputs")
res3        = input.timeframe(title= "TimeFrame#3", defval="5", group="Higher Time Frame Inputs")
res4        = input.timeframe(title= "TimeFrame#4", defval="15", group="Higher Time Frame Inputs")
res5        = input.timeframe(title= "TimeFrame#5", defval="60", group="Higher Time Frame Inputs")
res6        = input.timeframe(title= "TimeFrame#6", defval="240", group="Higher Time Frame Inputs")
res7        = input.timeframe(title= "TimeFrame#7", defval="D", group="Higher Time Frame Inputs")



//Wait for candle to close before you show signal or change in realtime
closeRrealtime = true
barState = (closeRrealtime == true) ? barstate.isconfirmed : barstate.isrealtime

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

// Get Higher time Frame and insert SuperTrend
htfSuperTrend = request.security(syminfo.tickerid, res, supertrend[barState ? 0 : 1])
htfSuperTrend2 = request.security(syminfo.tickerid, res2, supertrend[barState ? 0 : 1])
htfSuperTrend3 = request.security(syminfo.tickerid, res3, supertrend[barState ? 0 : 1])
htfSuperTrend4 = request.security(syminfo.tickerid, res4, supertrend[barState ? 0 : 1])
htfSuperTrend5 = request.security(syminfo.tickerid, res5, supertrend[barState ? 0 : 1])
htfSuperTrend6 = request.security(syminfo.tickerid, res6, supertrend[barState ? 0 : 1])
htfSuperTrend7 = request.security(syminfo.tickerid, res7, supertrend[barState ? 0 : 1])
htfClose = request.security(syminfo.tickerid, res, close[barState ? 0 : 1])
htfClose2 = request.security(syminfo.tickerid, res2, close[barState ? 0 : 1])
htfClose3 = request.security(syminfo.tickerid, res3, close[barState ? 0 : 1])
htfClose4 = request.security(syminfo.tickerid, res4, close[barState ? 0 : 1])
htfClose5 = request.security(syminfo.tickerid, res5, close[barState ? 0 : 1])
htfClose6 = request.security(syminfo.tickerid, res6, close[barState ? 0 : 1])
htfClose7 = request.security(syminfo.tickerid, res7, close[barState ? 0 : 1])

//HTF signal is true for the first bar that closes above supertrend and for the 
//first bar that closes below supertrend
bull_HTFe = htfClose > htfSuperTrend and htfClose[1] < htfSuperTrend[1]
bear_HTFe = htfClose < htfSuperTrend and htfClose[1] > htfSuperTrend[1]

bull_HTF2 = htfClose2 > htfSuperTrend2 and htfClose2[1] < htfSuperTrend2[1]
bear_HTF2 = htfClose2 < htfSuperTrend2 and htfClose2[1] > htfSuperTrend2[1]

bull_HTF3 = htfClose3 > htfSuperTrend3 and htfClose3[1] < htfSuperTrend3[1]
bear_HTF3 = htfClose3 < htfSuperTrend3 and htfClose3[1] > htfSuperTrend3[1]

bull_HTF4 = htfClose4 > htfSuperTrend4 and htfClose4[1] < htfSuperTrend4[1]
bear_HTF4 = htfClose4 < htfSuperTrend4 and htfClose4[1] > htfSuperTrend4[1]

bull_HTF5 = htfClose5 > htfSuperTrend5 and htfClose5[1] < htfSuperTrend5[1]
bear_HTF5 = htfClose5 < htfSuperTrend5 and htfClose5[1] > htfSuperTrend5[1]

bull_HTF6 = htfClose6 > htfSuperTrend6 and htfClose6[1] < htfSuperTrend6[1]
bear_HTF6 = htfClose6 < htfSuperTrend6 and htfClose6[1] > htfSuperTrend6[1]

bull_HTF7 = htfClose7 > htfSuperTrend7 and htfClose7[1] < htfSuperTrend7[1]
bear_HTF7 = htfClose7 < htfSuperTrend7 and htfClose7[1] > htfSuperTrend7[1]


// If price closes above supertrend return true if not return false.
momentum =  htfClose > htfSuperTrend ? true : false
momentum2 = htfClose2 > htfSuperTrend2 ? true : false
momentum3 = htfClose3 > htfSuperTrend3 ? true : false
momentum4 = htfClose4 > htfSuperTrend4 ? true : false 
momentum5 = htfClose5 > htfSuperTrend5 ? true : false
momentum6 = htfClose6 > htfSuperTrend6 ? true : false 
momentum7 = htfClose7 > htfSuperTrend7 ? true : false 

// Create hLines's to be filled 
hline_   = hline(timeFrameAmount < 7 ? 120: 0, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline0   = hline(timeFrameAmount < 6 ? 120: 20, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline20  = hline(timeFrameAmount < 5 ? 120: 40, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline40  = hline(timeFrameAmount < 4 ? 120: 60, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline60  = hline(timeFrameAmount < 3 ? 120: 80, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline80  = hline(timeFrameAmount < 2 ? 120: 100, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline100 = hline(120, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid)
hline120 = hline(140, color=color.new(color.black,100), editable=false, linewidth=1, linestyle=hline.style_solid, title="More Space?")

// Htf Momentum colors
trendColor  = bull_HTFe and momentumSwitch ? momentumSwitchBull : bear_HTFe and momentumSwitch ? momentumSwitchBear : momentum ? bullColor1 : bearColor1
trendColor2 = bull_HTF2 and momentumSwitch ? momentumSwitchBull : bear_HTF2 and momentumSwitch ? momentumSwitchBear : momentum2 ? bullColor2 : bearColor2
trendColor3 = bull_HTF3 and momentumSwitch ? momentumSwitchBull : bear_HTF3 and momentumSwitch ? momentumSwitchBear : momentum3 ? bullColor3 : bearColor3
trendColor4 = bull_HTF4 and momentumSwitch ? momentumSwitchBull : bear_HTF4 and momentumSwitch ? momentumSwitchBear : momentum4 ? bullColor4 : bearColor4
trendColor5 = bull_HTF5 and momentumSwitch ? momentumSwitchBull : bear_HTF5 and momentumSwitch ? momentumSwitchBear : momentum5 ? bullColor5 : bearColor5
trendColor6 = bull_HTF6 and momentumSwitch ? momentumSwitchBull : bear_HTF6 and momentumSwitch ? momentumSwitchBear : momentum6 ? bullColor6 : bearColor6
trendColor7 = bull_HTF7 and momentumSwitch ? momentumSwitchBull : bear_HTF7 and momentumSwitch ? momentumSwitchBear : momentum7 ? bullColor7 : bearColor7

//Fill hLines's with HTF Momentum Colors
fill(hline_,  hline0, trendColor7, editable=false)
fill(hline0,  hline20, trendColor6, editable=false)
fill(hline20,  hline40, trendColor5, editable=false)
fill(hline40, hline60, trendColor4, editable=false)
fill(hline60, hline80, trendColor3, editable=false)
fill(hline80, hline100, trendColor2, editable=false)
fill(hline100, hline120, trendColor, editable=false)
