Trend Trigger Factor w/ Discontinued Signal Lines [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Trend Trigger Factor w/ Discontinued Signal Lines [Loxx]",
     shorttitle = "TTFDSL [Loxx]", 
     overlay = false, 
     timeframe="", 
     timeframe_gaps = true)

greencolor = #2DD204
redcolor = #D2042D 

darkGreenColor =  #1B7E02 
darkRedColor = #93021F

len = input.int(15, "Period", group = "Basic Settings")
avglen = input.int(5, "Pre-smoothing Period", group = "Basic Settings")
siglen = input.int(10, "Discontinued Signal Lines (DSL) Period", group = "Basic Settings")

colorbars = input.bool(false, "Color bars?", group = "UI Options")

hhRecent = 0.
llRecent = 0.
hhOlder = 0. 
llOlder = 0.

levu = 0., levd = 0., fdn = 0.

mah = ta.ema(high, avglen)
mal = ta.ema(low, avglen)

start = len * 2 - 1
hhOlder := nz(ta.lowest(mah, len)[start])
llOlder := nz(ta.lowest(mal, len)[start])

hhRecent := nz(ta.lowest(mah, len-1)[start])
llRecent := nz(ta.lowest(mal, len-1)[start])

BuyPower = (mah > hhRecent ? mah : hhRecent) - llOlder
SellPower = hhOlder - (mal < llRecent ? mal : llRecent)

val = (BuyPower + SellPower != 0) ? 100.0 * (BuyPower - SellPower) / (0.5 * (BuyPower + SellPower)) : 0

dslAlpha = 2.0 / (1.0 + siglen)

levu := (val > 0) ? nz(levu[1]) + dslAlpha * (val - nz(levu[1])) : nz(levu[1])
levd := (val < 0) ? nz(levd[1]) + dslAlpha * (val - nz(levd[1])) : nz(levd[1])

lmid = (levu + levd)/2
colorout = val >= levu ? darkGreenColor : val <= levd ? darkRedColor : color.gray

lu = plot(levu, color = bar_index % 2 ? color.gray : na)
ld = plot(levd, color = bar_index % 2 ? color.gray : na)
lm = plot(val, color = colorout, linewidth = 3)

plot(lmid, color = bar_index % 2 ? color.gray : na)


fill(lu, lm, val >= levu ? greencolor : na)
fill(ld, lm, val <= levd ? redcolor : na)

coloroutb = val >= levu ? greencolor : val <= levd ? redcolor : color.gray

barcolor(colorbars ? coloroutb : na)































