/*[[ Name := Moving Average Author := Copyright © 2003, MetaQuotes Software Corp. Link := http://www.metaquotes.ru/ Separate Window := No First Color := Red First Draw Type := Line Use Second Data := No ]]*/ Inputs : MAPeriod(13); Variables : shift(0), MAType(1), cnt(0), loopbegin(0), first(True), prevbars(0); Variables : sum(0), smconst(0), prev(0), weight(0), linear(0); Variables : MAValue(0), MAstring(""); var: signal(0),noise(0),efRatio(0),i(0), Fastest(0.6667), Slowest(0.0645); SetLoopCount(0); // initial checkings If MAPeriod < 1 Then Exit; // check for additional bars loading or total reloading If Bars < prevbars Or Bars-prevbars>1 Then first = True; prevbars = Bars; If first Then Begin // loopbegin prevent couning of counted bars exclude current loopbegin = Bars-MAPeriod-1; If MAType = 1 then Begin // exponential MA initialization loopbegin = Bars-3; smconst = 2 / (1+MAPeriod); SetIndexValue(Bars-1,Close[Bars-1]); End; If loopbegin < 0 Then Exit; // not enough bars for counting first = False; End; // moving average loopbegin = loopbegin+1; // current bar is to be recounted too For shift = loopbegin Downto 0 Begin MAValue=0; Signal = Abs(Close[shift] - Close[shift+maPeriod]); Noise=0; for i=shift to shift+maPeriod { Noise += Abs(Close[shift] - Close[shift+1]); }; if noise<>0 then efRatio = Signal / Noise; if efRatio>1 then efRatio=1; smconst = pow((efRatio * (Fastest - Slowest) + Slowest),2); prev = GetIndexValue(shift+1); MAValue = smconst * (Close[shift]-prev) + prev; SetIndexValue(shift,MAValue); loopbegin = loopbegin-1; // prevent to previous bars recounting End;