/*[[ Name := MACD Author := Copyright © 2003, MetaQuotes Software Corp.; 2005 David W. Thomas Link := http://www.metaquotes.ru/; davidwt@usa.net Separate Window := Yes First Color := Blue First Draw Type := Line Use Second Data := Yes Second Color := Red Second Draw Type := Line ]]*/ Inputs : FastMAPeriod(12), SlowMAPeriod(26), SignalMAPeriod(9); Variables : shift(0), cnt(0), loopbegin(0), first(True), prevbars(0); Variables : alpha(0), alpha_1(0), FastMA(0), SlowMA(0), macd(0); SetLoopCount(0); // initial checkings If FastMAPeriod < 1 Or SlowMAPeriod < 1 Or SignalMAPeriod < 1 Then Exit; If FastMAPeriod >= SlowMAPeriod Then Exit; // check for additional bars loading or total reloading If Bars < prevbars Or Bars-prevbars>1 Then first = True; prevbars = Bars; // loopbegin prevent couning of counted bars exclude current If first Then Begin loopbegin = Bars-SlowMAPeriod-1; If loopbegin < 0 Then Exit; // not enough bars for counting alpha = 2.0 / (SignalMAPeriod + 1.0); alpha_1 = 1.0 - alpha; first = False; // this block is to be evaluated once only End; // convergence-divergence loopbegin++; // current bar is to be recounted too For shift = loopbegin Downto 0 Begin FastMA = iMAEx(FastMAPeriod,MODE_EMA,0,PRICE_CLOSE,shift); SlowMA = iMAEx(SlowMAPeriod,MODE_EMA,0,PRICE_CLOSE,shift); macd = FastMA-SlowMA; SetIndexValue(shift,macd); SetIndexValue2(shift,alpha*macd + alpha_1*GetIndexValue2(shift+1)); // ema of signal line loopbegin--; // prevent to previous bars recounting End;