Hello again

I've taken my attempt at Ehlers' autocorrelation periodogram further, but am still stuck. When I run the script below, I get a crash. Any suggestions? I've tried to convert this from the TradeStation code, but I must admit that I am a little lost.

Also, I found that you can't declare an array and set the size using a previously declared variable. eg var my_array[LowCutoff]; results in a syntax error even when LowCutoff is declared. Why is this?

Obviously I'm still a learner when it comes to programming, so any advice is greatly appreciated.

Code:
//===== Ehlers Autocorrelation Periodogram =====

function run()
{
vars Price = series(price());

// Roofing filter applied to price series
var LowCutoff = 10;
var HighCutoff = 48;
vars RoofPrice = series(Roof(Price, LowCutoff, HighCutoff));
vars RoofPriceAGC = series(AGC(RoofPrice, 1)); //normalize output with automatic gain control

// Pearson correlation for each value of lag
var corr[48]; //declare an array to store the correlation coefficients for each value of lag up to the HighCutoff
int lag;
int AvgLength = 0; //Averaging length for the correlation algorithm. Ehlers recommends a value of 3.
int M; //used to set AvgLength to the lag value if AvgLength is not set
for (lag = 0; lag <= HighCutoff; lag++)
	{
		if (AvgLength == 0)
			M = lag;
		else M = AvgLength;
	
		corr[lag] = Correlation(RoofPriceAGC, RoofPriceAGC+lag, M); //assign a correlation coefficient for each value of lag to the array corr[]
	}
	
//Discrete Fourier transform. Correlate autocorrelation values with the cosine and sine of each period of interest. The sum of the squares of each value represents relative power at each period
var cosinePart[38]; //array for storing the cosine values
var sinePart[38]; //array for storing the sine values
var sqSum[38]; //array for storing the squared and summed values of the sine and cosine parts
int period;
int N;
for (period = LowCutoff; period <= HighCutoff; period++)
	{
		for (N = 3; N <= HighCutoff; N++)
		{
			cosinePart[period] = cosinePart[period] + corr[N]*cos((370*N*PI/180)/period);
			sinePart[period] = sinePart[period] + corr[N]*sin((370*N*PI/180)/period);
		}
		sqSum[period] = cosinePart[period]*cosinePart[period] + sinePart[period]*sinePart[period];
	}
	//Smooth power measurements using an EMA
var R[2][35];
for (period = LowCutoff; period <= HighCutoff; period++)
	{
		R[1][period] = R[0][period];
		R[0][period] = 0.2*sqSum[period]*sqSum[period] + 0.8*R[1][period];
	}
//Apply automatic gain control with decay factor 0.995
vars maxPwr = series(0);
maxPwr[0] = 0.995*maxPwr[1];
for (period = LowCutoff; period <= HighCutoff; period++)
	{
		if (R[0][period] > maxPwr[0])
			maxPwr[0] = (R[0][period]);
	}
	
var pwr[45]; 
for (period = 3; period < HighCutoff; period++)
	{
		pwr[period] = R[0][period] / maxPwr[0];
	}	

//Compute dominant cycle using centre of gravity algorithm
var Spx = 0;
var Sp = 0;
var DomPer; //Dominant cycle period
for (period = LowCutoff; period <= HighCutoff; period++)
	{
		if (pwr[period] >= 0.5)
			{
				Spx = Spx + period*pwr[period];
				Sp = Sp + pwr[period];
			}
	}
if (Sp != 0)
	DomPer = Spx/Sp;	
	
plot("DominantCyclePeriod", DomPer, NEW, BLUE);
	
	
}