3 Moving Averages in one click

 
//+------------------------------------------------------------------+
//|                                       Custom 3Moving Average.mq4 |
//|                                                      Open Source |
//|                                              http://www.mql5.it/ |
//+------------------------------------------------------------------+
#property copyright "Open Source by Grandemario"
#property link      "http://www.mql5.it/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Yellow
#property indicator_color3 Orange

//---- indicator parameters
extern int MA_Period_0=4;
extern int MA_Period_1=9;
extern int MA_Period_2=18;
extern int MA_Method=1;
extern int MA_Shift=0;

//---- indicator buffers
double ExtMapBuffer0[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];

//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexShift(1,MA_Shift);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexShift(2,MA_Shift);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexShift(3,MA_Shift);
         
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(MA_Period_0<2 || MA_Period_0>=MA_Period_1) MA_Period_0=4;
   if(MA_Period_1<=MA_Period_0 || MA_Period_1>=MA_Period_2) MA_Period_1=9;
   if(MA_Period_2<=MA_Period_1) MA_Period_2=18;
   
//---- indicator short name
   switch(MA_Method)
     {
      case 1 : short_name="EMA(";  break; //draw_begin=0; break;
      case 2 : short_name="SMMA("; break;
      case 3 : short_name="LWMA("; break;
      default :
         MA_Method=0;
         short_name="SMA(";
     }
   IndicatorShortName(short_name+MA_Period_0+" "+MA_Period_1+" "+MA_Period_2+")");
   SetIndexLabel(0,"MA"+DoubleToStr(MA_Period_0, 0));
   SetIndexLabel(1,"MA"+DoubleToStr(MA_Period_1, 0));
   SetIndexLabel(2,"MA"+DoubleToStr(MA_Period_2, 0));   
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer0);
   SetIndexBuffer(1,ExtMapBuffer1);
   SetIndexBuffer(2,ExtMapBuffer2);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period_0 || Bars<=MA_Period_1 ||Bars<=MA_Period_2) return(0);
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 1-st MA buffer
   for(int i=0; i<limit; i++){
      ExtMapBuffer0[i]=iMA(NULL,0,MA_Period_0,MA_Shift,MA_Method,PRICE_CLOSE,i);
//---- 2-nd MA buffer
      ExtMapBuffer1[i]=iMA(NULL,0,MA_Period_1,MA_Shift,MA_Method,PRICE_CLOSE,i);
//---- 3-rd MA buffer
      ExtMapBuffer2[i]=iMA(NULL,0,MA_Period_2,MA_Shift,MA_Method,PRICE_CLOSE,i);
//---- done
   }
   return(0);
  }
This simple indicator is particularly useful in backtesting, it avoids to manually enter
moving averages one by one, saving a lot of time.
Files:
 

ciao mario.

io cambiarei

//|                                              http://www.mlq5.it/

in

//|                                              http://www.mql5.it/

eh hai fatto una variabla esterna

extern int MA_Shift=0;

che non stai usando nella calculazione.


//z hui, non ho piú parlato/scritto in italiano da tanto tempo...

 
zzuegg:

ciao mario.

io cambiarei

in

eh hai fatto una variabla esterna

che non stai usando nella calculazione.


//z hui, non ho piú parlato/scritto in italiano da tanto tempo...


Grazie zzuegg! Troppa fretta, ora è corretto!

Ciao

Reason: