Download MetaTrader 5

Is something wrong with my writing of SMA?

To add comments, please log in or register
yoshi1988
30
yoshi1988  

Hi!

I have started learning Metatrader 5 and still progressing.

I have written my first indicator of plotting two SMA lines.

1.Red Line Period: 13

2.Yellow Line Period: 25 

 What I have received is indicated in posted photo, successful red line and unexpected Slanted yellow line.

I am supposed to be getting two SMA lines. 

 Would some one please give me advice?

 

Thank you very much 

 Yoshi 

//+------------------------------------------------------------------+
//|                                                          SMA.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
//---- the indicator will be plotted in the main window
#property indicator_chart_window
//---- one buffer will be used for the calculations and plot of the indicator
#property indicator_buffers 2
//---- only one graphic plot is used 
#property indicator_plots   2
//---- the indicator should be plotted as a line
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
//---- the color of the indicator's line is red 
#property indicator_color1  Red 
#property indicator_color2  Yellow
//---- indicator input parameters
input int MAPeriod = 13; //Averaging period
input int MAShift = 0; //Horizontal shift (in bars)
input int MAPeriod2 = 25;
//---- the declaration of the dynamic array
//that will be used further as an indicator's buffer
double ExtLineBuffer[]; 
double ExtLineBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//----+
//---- assign the dynamic array ExtLineBuffer with 0th indicator's buffer
   SetIndexBuffer(0,ExtLineBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLineBuffer2,INDICATOR_DATA);
//---- set plot shift along the horizontal axis by MAShift bars
   PlotIndexSetInteger(0,PLOT_SHIFT,MAShift);
   PlotIndexSetInteger(1,PLOT_SHIFT,MAShift);
//---- set plot begin from the bar with number MAPeriod
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,MAPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,MAPeriod2);
     
//----+
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(
                const int rates_total,    // number of available bars in history at the current tick
                const int prev_calculated,// number of bars, calculated at previous tick
                const int begin,          // index of the first bar
                const double &price[]     // price array for the calculation
                )
  {
//----+   
   //---- check for the presence of bars, sufficient for the calculation
   if (rates_total < MAPeriod2 - 1 + begin)
    return(0);
   
   //---- declaration of local variables 
   int first, bar, iii;
   double Sum, SMA;
   double Sum2, SMA2;
   
   //---- calculation of starting index first of the main loop
   if(prev_calculated==0) // check for the first start of the indicator
      first=MAPeriod2-1+begin; // start index for all the bars
   else first=prev_calculated-1; // start index for the new bars
   //---- main loop of the calculation
   for(bar = first; bar < rates_total; bar++)
    {    
      Sum=0.0;
      //---- summation loop for the current bar averaging
      for(iii=0;iii<MAPeriod;iii++)
         Sum+=price[bar-iii]; // It's equal to: Sum = Sum + price[bar - iii];
       
      for(iii=0;iii<MAPeriod2;iii++)
         Sum2+=price[bar-iii];
         
         
      //---- calculate averaged value
      SMA=Sum/MAPeriod;
      SMA2=Sum2/MAPeriod2;
      //---- set the element of the indicator buffer with the value of SMA we have calculated
      ExtLineBuffer[bar]=SMA;
      ExtLineBuffer2[bar]=SMA2;
    }
//----+     
   return(rates_total);
  }

//+------------------------------------------------------------------+

 

Alain Verleyen
Moderator
29866
Alain Verleyen  
Please EDIT your post and use the SRC button when you post code. Thanks.
yoshi1988
30
yoshi1988  
angevoyageur:
Please EDIT your post and use the SRC button when you post code. Thanks.

will do !

Thank you for your advice Mr. angevoyageur

Simon Gniadkowski
Moderator
18058
Simon Gniadkowski  
yoshi1988:

will do !

Thank you for your advice Mr. angevoyageur

You were asked to EDIT your post  . . . not create another thread.  Now please EDIT your post,  it is the first post in this thread.  Thank you.

How to use the SRC button.

 

Alain Verleyen
Moderator
29866
Alain Verleyen  
yoshi1988:

will do !

Thank you for your advice Mr. angevoyageur

As it seems so difficult to edit your post, I do it for you this time.

You have to add this line in your loop:

      Sum=0.0;
      Sum2=0.0;
yoshi1988
30
yoshi1988  
angevoyageur:

As it seems so difficult to edit your post, I do it for you this time.

You have to add this line in your loop:

Mr Angevoyageur

I am very sorry creating double threads.

I will be cautious from now on.

 And THank you very much your advice. I totally understood. 

To add comments, please log in or register