How to combine 2 Moving average with different Periods?

 

Hi,

in attachment you see the source code of custom Moving average. I want to change it so that two Moving averages with several Periods combines together but the output shows only 1 Buffer. For example i have this changes for SMA mode like below:

please help me what is here wrong or what should changes here?

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=0;

extern int MA_Period1=10;
extern int MA_Period2=12;

//---- indicator buffers
double ExtMapBuffer[];
double Buffer1[];
double Buffer2[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---- drawing settings
   IndicatorBuffers(3);

   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   draw_begin=MA_Period-1;
//---- indicator short name
   switch(MA_Method)
     {
      case 1 : short_name="EMA(";  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_Period1+MA_Period2+")");
   SetIndexDrawBegin(0,draw_begin);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexBuffer(1,Buffer1);
   SetIndexBuffer(2,Buffer2);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
//----
   switch(MA_Method)
     {
      case 0 : sma();  break;
      case 1 : ema();  break;
      case 2 : smma(); break;
      case 3 : lwma();
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
void sma()
  {
   double sum1=0;
   double sum2=0;
   int    i,pos,pos1,pos2=Bars-ExtCountedBars-1;
//---- initial accumulation for first MA
   if(pos1<MA_Period1) pos1=MA_Period1;
   {
   for(i=1;i<MA_Period1;i++,pos1--)
     sum1+=Close[pos1];
//---- main calculation loop for first MA
     while(pos1>=0)
     {
     sum1+=Close[pos1];
     Buffer1[pos1]=sum1/MA_Period1;
     sum1-=Close[pos1+MA_Period2-1];
     pos1--;
     }
     }
//---- initial accumulation for 2.th MA
     
  if(pos2<MA_Period2) pos2=MA_Period2;
   {
   for(i=1;i<MA_Period2;i++,pos2--)
      sum2+=Close[pos2];
//---- main calculation loop for 2.th MA
   while(pos2>=0)
     {
      sum2+=Close[pos2];
      Buffer2[pos2]=sum2/MA_Period2;
           sum2-=Close[pos2+MA_Period2-1];
           pos2--;
     }
     }
     ExtMapBuffer[pos]=(Buffer2[pos1]+Buffer1[pos2])/2;
//---- zero initial bars
   if(ExtCountedBars<1)
      for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;
  }
Files:
 
Somewhere in your code I think you need to use Period() for the moving average.
 
hmrt135:

Hi,

in attachment you see the source code of custom Moving average. I want to change it so that two Moving averages with several Periods combines together but the output shows only 1 Buffer. For example i have this changes for SMA mode like below:

please help me what is here wrong or what should changes here?

Hi Hmrt...

#property indicator_buffers 1

Change the above line to 3 to get all 3 buffers.

#property indicator_buffers 3

Recompile and run the indicator and that should work for you.

Hope that helps,

Robert

 
cosmicbeing:

Hi Hmrt...

Change the above line to 3 to get all 3 buffers.

Recompile and run the indicator and that should work for you.

Hope that helps,

Robert


Thank you, I change the code like below and worked well.

//+------------------------------------------------------------------+
//|                                                                  | 
//|                                                   Hamirta MA.mq4 |
//+------------------------------------------------------------------+

#property link      "Hamirta"


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue




extern   int      MA_Period_1               = 13;
extern   int      MA_Period_2               = 12;
extern   int      MA_Type_1                 = MODE_SMA;
extern   int      MA_Type_2                 = MODE_SMA;
extern   int      MA_Shift1                 =0;
extern   int      MA_Shift2                 =0;


double UpBuffer1[];
double UpBuffer2[];
double CLOSE[];
double ma_1,ma_2;

int init()
{
   IndicatorBuffers(3); 

//  SetIndexStyle(0,DRAW_HISTOGRAM, STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(0,UpBuffer1);
   SetIndexShift(0,MA_Shift1);
   SetIndexBuffer(1,UpBuffer2);
   SetIndexShift(1,MA_Shift2);
   SetIndexBuffer(2,CLOSE);

   return(0);
}

int deinit()
{
   return(0);
}



void start()
{
   int limit;
   
   
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   limit=Bars-counted_bars-1;
   

   for(int shift=0; shift<limit; shift++)
       CLOSE[shift] = iClose(NULL,0,shift);

   for(shift=0; shift<limit; shift++)
   {       
      ma_1 = iMAOnArray(CLOSE,0,MA_Period_1,0,MA_Type_1,shift);
      ma_2 = iMAOnArray(CLOSE,0,MA_Period_2,0,MA_Type_2,shift);
      
       UpBuffer1[shift]=ma_1;
       UpBuffer2[shift]=ma_2;
  
}
   

   return(0);
}

Reason: