[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Gaussian MA , my first dll



PureBytes Links

Trading Reference Links

Hi to all c++ programmers
I am a poor one, but I have written a dll for
the NUTS& Bold of Ehlers, you can read a resume below, the purpose is 
to get some polynomial moving average ( he calls them gaussian 
filter )
I join the four poles in c++, and the dll in next mail ( the dll is 
OK)
you can apply these dll with copying and paste
MaxGraph=14;
Graph0=Close;
Graph0Style=68;
Graph0Color=2;
Graph2 = Gauss2ord((H+L)/2,4);
Graph2Color=2;
Graph3 = Gauss3ord((H+L)/2,4);
Graph3Color=3;
Graph4 = Gauss4ord((H+L)/2,4);
Graph4Color=4;
Graph2Style=Graph3Style=Graph4Style=1;

Just could tell me if there is not a simplification of this c++ code. 





************
There is no magic to a Gaussian filter. It is just the multiple 
application of an Exponential Moving Average. From my previous 
article the transfer response of an exponential moving average is
H(z) = a / (1 – (1-a) Z-1)
So that applying the exponential moving average "N" times gives a N 
Pole filter response as 
H(z) = aN / (1 – (1-a) Z-1)N
At zero frequency Z-1=1, so this low pass filter has unity gain.  
Also, the denominator assumes the value of aN at zero frequency. The 
corner frequency of the filter is defined as that point where the 
transfer response is down by 3 dB, or .707 in amplitude. When this 
occurs, we have the relationship
(1 – (1-a) Z-1)N = 1.414aN where Z-1 = e-jw and w = 2p/P
Crunching through the complex arithmetic, we arrive at the solution 
for alpha as
a = -b + SQR(b2 + 2b)
Where b = (1 – cos(w)) / (1.4142/N – 1)

This generalized solution for alpha can be used to compute the 
coefficients for any order Gaussian filter. Recalling that the 
filtered output is determined by the equation
f(z) = H(z)g(z)	where g(z) is the input price

If H(z) is of the form 1/(1 – (1-a) Z-1)N we can easily form 
equations for the output in EasyLanguage metaphor because Z-1 is 
synonymous with a one bar lag. These equations are:
One Pole:	f = ag + (1-a)f[1]
Two Poles:	f = a2g + 2(1-a)f[1] - (1-a)2f[2]
Three Poles:	f = a3g + 3(1-a)f[1] - 3(1-a)2f[2] + (1-a)3f[3]
Four Poles:	f = a4g + 4(1-a)f[1] - 6(1-a)2f[2] + 4(1-a)3f[3] - 
(1-a)4f[4]
*********************************

/*BELOW THE C++ CODE OF GAUSSIAN MA only the foor pole*/




// MovingAverage( array, periods )
// This function demonstrates
// how to calculate 
// Gaussian moving average 4 order



AmiVar Gauss4ord( int NumArgs, AmiVar *ArgsTable )
{
int i, j;
float valg4,prev1,prev2,prev3,prev4;
double alp;
AmiVar result;

result = gSite.AllocArrayResult();

int nSize = gSite.GetArraySize();

float *SrcArray = ArgsTable[ 0 ].array;
int nRange = (int) ArgsTable[ 1 ].val;

j = SkipEmptyValues( nSize, SrcArray, result.array );

for( i = j; i < nSize; i++ )
{
////////////////////////////
// check if there is enough data to calculate average
// skip elsewhere
////////////////////////////
if( i < j + nRange ) 
{
result.array[ i ] = EMPTY_VAL;
continue;
}

///////////////////Initialize/////////////////////////////////


alp = 2.0 / (nRange + 1.0);
valg4= (float)(alp*alp*alp*alp)*SrcArray[i] 
+ (float)(4.0*(1.0-alp))*prev1
- (float)(6.0*(1.0-alp)*(1.0-alp)*prev2) 
+ (float)(4*(1.0-alp)*(1.0-alp)*(1.0-alp)*prev3) 
- (float)((1.0-alp)*(1.0-alp)*(1.0-alp)*(1.0-alp))*prev4;
prev4 = prev3;
prev3 = prev2;
prev2 = prev1;
prev1 = valg4;


result.array[ i ] = (float)valg4;
}
return result;
}

/////////////////////////////////////////////
// Function table now follows
//
// You have to specify each function that should be
// visible for AmiBroker.
// Each entry of the table must contain:
// "Function name", { FunctionPtr, <no. of array args>, <no. of 
string args>, <no. of float args>, <no. of default args>, <pointer to 
default values table float *>

FunctionTag gFunctionTable[] = {
"Gauss2ord",{ Gauss2ord, 1, 0, 1, 0, NULL }, 
"Gauss3ord",{ Gauss3ord, 1, 0, 1, 0, NULL }, 
"Gauss4ord",{ Gauss4ord, 1, 0, 1, 0, NULL }, 
};

int gFunctionTableSize = sizeof( gFunctionTable )/sizeof( 
FunctionTag );