# LWMA - Linearly weighted moving average

#### extremist

##### Well-Known Member
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
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
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.

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

##### Well-Known Member
No need to do it.
LWMA is WMA. Already part of ami.
Superimpose the 2 and see.

Good effort of showing the formula.
Reference guide for future use.
Similarly many still use older codes for Linear Regression, Hull Moving Avg etc. even after they are now available in AMI.

Cheers
::thumb::

#### mastermind007

##### Well-Known Member
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.