LWMA - Linearly weighted moving average

extremist

Well-Known Member
#1
In a 15-day linearly-weighted moving average,
todays closing price is multiplied by 15, yesterdays by 14, and so on until day 1 in the periods range is reached. These results are then added together and divided by the sum of the multipliers (15 + 14 + 13 + + 3 + 2 + 1 = 120).

The linearly weighted moving average was one of the first responses to placing a greater importance on recent data. The popularity of this moving average has been diminished by the exponential moving average, but none the less it still proves to be very useful.
From : investopedia.

Ive done the coding only.
see if it could help u in improving ur systems.

previously i have uploaded this on other websites.
now here to benifit all of us


/************/

function LWMA( P, per )
{
s=0;
pa=0;

for( i = 0; i < per; i++ )
{
s=s+(Ref(P,-i)*(per-i));
pa=pa+(per-i);
}
return (s/pa);
}

P = ParamField("Price field");
Periods = Param("Periods", 15, 2, 300, 1, 10 );

Plot(LWMA( P, Periods ), "LWMA("+Periods +")", ParamColor( "LWMA Color", colorCycle ), ParamStyle("LWMA Style") );

/*************/
 

extremist

Well-Known Member
#2
you can save following code by name LWMA .afl in include folder

function LWMA( P, per )
{
s=0;
pa=0;

for( i = 0; i < per; i++ )
{
s=s+(Ref(P,-i)*(per-i));
pa=pa+(per-i);
}
return (s/pa);
}

then u can use it in any afl after calling the function by

#include <LWMA.afl>;
 

KelvinHand

Well-Known Member
#3
you can save following code by name LWMA .afl in include folder

function LWMA( P, per )
{
s=0;
pa=0;

for( i = 0; i < per; i++ )
{
s=s+(Ref(P,-i)*(per-i));
pa=pa+(per-i);
}
return (s/pa);
}

then u can use it in any afl after calling the function by

#include <LWMA.afl>;

No need to do it.
LWMA is WMA. Already part of ami.
Superimpose the 2 and see.:D

Good effort of showing the formula.
Reference guide for future use.
 

mastermind007

Well-Known Member
#5
:mad:
Similarly many still use older codes for Linear Regression, Hull Moving Avg etc. even after they are now available in AMI.

Cheers
::thumb::
IMHO, Built-in Ami function uses a slightly faster variant which will only take 2 multiplications and one addition per loop regardless of the period specified.
 
Thread starter Similar threads Forum Replies Date
C AmiBroker 0

Similar threads