Generalized Smooth Step
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tarasenko_

//@version=5
indicator("Generalized Smooth Step", precision = 2)




// Input
p = input(36, "Lookback")
eq_order = input.int(5, "Order of the equation (1-18)", 0, 18)




// Functions
// Pascal triangle
pascal_triangle(int a, int b) =>
    float result = 1.
    for i = 0 to b
        if i != b
            result *= (a - i) / (i + 1)
    result

// Smoothstep of Nth order equation
general_smoothstep(int N, x) =>
    array<string> nums = array.new<string>(0)
    result = 0.
    for n = 0 to N
        result += pascal_triangle(-N - 1, n) * pascal_triangle(2 * N + 1, N - n) * math.pow(x, N + n + 1)
    result




// Calculations
h = ta.highest(close, p)
l = ta.lowest(close, p)

osc = (close - l) / (h - l)
smooth_step = general_smoothstep(eq_order, osc)
col = smooth_step > 0.5 ? #00FF00 : #FF0000




// Plottings
plot(smooth_step, "Smooth Step", col, 1)
hline(0.5)
barcolor(col)


// Alerts
alertcondition(col != col[1], "Colour changed!", "Smooth's Step colour change!")
