# Need Help On LWMA

203

Hello

For some resons I need to obtain values of LWMAs(Linear Weighted Moving Average) for different nearby periods. For example

iMA(NULL, Period(), 5, 0, ma_method, PRICE_CLOSE, 0)
iMA(NULL, Period(), 7, 0, ma_method, PRICE_CLOSE, 0)
iMA(NULL, Period(), 9, 0, ma_method, PRICE_CLOSE, 0)
.
.
.
iMA(NULL, Period(), 21, 0, ma_method, PRICE_CLOSE, 0)

Since using this IMA method in a loop is cinsuming too much of time, I tried to calculate the values using this piece of code:
```void FillWMAs()
{

int i=1;
int sumi=1;
WMAs[0]=0;
WMAs[1]=Close[0]*sumi;
Print("i="+i+" Close="+Close[0]+"WMAs[" + i + "]=" + WMAs[i]+"WMAs[" + (i-1) + "]=" + (WMAs[i-1])+"Sum(" + i + ")=" + (sumi) +"Sum(" + (i-1) + ")=" +(sumi-i) +" WMA Indicator["+i+"]=" + iMA(NULL, Period(), i , 0, ma_method, PRICE_CLOSE, 0) + " WMA Indicator["+(i-1)+"]=" + iMA(NULL, Period(), i-1 , 0, ma_method, PRICE_CLOSE, 0));

for(i=2;i<22;i++)
{
WMAs[i]= WMAs[i-1]*sumi+Close[i-1]*i;
sumi=sumi+i;
WMAs[i]=WMAs[i]/sumi;

Print("i="+i+" Close="+Close[0]+"WMAs[" + i + "]=" + WMAs[i]+"WMAs[" + (i-1) + "]=" + (WMAs[i-1])+"Sum(" + i + ")=" + (sumi) +"Sum(" + (i-1) + ")=" +(sumi-i) +" WMA Indicator["+i+"]=" + iMA(NULL, Period(), i , 0, ma_method, PRICE_CLOSE, 0) + " WMA Indicator["+(i-1)+"]=" + iMA(NULL, Period(), i-1 , 0, ma_method, PRICE_CLOSE, 0));
}
}```

"Print" instructions are for debuging.
This seems to be the same formula as its described in program help and all references. but the results were different:

18:26:15 AR_BGEA5 started for testing
18:26:15 AR_DailyOpen EURUSD,M30: removed
18:26:15 2006.01.02 05:00 AR_DailyOpen EURUSD,M30: loaded successfully
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=1 Close=1.18200000WMAs[1]=1.18200000WMAs[0]=0. 00000000Sum(1)=1Sum(0)=0 WMA Indicator[1]=1.18200000 WMA Indicator[0]=0.00000000
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=2 Close=1.18200000WMAs[2]=1. 18260000WMAs[1]=1. 18200000Sum(2)=3Sum(1)=1 WMA Indicator[2]=1.18230000 WMA Indicator[1]=1. 18200000
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=3 Close=1.18200000WMAs[3]=1. 18245000WMAs[2]=1. 18230000Sum(3)=6Sum(2)=3 WMA Indicator[3]=1.18240000 WMA Indicator[2]=1. 18230000
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=4 Close=1.18200000WMAs[4]=1. 18268000WMAs[3]=1. 18240000Sum(4)=10Sum(3)=6 WMA Indicator[4]=1.18250000 WMA Indicator[3]=1. 18240000
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=5 Close=1.18200000WMAs[5]=1. 18286667WMAs[4]=1. 18250000Sum(5)=15Sum(4)=10 WMA Indicator[5]=1.18261333 WMA Indicator[4]=1. 18250000
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=6 Close=1.18200000WMAs[6]=1. 18289524WMAs[5]=1. 18261333Sum(6)=21Sum(5)=15 WMA Indicator[6]=1.18271429 WMA Indicator[5]=1. 18261333
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=7 Close=1.18200000WMAs[7]=1. 18291071WMAs[6]=1. 18271429Sum(7)=28Sum(6)=21 WMA Indicator[7]=1.18279643 WMA Indicator[6]=1. 18271429
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=8 Close=1.18200000WMAs[8]=1. 18288611WMAs[7]=1. 18279643Sum(8)=36Sum(7)=28 WMA Indicator[8]=1.18285556 WMA Indicator[7]=1. 18279643
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=9 Close=1.18200000WMAs[9]=1. 18276444WMAs[8]=1. 18285556Sum(9)=45Sum(8)=36 WMA Indicator[9]=1.18288222 WMA Indicator[8]=1. 18285556
18:26:16 2006.01.02 07:12 AR_BGEA5 EURUSD,M30: i=10 Close=1.18200000WMAs[10]=1. 18286727WMAs[9]=1.18288222Sum(10)=55Sum(9)=45 WMA Indicator[10]=1.18289818 WMA Indicator[9]=1.18288222

as you can see, the values are are different for all periods, even period of 1. can any one please tell me what's wrong with my code?
thanks
Moderator
7544

Don't forget that counting should be directed for begin of data (oldest bar) to end (current bar)
```//+------------------------------------------------------------------+
//| Linear Weighted Moving Average                                   |
//+------------------------------------------------------------------+
void lwma()
{
double sum=0.0,lsum=0.0;
double price;
int    i,weight=0,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<=MA_Period;i++,pos--)
{
price=Close[pos];
sum+=price*i;
lsum+=price;
weight+=i;
}
//---- main calculation loop
pos++;
i=pos+MA_Period;
while(pos>=0)
{
ExtMapBuffer[pos]=sum/weight;
if(pos==0) break;
pos--;
i--;
price=Close[pos];
sum=sum-lsum+price*MA_Period;
lsum-=Close[i];
lsum+=price;
}
//---- zero initial bars
if(ExtCountedBars<1)
for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;
}
//+------------------------------------------------------------------+```
see 'Moving Averages, MA'
203

Thanks Stringo. Now it works well.