/*[[ Name := t3_Moving_Average_Support 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(1), MAType(0); Variables : shift(0), cnt(0), loopbegin(0), first(True), prevbars(0); Variables : sum(0), smconst(0), prev(0), weight(0), linear(0); Variables : MAValue(0), MAstring(""); 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; If MAType = 0 Then MAstring = "simple MA" Else If MAType = 1 Then MAstring = "exponential MA" Else If MAType = 2 Then MAstring = "smoothed MA" Else If MAType = 3 Then MAstring = "linear weighted MA"; print(MAstring, " initialized"); End; // moving average loopbegin = loopbegin+1;// current bar is to be recounted too For shift = loopbegin Downto 0 Begin MAValue=0; // 0 - simple If MAType = 0 Then Begin sum = 0; For cnt = 0 To MAPeriod-1 Begin sum = sum + Close[shift+cnt]; End; MAValue = sum/MAPeriod; End // 1 - exponential Else If MAType = 1 Then Begin prev = GetIndexValue(shift+1); MAValue = smconst * (Close[shift]-prev) + prev; End // 2 - smoothed Else If MAType = 2 Then Begin If shift = Bars-MAPeriod Then Begin sum = 0; For cnt = 0 To MAPeriod-1 Begin sum = sum + Close[shift+cnt]; End; End Else Begin prev = GetIndexValue(shift+1); sum = prev*MAPeriod - prev + Close[shift]; End; MAValue = sum/MAPeriod; End // 3 - linear weighted Else If MAType = 3 Then Begin sum = 0; weight = 0; linear = MAPeriod; For cnt = 0 To MAPeriod-1 Begin sum = sum + Close[shift+cnt]*linear; weight = weight + linear; linear = linear - 1; End; MAValue = sum/weight; End; SetIndexValue(shift,MAValue); loopbegin = loopbegin-1;// prevent to previous bars recounting End;