//+------------------------------------------------------------------+ //| Custom Moving Average.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 Red //---- indicator parameters //---- forward declaration of input parameters with global scope. extern int ExtPeriod=12; extern int ExtType=0; //---- indicator buffers double ExtMapBuffer[1]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { string name="Unknown"; //---- drawing settings SetIndexStyle(0,DRAW_LINE); SetIndexDrawBegin(0,ExtPeriod); SetIndexDigits(5); //---- indicator buffers mapping SetIndicatorArray(ExtMapBuffer,0); //---- switch(ExtType) { case 0: name="SMA"; break; case 1: name="EMA"; break; case 2: name="SMA"; break; case 3: name="LMA"; break; } IndicatorShortName(name+"("+ExtPeriod+")"); //---- initialization done return(0); } //+------------------------------------------------------------------+ //| Moving Averages Convergence/Divergence | //+------------------------------------------------------------------+ int start() { int limit; int counted_bars=IndicatorCounted(); //---- check for possible errors if(counted_bars<0) return(-1); //---- last counted bar will be recounted if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; //---- calculate current moving average value switch(ExtType) { case 0: CalculateSMA(counted_bars); break; /* case 1: CalculateEMA(counted_bars); break; case 2: CalculateSMMA(counted_bars); break; case 3: CalculateLWMA(counted_bars); break; */ } /* //---- macd counted in the 1-st buffer for(int i=0; i=0) { sum+=Close[pos]; ExtMapBuffer[pos]=sum/ExtPeriod; sum-=Close[pos+ExtPeriod-1]; pos--; } //---- zero initial bars if(counted_bars<1) for(i=1;i=0;i--) { wsum=wsum-lsum+Close[i]*ExtPeriod; lsum=lsum-Close[i+ExtPeriod]; lsum=lsum+Close[i]; SetIndexValue(0,i,wsum/weight); //---- store linear & weighted sums for fast calculation g_LastWSum=wsum; g_LastLSum=lsum; } return; } //---- fast mode for(i=0;i<=ExtPeriod;i++) weight+=(ExtPeriod-i); wsum=g_LastWSum-g_LastLSum+Close[0]*ExtPeriod; SetIndexValue(0,0,wsum/weight); //---- } */ //+------------------------------------------------------------------+