T3 Oscillator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Daniel_Ge

//@version=5
indicator("T3 Oscillator", "T3O", overlay = false, timeframe = "")

// Input
src = input(close, "Source")
t3_length = input.int(50, "Length")
smooth_length = input.int(3, "Smoothing Length", minval = 1, tooltip = "1 = no smoothing")
volume_factor = input.float(0.618, "Volume Factor", step = 0.01, tooltip = "0.7 is the original value used by Tim Tillson. Other common values are 0.618 and 0.55")


// T3 MA Function
t3_ma(source, length) =>
    ema_i1 = ta.ema(source, length)
    ema_i2 = ta.ema(ema_i1, length)
    ema_i3 = ta.ema(ema_i2, length)
    ema_i4 = ta.ema(ema_i3, length)
    ema_i5 = ta.ema(ema_i4, length)
    ema_i6 = ta.ema(ema_i5, length)
    const07 = volume_factor
    var1 = -const07 * const07 * const07
    var2 = 3 * const07 * const07 + 3 * const07 * const07 * const07
    var3 = -6 * const07 * const07 - 3 * const07 - 3 * const07 * const07 * const07
    var4 = 1 + 3 * const07 + const07 * const07 * const07 + 3 * const07 * const07
    t3 = var1 * ema_i6 + var2 * ema_i5 + var3 * ema_i4 + var4 * ema_i3
    t3
//

// Calc Oscillator and Smoothing
t3 = ta.sma(src - t3_ma(src, t3_length), smooth_length)

// Plots
hline(0, "Zero Line", linestyle = hline.style_dotted)
p0 = plot(0, editable = false, display = display.none) // placeholder for filling
p1 = plot(t3, "T3 Oscillator", color = color(#d1d4dc), linewidth = 2)
fill(p0, p1, color = t3 > 0 ? color.new(#0095bb, 70) : color.new(#b2b5be, 70), title = "Filling")

// Alerts
co = ta.crossover(t3, 0)
alertcondition(co, "T3O Cross over 0", "")
cu = ta.crossunder(t3, 0)
alertcondition(cu, "T3O Cross under 0")