Daily Trend
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © matHodl

//@version=5
indicator("Daily Trend", overlay = true)

res = input.timeframe("D", "Time Frame Analysis", group = "Past Candle Price")
lb = input(1, "Bar Lookback", group = "Past Candle Price")

mid0 = request.security(syminfo.tickerid, res, hl2[lb])
showMid0 = input.bool(true, "M", group = "Past Candle Price", inline = "1")

res3 = request.security(syminfo.tickerid, res, high[lb])
showRes3 = input.bool(true, "R3", group = "Past Candle Price", inline = "2")
res2 = request.security(syminfo.tickerid, res, res3-((res3-mid0)/3))
showRes2 = input.bool(true, "R2", group = "Past Candle Price", inline = "2")
res1 = request.security(syminfo.tickerid, res, mid0+((res3-mid0)/3))
showRes1 = input.bool(true, "R2", group = "Past Candle Price", inline = "2")

sup3 = request.security(syminfo.tickerid, res, low[lb])
showSup3 = input.bool(true, "S3", group = "Past Candle Price", inline = "3")
sup2 = request.security(syminfo.tickerid, res, sup3+((mid0-sup3)/3))
showSup2 = input.bool(true, "S2", group = "Past Candle Price", inline = "3")
sup1 = request.security(syminfo.tickerid, res, mid0-((mid0-sup3)/3))
showSup1 = input.bool(true, "S1", group = "Past Candle Price", inline = "3")

res4 =  request.security(syminfo.tickerid, res, res3+((res3-mid0)/3))
showRes4 = input.bool(true, "R4", group = "Past Candle Price", inline = "2")
sup4 = request.security(syminfo.tickerid, res, sup3-((mid0-sup3)/3))
showSup4 = input.bool(true, "S4", group = "Past Candle Price", inline = "3")

session = request.security(syminfo.tickerid, res, time[lb])

var label TF_close = na
var line line1 =  na
var line line2 =  na
var line line3 =  na
var line line4 =  na
var line line5 =  na
var line line6 =  na
var line line7 =  na
var line line8 =  na
var line line9 =  na

if mid0 and showMid0
    line1 := line.new(bar_index, mid0, bar_index + 10, mid0, color = color.blue, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line1[1])
plot(line.get_price(line1, bar_index), color = color.new(color.blue, 100))

if res1 and showRes1
    line4 := line.new(bar_index, res1, bar_index + 10, res1, color = color.green, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line4[1])
plot(line.get_price(line4, bar_index), color = color.new(color.green, 100))
if res2 and showRes2
    line3 := line.new(bar_index, res2, bar_index + 10, res2, color = color.green, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line3[1])
plot(line.get_price(line3, bar_index), color = color.new(color.green, 100))
if res3 and showRes3
    line2 := line.new(bar_index, res3, bar_index + 10, res3, color = color.green, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line2[1])
plot(line.get_price(line2, bar_index), color = color.new(color.green, 100))
if res4 and showRes4
    line8 := line.new(bar_index, res4, bar_index + 10, res4, color = color.green, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line8[1])
plot(line.get_price(line8, bar_index), color = color.new(color.green, 100))

if sup1 and showSup1
    line7 := line.new(bar_index, sup1, bar_index + 10, sup1, color = color.red, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line7[1])
plot(line.get_price(line7, bar_index), color = color.new(color.red, 100))
if sup2 and showSup2
    line6 := line.new(bar_index, sup2, bar_index + 10, sup2, color = color.red, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line6[1])
plot(line.get_price(line6, bar_index), color = color.new(color.red, 100))
if sup3 and showSup3
    line5 := line.new(bar_index, sup3, bar_index + 10, sup3, color = color.red, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line5[1])
plot(line.get_price(line5, bar_index), color = color.new(color.red, 100))
if sup4 and showSup4
    line9 := line.new(bar_index, sup4, bar_index + 10, sup4, color = color.red, style = line.style_dotted, extend = extend.left, width = 2)
    line.delete(line9[1])
plot(line.get_price(line9, bar_index), color = color.new(color.red, 100))

linefill.new(line2, line8, color = color.new(color.green, 90))
linefill.new(line5, line9, color = color.new(color.red, 90))

//=====Supertrend=====//
atrPeriod = input(10, "ATR Length", group = "SUPERTREND")
factor = input.float(3.0, "Factor", step = 0.01, group = "SUPERTREND")
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

//LTF
resLTF = input.timeframe("15", "LTF Analysis", group = "SUPERTREND")
lbm = request.security(syminfo.tickerid, resLTF, hl2)
lut = request.security(syminfo.tickerid, resLTF, direction < 0 ? supertrend : na)
ldt = request.security(syminfo.tickerid, resLTF, direction < 0? na : supertrend)
lowBodyMiddle = plot(lbm, display = display.none)
lowUpTrend = plot(lut, "Up Trend", color = color.green, style = plot.style_linebr, display = display.none)
lowDownTrend = plot(ldt, "Down Trend", color = color.red, style = plot.style_linebr, display = display.none)
fill(lowBodyMiddle, lowUpTrend, color.new(color.green, 80), fillgaps = false, display = display.none)
fill(lowBodyMiddle, lowDownTrend, color.new(color.red, 80), fillgaps = false, display = display.none)

upLTF = request.security(syminfo.tickerid, resLTF, direction < 0)
downLTF = request.security(syminfo.tickerid, resLTF, direction > 0)

//HTF
resHTF = input.timeframe("60", "HTF Analysis", group = "SUPERTREND")
hbm = request.security(syminfo.tickerid, resHTF, hl2)
hut = request.security(syminfo.tickerid, resHTF, direction < 0 ? supertrend : na)
hdt = request.security(syminfo.tickerid, resHTF, direction < 0? na : supertrend)
highBodyMiddle = plot(hbm, display = display.none)
highUpTrend = plot(hut, "Up Trend", color = color.green, style = plot.style_linebr, display = display.none)
highDownTrend = plot(hdt, "Down Trend", color = color.red, style = plot.style_linebr, display = display.none)
fill(highBodyMiddle, highUpTrend, color.new(color.green, 80), fillgaps = false)
fill(highBodyMiddle, highDownTrend, color.new(color.red, 80), fillgaps = false)

upHTF = request.security(syminfo.tickerid, resHTF, direction < 0)
downHTF = request.security(syminfo.tickerid, resHTF, direction > 0)

up = request.security(syminfo.tickerid, resLTF, ta.change(direction) < 0) and upHTF
down = request.security(syminfo.tickerid, resLTF, ta.change(direction) > 0) and downHTF

plotshape(up, "Up", style = shape.triangleup, textcolor = color.white, color = color.green, size = size.tiny, location = location.belowbar)
plotshape(down, "Down", style = shape.triangledown,textcolor = color.white, color = color.red, size = size.tiny, location = location.abovebar)

var line lineEntry =  na
if up
    lineEntry := line.new(bar_index, high, bar_index + 1, high, color = color.blue, style = line.style_dotted, extend = extend.right, width = 2)
    line.delete(lineEntry[1])
if down
    lineEntry := line.new(bar_index, low, bar_index + 1, low, color = color.blue, style = line.style_dotted, extend = extend.right, width = 2)
    line.delete(lineEntry[1])
plot(line.get_price(lineEntry, bar_index), color = color.new(color.blue, 100))

barcolor(upHTF ? color.lime : downHTF ? color.red : na)