Sinusoidal Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Sinusoidal Moving Average", "SMA", overlay = true)
// This function calculates the sinusoidal moving average of a given data series
// This function calculates the sine-weighted moving average of a given data series
sinusoidal_ma(data, length) =>
    float sum = 0
    float weight_sum = 0
    for i = 0 to length - 1
        a = math.sin((i + 1) * math.pi / (length + 1))
        sum := sum + nz(data[i]) * a
        weight_sum := weight_sum + a
    // Calculate the sine-weighted average using the built-in sma function
    // and the built-in sin function
    sum/weight_sum

// Define the input parameters for the sine-weighted moving average
data = input.source(close, "Data")
length = input.int(20, "Length", 3)

// Plot the sine-weighted moving average on the chart 
plot(sinusoidal_ma(data, length))
