/*[[ Name := ATRSOV Author := Copyright © 2003, Company Link := http://www.company.com/ Separate Window := Yes First Color := Blue First Draw Type := Histogram First Symbol := 217 Use Second Data := Yes Second Color := Red Second Draw Type := Line Second Symbol := 218 ]]*/ Inputs : FastPeriod(12), SlowPeriod(24), SignalPeriod(9); Variables : shift(0), cnt(0), sum(0), loopbegin1(0), loopbegin2(0), first(True), prevbars(0); Variables : FastATR(0), SlowATR(0); SetLoopCount(0); // initial checkings If FastPeriod < 1 Or SlowPeriod < 1 Or SignalPeriod < 1 Then Exit; If FastPeriod >= SlowPeriod 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-SlowPeriod-1; If loopbegin1 < 0 Then Exit; // not enough bars for counting loopbegin2 = Bars-SlowPeriod-SignalPeriod-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 FastATR = iATR(FastPeriod,shift); SlowATR = iATR(SlowPeriod,shift); SetIndexValue(shift,FastATR-SlowATR); 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 SignalPeriod-1 Begin sum = sum + GetIndexValue(shift+cnt); End; SetIndexValue2(shift,sum/SignalPeriod); loopbegin2 = loopbegin2-1; // prevent to previous bars recounting End;