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

RE: Adaptive Moving Averges



PureBytes Links

Trading Reference Links

Cameron,

Hi, I just posted this with an ELD attachment, but for the 2000i people....

Here's my attempt at the KAMA and the MAMA, with a wrapper and some others
thrown in. Warning: Using the wrapper is a very flexible way to do it, but
it has high overhead because TS computes all of the averages every bar.

Let me know if this is any help.

- Ken Greenwood

===============================FlexAvg2 (Wrapper)
function===============================
{ Flexible Average - uses different averaging types, Version 2 (additional
input enables use of MAMA/KAMA functions }
inputs:
	AvgType( numericsimple ),	{ -1=NoSmoothing, 0=Average, 1=AverageFC, 2=JMA,
3=Exponential, 4=Linear Regression Smoothing,
								   5=MAMA, 6=FAMA, 7=KAMA, 8=Smoothed KAMA, 9=Median 		}
	Price( numericseries ),		{ price value, usually (H+L)/2 for MAMA,  unused
for KAMA	}
	Len( numericsimple ),		{ MA Length, Unused for MAMA, Period for KAMA				}
	P1( numericsimple ),		{ Phase for JMA, TgtBar for LinReg, Fastest for
MAMA/KAMA 	}
	P2( numericsimple ),		{ Slowest for MAMA/KAMA 									}
	P3( numericsimple ),
	P4( numericsimple ) ;

var:
	OutVal( 0 ) ;

if 		AvgType = -1 then
	OutVal = Price
else if AvgType = 0 then
	OutVal = Average( Price, Len )
else if AvgType = 1 then
	OutVal = AverageFC( Price, Len )
else if AvgType = 2 then
	OutVal = jrc.jma.2k( Price, Len, P1 )
else if AvgType = 3 then
	OutVal = XAverage( Price, Len )
else if AvgType = 4 then
	OutVal = LinearRegValue( Price, Len, P1 )
else if AvgType = 5 then
	value1 = KenMAMA( Price, P1, P2, OutVal, value2 )
else if AvgType = 6 then
	value1 = KenMAMA( Price, P1, P2, value2, OutVal )
else if AvgType = 7 then
	OutVal = KenKAMA( Len, P1, P2 )
else if AvgType = 8 then
	OutVal = LinearRegValue( KenKAMA( Len, P1, P2 ), Price, 0 )
else if AvgType = 9 then
	OutVal = Median( Price, Len ) ;

FlexAvg2 = OutVal ;

===============================End of
Function===============================

===============================KenMAMA
Function===============================

Inputs:
	Price( numericseries ), 		{ usually (H+L)/2 				}
	FastLimit( numericsimple ), 	{ start with .5					}
	SlowLimit( numericsimple ),		{ start with .05				}
	oMAMA( numericref ),			{ MAMA output					}
	oFAMA( numericref ) ;			{ FAMA output					}

Vars:
	Smooth(0),
	Detrender(0),
	I1(0),
	Q1(0),
	jI(0),
	jQ(0),
	I2(0),
	Q2(0),
	Re(0),
	Im(0),
	Period(0),
	SmoothPeriod(0),
	Phase(0),
	DeltaPhase(0),
	alpha(0),
	MAMA(0),
	FAMA(0);

If CurrentBar > 5 then begin
	Smooth = (4*Price + 3*Price[1] + 2*Price[2] + Price[3]) / 10;
	Detrender = (.0962*Smooth + .5769*Smooth[2] - .5769*Smooth[4] -
.0962*Smooth[6])*(.075*Period[1] + .54);	{Compute InPhase and Quadrature
components}
	Q1 = (.0962*Detrender + .5769*Detrender[2] - .5769*Detrender[4] -
.0962*Detrender[6])*(.075*Period[1] + .54);
	I1 = Detrender[3];	{Advance the phase of I1 and Q1 by 90 degrees}
	jI = (.0962*I1 + .5769*I1[2] - .5769*I1[4] - .0962*I1[6])*(.075*Period[1] +
.54);
	jQ = (.0962*Q1 + .5769*Q1[2] - .5769*Q1[4] - .0962*Q1[6])*(.075*Period[1] +
.54);	{Phasor addition for 3 bar averaging)}
	I2 = I1 - jQ;	Q2 = Q1 + jI;	{Smooth the I and Q components before applying
the discriminator}
	I2 = .2*I2 + .8*I2[1];	Q2 = .2*Q2 + .8*Q2[1];	{Homodyne Discriminator}
	Re = I2*I2[1] + Q2*Q2[1];	Im = I2*Q2[1] - Q2*I2[1];
	Re = .2*Re + .8*Re[1];	Im = .2*Im + .8*Im[1];
	If Im <> 0 and Re <> 0 then
		Period = 360/ArcTangent(Im/Re);
	If Period > 1.5*Period[1] then
		Period = 1.5*Period[1];
	If Period < .67*Period[1] then
		Period = .67*Period[1];
	If Period < 6 then
		Period = 6;
	If Period > 50 then
		Period = 50;
	Period = .2*Period + .8*Period[1];
	SmoothPeriod = .33*Period + .67*SmoothPeriod[1];
	If I1 <> 0 then
		Phase = (ArcTangent(Q1 / I1));
	DeltaPhase = Phase[1] - Phase;
	If DeltaPhase < 1 then
		DeltaPhase = 1;
	alpha = FastLimit / DeltaPhase;
	If alpha < SlowLimit then
		alpha = SlowLimit;
	MAMA = alpha*Price + (1 - alpha)*MAMA[1];
	FAMA = .5*alpha*MAMA + (1 - .5*alpha)*FAMA[1];
	end;

oMAMA = MAMA ;
oFAMA = FAMA ;
KenMAMA = MAMA ;

===============================End of
Function===============================

===============================KenKAMA
Function===============================

Inputs:
	Period(Numeric),
	Fastest( numericsimple ), { .6667 }
	Slowest( numericsimple ) ; { .0645 }

Vars:
	Noise(0),
	Signal(0),
	Diff(0),
	efRatio(0),
	Smooth(1),
	AdaptMA(0) ;

Diff = AbsValue(Close - Close[1]);
IF CurrentBar <= Period Then
	AdaptMA = Close;
IF CurrentBar > Period Then Begin
	Signal = AbsValue(Close - Close[Period]);
	Noise = Summation(Diff, Period);
	if Noise <> 0 then
		efRatio = Signal / Noise
	else
		efRatio = efRatio[1] ;
	Smooth = Power(efRatio * (Fastest - Slowest) + Slowest, 2);
	AdaptMA = AdaptMA[1] + Smooth * (Close - AdaptMA[1]);
	End;

KenKAMA = AdaptMA;

===============================End of
Function===============================


-----Original Message-----
From: c [mailto:camacazi@xxxxxxxxxxx]
Sent: Wednesday, February 04, 2004 7:44 PM
To: omega-list@xxxxxxxxxx
Subject: Adaptive Moving Averges


Hi all

Does anybody have any "adaptive moving average" code they are willing to
share with me :O) please please please

I have a few but would like some more ideas....

Any help muchly appreciated :O)
Cheers
Cameron

---