It feels so great when you contribute to another person, Thank you so much for your invaluable advice; your help is sincerely appreciated.

I would like to have that experience too, through your help, I could help more users feel the same as I feel, and you feel...

I am sharing the solution, I know Zorro is not meant for graphical display, rather it is for high-performance trading.

Maybe the Gaussian Function could inspire others to change their viewpoint.

Code
function gauss_elimination(int n, var* a, var* b, var* x) {
    int i, j, k, l;
    var q, m, t;

    for (k = 0; k < n - 1; k++) {
        l = k;
        m = fabs(a[k * n + k]);
        for (i = k + 1; i < n; i++) {
            if (fabs(a[i * n + k]) > m) {
                m = fabs(a[i * n + k]);
                l = i;
            }
        }
        if (m == 0) return; // Singular matrix

        if (l != k) {
            for (j = 0; j < n; j++) {
                t = a[k * n + j];
                a[k * n + j] = a[l * n + j];
                a[l * n + j] = t;
            }
            t = b[k];
            b[k] = b[l];
            b[l] = t;
        }

        for (i = k + 1; i < n; i++) {
            q = a[i * n + k] / a[k * n + k];
            for (j = k; j < n; j++) {
                a[i * n + j] -= q * a[k * n + j];
            }
            b[i] -= q * b[k];
        }
    }

    for (i = n - 1; i >= 0; i--) {
        t = b[i];
        for (j = i + 1; j < n; j++) {
            t -= a[i * n + j] * x[j];
        }
        x[i] = t / a[i * n + i];
    }
}

void calculateGaussianFilter(vars ClosePrices, vars Filtered, vars UpperBand, vars LowerBand, int period, int RegressionDegree, var KNL_Dev) {
    // Variables for regression calculation
    var a[100], b[10], x[10], sx[20];
    var sum, sq;
    int i, j, k, n, nn;
    n = period - 1;
    nn = RegressionDegree + 1;

    // Calculate sx
    sx[0] = n + 1;
    for (i = 1; i <= nn * 2 - 1; i++) {
        sum = 0.0;
        for (k = 0; k <= n; k++) sum += pow(k, i);
        sx[i] = sum;
    }

    // Calculate syx
    for (i = 0; i < nn; i++) {
        sum = 0.0;
        for (k = 0; k <= n; k++) {
            if (i == 0) sum += ClosePrices[k];
            else sum += ClosePrices[k] * pow(k, i);
        }
        b[i] = sum;
    }

    // Fill matrix a
    for (k = 0; k < nn; k++) 
        for (j = 0; j < nn; j++) 
            a[j * nn + k] = sx[j + k];

    // Perform Gauss elimination
    gauss_elimination(nn, a, b, x);

    // Calculate the sum of squares
    sq = 0.0;
    for (k = 0; k <= n; k++) {
        sum = 0.0;
        for (i = 1; i <= RegressionDegree; i++) {
            sum += x[i] * pow(k, i);
        }
        var fx = x[0] + sum;
        sq += pow(ClosePrices[k] - fx, 2);
    }
    sq = KNL_Dev * sqrt(sq / (n + 1));

    // Calculate regression values and bands
    for (i = 0; i <= n; i++) {
        sum = 0.0;
        for (j = 1; j <= RegressionDegree; j++) {
            sum += x[j] * pow(i, j);
        }
        var fx = x[0] + sum;
        Filtered[i] = fx;
        UpperBand[i] = fx + sq;
        LowerBand[i] = fx - sq;
    }
}

function run() {
    set(PLOTNOW); // Enable plotting

    vars ClosePrices = series(priceClose()); // Get the closing prices
    vars Filtered = series(0); // Initialize the series for filtered values
    vars UpperBand = series(0); // Initialize the series for upper band
    vars LowerBand = series(0); // Initialize the series for lower band

    int RegressionDegree = 5; // Set the degree of the regression
    int period = 20; // Set the period for the Gaussian filter
    var KNL_Dev = 2.72; // Set the deviation multiplier

    if (Bar < period) return;

    // Call the regression-based filter calculation function
    calculateGaussianFilter(ClosePrices, Filtered, UpperBand, LowerBand, period, RegressionDegree, KNL_Dev);

    // Ensure valid values for plotting
    if (is(LOOKBACK)) {
        plot("Close Price", ClosePrices, NEW, BLUE); // Plot the closing prices
        plot("Filtered", Filtered, 0, RED); // Plot the filtered value
        plot("Upper Band", UpperBand, 0, GREEN); // Plot the upper band
        plot("Lower Band", LowerBand, 0, ORANGE); // Plot the lower band
    }
}

Last edited by TipmyPip; 07/26/24 13:33.