/*[[ Name := MACD Author := Copyright © 2003, MetaQuotes Software Corp. Link := http://www.metaquotes.ru/ Separate Window := Yes First Color := White First Draw Type := Histogram Use Second Data := Yes Second Color := Red Second Draw Type := Line ]]*/ Inputs : FastMAPeriod(20), SlowMAPeriod(126), SignalMAPeriod(10); Variables : shift(0), cnt(0), sum(0), loopbegin1(0), loopbegin2(0), first(True), prevbars(0); Variables : FastMA(0), SlowMA(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; // loopbegin1 and loopbegin2 prevent couning of counted bars exclude current If first Then Begin loopbegin1 = Bars-SlowMAPeriod-1; If loopbegin1 < 0 Then Exit; // not enough bars for counting loopbegin2 = Bars-SlowMAPeriod-SignalMAPeriod-2; If loopbegin2 < 0 Then Exit; // not enough bars for counting first = False; // this block is to be evaluated once only End; // convergence-divergence loopbegin1 = loopbegin1+1; // current bar is to be recounted too For shift = loopbegin1 Downto 0 Begin FastMA = iMAEx(FastMAPeriod,MODE_EMA,0,PRICE_CLOSE,shift); SlowMA = iMAEx(SlowMAPeriod,MODE_EMA,0,PRICE_CLOSE,shift); SetIndexValue(shift,(((FastMA+SlowMA)*2.2)/4.4)+(((FastMA-SlowMA)*2.2)/4.4)); loopbegin1 = loopbegin1-1; // prevent to previous bars recounting End; // signal line loopbegin2 = loopbegin2+1; // current bar is to be recounted too For shift = loopbegin2 Downto 0 Begin sum = 0; for cnt = 0 To SignalMAPeriod-1 Begin sum = sum + GetIndexValue(shift+cnt); End; SetIndexValue2(shift,sum/SignalMAPeriod); loopbegin2 = loopbegin2-1; // prevent to previous bars recounting End;