2 registered members (TipmyPip, AndrewAMD),
16,415
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Gaussian Crash Error (Solved)
#488320
07/20/24 09:49
07/20/24 09:49
|
Joined: Sep 2017
Posts: 164
TipmyPip
OP
Member
|
OP
Member
Joined: Sep 2017
Posts: 164
|
I have a problem with Zorro Trader, It all the time crashs after running the script, Can anyone please check if the following script displays the Gaussian Indicator in the window , Thank you. 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];
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);
for (i = 0; i < Bar; i++) {
if (i > lag) {
srcData[i] = src[i] + (src[i] - src[i - lag]);
} else {
srcData[i] = src[i];
}
}
for (i = 0; i < Bar; i++) {
var upper, lower;
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/26/24 14:49.
|
|
|
Re: Gaussian Crash Error
[Re: AndrewAMD]
#488322
07/20/24 13:52
07/20/24 13:52
|
Joined: Sep 2017
Posts: 164
TipmyPip
OP
Member
|
OP
Member
Joined: Sep 2017
Posts: 164
|
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. 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.
|
|
|
Re: Gaussian Crash Error
[Re: TipmyPip]
#488324
07/21/24 11:54
07/21/24 11:54
|
Joined: Sep 2017
Posts: 164
TipmyPip
OP
Member
|
OP
Member
Joined: Sep 2017
Posts: 164
|
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. 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.
|
|
|
Working Gaussian Indicator
[Re: TipmyPip]
#488326
07/26/24 13:21
07/26/24 13:21
|
Joined: Sep 2017
Posts: 164
TipmyPip
OP
Member
|
OP
Member
Joined: Sep 2017
Posts: 164
|
Here is a working version of the Gaussian Indicator : 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];
}
}
function calculateRegressionIndicator(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);
// Input parameters
int period = 20; // Calculate only for the last 20 bars
int RegressionDegree = 5;
var KNL_Dev = 2.72;
vars ClosePrices = series(priceClose());
vars Filtered = series(0);
vars UpperBand = series(0);
vars LowerBand = series(0);
if (Bar < period) return;
// Call the indicator calculation function
calculateRegressionIndicator(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
}
}
|
|
|
|