Ok, Now, it works only sometimes, but it still crashes Thank you for your help, But if you please, I would like to know where in the manual it is stated.

But the code is still causing a crash, if you can please help me, thank you.

I wish someone could help me with this in-depth, in addition, the more we get help, the more users will be able to enjoy Zorro Trader,
and Be passionate about developing ideas.

I do understand that application crashes usually occur when memory or conflicts take place, but it is very hard to really find out what is the problem.
In addition, if there is a more in-depth problem with memory management, and it is a problem with the software, then the developer team will be able to improve and solve the bug.

Code
function filt9x_with_bands(var a, var s, int poles, var deviationMultiplier, var* upperBand, var* lowerBand) {
    int i;
    static var f[10]; // Array to store previous filter values
    var x = 1 - a;
    var filt = s * a;

    // Precompute powers of x to avoid redundant calculations
    static var x_powers[10];
    if (is(INITRUN)) {
        x_powers[1] = x;
        for (i = 2; i <= 9; i++) {
            x_powers[i] = x_powers[i - 1] * x;
        }
    }

    // Calculate the filter value iteratively
    var coefficients[10] = {0, 1, -36, 84, -126, 126, -84, 36, -9, 1};
    for (i = 1; i <= poles; i++) {
        filt += coefficients[i] * x_powers[i] * f[i];
    }

    // Shift the previous values in the array
    for (i = 9; i > 0; i--) {
        f[i] = f[i - 1];
    }
    f[0] = filt;

    // Calculate mean and variance in a single pass
    var sum = 0, varianceSum = 0;
    int count = 10;
    for (i = 0; i < count; i++) {
        sum += f[i];
    }
    var mean = sum / count;

    for (i = 0; i < count; i++) {
        varianceSum += (f[i] - mean) * (f[i] - mean);
    }
    var stddev = sqrt(varianceSum / count);

    // Calculate upper and lower bands
    *upperBand = filt + deviationMultiplier * stddev;
    *lowerBand = filt - deviationMultiplier * stddev;

    return filt;
}

void calculateGaussianFilter(vars src, int poles, int period, vars filtSeries, vars upperBandSeries, vars lowerBandSeries) {
    int i;
    var lag = (period - 1) / (2.0 * poles);
    vars srcData = series(0);

    // Precompute srcData for all bars
    for (i = 0; i < Bar; i++) {
        if (i > lag) {
            srcData[i] = src[i] + (src[i] - src[i - lag]);
        } else {
            srcData[i] = src[i];
        }
    }

    // Calculate filter and bands once per bar
    var upper, lower;
    for (i = 0; i < Bar; i++) {
        filtSeries[i] = filt9x_with_bands(0.5, srcData[i], poles, 1.414, &upper, &lower);
        upperBandSeries[i] = upper;
        lowerBandSeries[i] = lower;
    }
}

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 poles = 3; // Set the number of poles
    int period = 20; // Set the period for the Gaussian filter

    calculateGaussianFilter(ClosePrices, poles, period, Filtered, UpperBand, LowerBand);

    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/20/24 17:02.