Hi am been trying to compute a weighted moving average as given in Robert Carver's book Advanced Futures.

unlike the EMA the periods used in the MA and the weight applied to the price array is different. for instance, the 32 period WMA is calculated with the weights derived as follows:

Span = 2
Lamda = 2/(C1+1)
w1 = lamda*(1-lamda)^1C
w2 = lamda
(1-lamda)^2* ref(c,-1)
....
w32 = lamda*(1-lamda)^32* ref(c,-32)

WEMA = (W1+w2+w3..+w32)/32

am not proficent in loops and cannot get beyond getting the values for Lamda. one roundabout way of doing this is multiply Lamda with Ref(c,s-shift) and compute an average but that's quite cumbersome. is there any other way efficient way of doing this.

have given the code for a 5 day WEMA but its not possible to do this for 64, 128 and 256 periods.

``````span2 = 2;//
lamda2 = 2/(span2+1);

pp2[1] = lamda2*(1-lamda2);//weight for the first bar.
for( i = 1; i < BarCount; i++ )
{pp2[i] = lamda2*(1-lamda2)^(i);//conitnue computing the weights for 32 bars
;

}

wf1 = pp2[1];//weight for each day's close over the MA computation period
wf2 = pp2[2];
wf3 = pp2[3];
wf4 = pp2[4];
wf5 = pp2[5];
c1 = Ref(C,-1);//close price over the MA computation period
c2 = Ref(C,-2);
c3 = Ref(C,-3);
c4 = Ref(C,-4);
c5 = Ref(C,-5);

wfavg = (wf1*c1+ wf2*c2+wf3*c3+wf4*c4+wf5*c5)/(wf1+wf2+wf3+wf4+wf5);//weighted average

Filter = C > 0;

``````

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.