Skip to main content

Signal to Noise Ratio (SNR) for Amibroker (AFL)

burgessx almost 16 years ago Amibroker (AFL)

  • Rating:
    3 / 5 (Votes 4)
  • Tags:
    oscillator, amibroker, ehler

The Signal to Noise Ratio (SNR) is derived from John Ehlers work in Rocket Science for Traders. In Rocket Science, Mr. Ehlers “hard coded” the front -end section of the code to input the Homodyne Cycle Period as the adaptive cycle input to measure the SNR. In a subsequent book, Cybernetic Analysis for Stocks and Futures, he derived a new and faster cycle discriminator, the Cybernetic Cycle Period. In order to allow a user maximum flexibility, ANY cycle period function a user has access to in Amibroker code can be called as an input, and is not “hard coded”. The remainder of the code applies the user specified cycle input to measure the SNR.

Values below zero tend to be very congested periods.

Screenshots

Indicator / Formula

Copy & Paste Friendly
	
function SNR(Input, Periods, Alphas)
{
	Result = 0;
	Noise = 0;


	
// we need a seed value here to keep from referencing uninitialized memory
	Result[21] = 0.0;

	for (i = 22; i < BarCount; i++)
	{

		alpha = Alphas[i];

		if (alpha < 0)
			alpha = 0;
		if (alpha > 1)
			alpha = 1;

		period = Periods[i];

		if (period < 4)
			period = 4; // this will guarentee one count in the loop below
		if ((period / 2 - 1) > 99)
			period = (99 + 1) * 2;

		Smooth[i] = (4.0 * Input[i] 
							+ 3.0 * Input[i-1]
							+ 2.0 * Input[i-2]
							+ 1.0 * Input[i-3]) 
							/ 10.0 ;

	
		//Q3[0] = (float)ForceFloatRange( .5 * period);
		Q3[i] =  .5 * (Smooth[i] - Smooth[i-2]) * (.1759*period + .4607) ;

		I3 = 0.0;

		for (count = 0; count <= int(period / 2) - 1; count++)	
			I3 = I3 + Q3[BarCount-1 - count];

		//if (period == 2)
		//	I3 = 0.0;
		//else
			I3 = 1.57 * I3 / int(period / 2);
		
		Signals = I3*I3 + Q3[i]*Q3[i];

		Noise[i] = 
				.1 * (High[i] - Low[i])
				 *(High[i] - Low[i])
			 	 *.25 
				+ .9*Noise[i-1];

		if (Noise[i] != 0 && Signals != 0)
		{
			Result[i] = alpha * (10*log(Signals/Noise[i]) / log(10)) 
								+ (1-alpha) * Result[i-1];
		}		
	}

	return Result;
}

// subtract 6 to center over zero.
SNRs = SNR(Close, 20, .5);

//Plot(Close, "Close", colorBlack);
Plot(SNRs, "SNR", colorRed);

0 comments

Leave Comment

Please login here to leave a comment.