Well, you're a programmer too and are not sure, or are you? I can also only compare your code with mine. Not even God can see at a first glance if a code works correct or not - that's why we have debuggers. Your code works in a different way than mine, but it appears indeed correct at a second glance. However you would save yourself and me a lot time when we used a similar platform so that there's no uncertainty if it's correctly implemented or not. With your code, can you replicate the curve that I posted? That would be the first step, the next step would be to determine when and under which circumstances less lag generates better performance - or not.
By the way, there are many alternative functions with low lag - you might try them too. This is the Zero-Lag MA by John Ehlers: Code: // Zero-lag Moving Average var rError; var rEMA; var ZMA(var* Data,int Period) { var *vEMA = series(*Data,2); var *vZMA = series(*Data,2); var a = smoothF(Period); vEMA[0] = a*Data[0]+(1.-a)*vEMA[1]; rEMA = vEMA[0]; rError = 1000000; var Gain,GainLimit=5,BestGain=0; for(Gain=-GainLimit; Gain < GainLimit; Gain += 0.1) { vZMA[0] = a*(vEMA[0] + Gain*(Data[0]-vZMA[1])) + (1-a)*vZMA[1]; var Error = Data[0] - vZMA[0]; if(abs(Error) < rError) { rError = abs(Error); BestGain = Gain; } } return vZMA[0] = a*(vEMA[0] + BestGain*(Data[0]-vZMA[1])) + (1-a)*vZMA[1]; } It has not zero lag of course, but it has low lag, almost like a lowpass filter. And this is a 3 pole Butterworth filter: Code: var Butterworth(var *Data,int Period) { var a = exp(-PI / Period); var b = 2*a*cos(1.738*PI / Period); var c = a*a; var c1 = b + c; var c2 = -(c + b*c); var c3 = c*c; var c0 = 1 - c1 - c2 - c3; var* Filt = series(*Data,4); return Filt[0] = c0*Data[0] + c1*Filt[1] + c2*Filt[2] + c3*Filt[3]; } I haven't done much with those functions yet, but they might be worth testing when you need faster signals from an MA.
Yes, all functions that manipulate the frequency spectrum of the signal add more or less noise. This is also true for normal MAs without zero lag.
Not true. Normal MAs are lowpass filters, and the output of a lowpass filter is always less noisy than the input, otherwise what's the point of using them? "Zero-lag MAs" aren't true lowpass filters, so no surprise that they add noise.
You're right of course: not the noise, but the signal-to-noise ratio can become worse. The absolute noise, i.e. the high frequencies in the price signal, are always reduced by all functions mentioned here.