/*[[ Name := AO Author := Copyright © 2003, MetaQuotes Software Corp. Link := http://www.metaquotes.ru/ Separate Window := Yes First Color := LimeGreen First Draw Type := Histogram Use Second Data := Yes Second Color := Red Second Draw Type := Histogram ]]*/ Inputs : FastMAPeriod(13), SlowMAPeriod(34),SignalMAPeriod(13),shift(0); Variables : cnt(0), sum(0), loopbegin(0), first(True), prevbars(0); Variables : OsMA1(0), OsMA2(0),P(0), P2(0),prevP(0); SetLoopCount(0); // initial checkings If FastMAPeriod < 1 Or SlowMAPeriod < 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 counting of counted bars exclude current If first Then Begin loopbegin = Bars-SlowMAPeriod-1; If loopbegin < 0 Then Exit; // not enough bars for counting first = False; // this block is to be evaluated once only End; // convergence-divergence loopbegin = loopbegin+1; // current bar is to be recounted too For shift = loopbegin Downto 0 Begin OsMA1 = iOsMA(FastMAPeriod,SlowMAPeriod,SignalMAPeriod,PRICE_CLOSE,shift); //We need to do it again with a shift back one bar(shift+1) //so we can change colors in the histogram. OsMA2 = iOsMA(FastMAPeriod,SlowMAPeriod,SignalMAPeriod,PRICE_CLOSE,shift+1); P = OsMA1; P2 =OsMA2; //If the current bar is less than the last bar in our histogram we want it to be Red. //If the current bar is greater than the last bar in our histogram we want it to be Green. If P>P2 then SetIndexValue(Shift,P); If P2>P then SetIndexValue2(Shift,P2); loopbegin = loopbegin-1; // prevent to previous bars recounting End;