Tilson Bull-Bear-Marker
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lantzwat

//@version=5
indicator("Tilson Bull-Bear-Marker", "Tilson BBM")

src = input.source(close, 'Source')
L1  =  input.int(5,  "T3 Length 1") // 5
L2  =  input.int(8, "T3 Length 2")  // 8

bShowCrossover = input.bool(true, "Show crossover marker")

T3tilson(float src, int len) =>
    e1        =  ta.ema(src, len)
    e2        =  ta.ema(e1,  len)
    e3        =  ta.ema(e2,  len)
    e4        =  ta.ema(e3,  len)
    e5        =  ta.ema(e4,  len)
    e6        =  ta.ema(e5,  len)
    a         =  0.7 // alternatively 0.618 
    c1        =  -1 * (a * a * a)
    c2        =  3 * (a * a) + 3 * (a * a * a)
    c3        =  -6  * (a * a) - 3 * a - 3 * (a * a * a)
    c4        =  1 + 3 * a + (a * a * a) + 3 * (a * a)
    _return   =  c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3


T3_1 =  T3tilson(src, L1)
T3_2 =  T3tilson(src, L2)

c1  =  T3_1 > T3_1 [1] 
c2  =  T3_2 > T3_2 [1] 

ColorC1 =  c1 ?  color.new(color.blue, 0) : color.new(color.red, 0) 
ColorC2 =  c2 ?  color.new(color.blue, 0) : color.new(color.red, 0) 

lookback = last_bar_index < 20 ? last_bar_index : 20
tdiff = T3_1 - T3_2
factor = 10 / (ta.highest(tdiff,lookback) - ta.lowest(tdiff,lookback))
tdiff := tdiff * factor

tp1 = plot(tdiff, color=ColorC1, linewidth = 3)
tp2 = plot(tdiff * -1, color=ColorC2)

fillcol = c1 == c2 ? ColorC1 : na

fill(tp1, tp2, color=color.new(fillcol,70))

// --------------------

plotshape(bShowCrossover and ta.crossover(T3_1,T3_2) ? tdiff - 5 : na, location = location.absolute, style = shape.triangleup, color = color.new(color.blue,0), size = size.tiny)
plotshape(bShowCrossover and ColorC1 == color.blue and ColorC1[1] == color.red ? tdiff - 5 : na, location = location.absolute, style = shape.triangleup, color = color.new(color.green,0), size = size.tiny)

plotshape(bShowCrossover and ta.crossunder(T3_1,T3_2) ? tdiff + 5 : na, location = location.absolute, style = shape.triangledown, color = color.new(color.red,0), size = size.tiny)
plotshape(bShowCrossover and ColorC1 == color.red and ColorC1[1] == color.blue ? tdiff + 5 : na, location = location.absolute, style = shape.triangledown, color = color.new(color.yellow,0), size = size.tiny)
// Alerts --------------------

alertcondition(ta.crossover(T3_1,T3_2),"Tilson - bullish marker", "Tilson - turned bullish")
alertcondition(ta.crossover(T3_1,T3_2),"Tilson - bearish marker", "Tilson - turned bearish")

alertcondition(ColorC1 == color.blue and ColorC1[1] == color.red, "Tilson - early bullish marker", "Tilson - potentially bullish turn")
alertcondition(ColorC1 == color.red and ColorC1[1] == color.blue, "Tilson - early bearish marker", "Tilson - potentially bearish turn")
