MovingAverages.mqh is not working!!!

 

Hello,

I need some help with this code, I only need plot a MA (case 1) and EMA (case2) through MovingAverages.mqh but in the first case moving is drawing this way: MA(CASE1)

And the case 2 (EMA) this is the result:

EMA(CASE2)

This is the code.

#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
#include <MovingAverages.mqh>


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot MyMA
#property indicator_label1  "MyMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         MyMABuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MyMABuffer);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   for(int i = 0;i<rates_total;i++)
   {      
      //CASE 1
      MyMABuffer[i] = SimpleMA(i,20,close);
      //CASE 2
      //MyMABuffer[i] = ExponentialMA(i,20,i+1,close); 

   }  
  

   return(rates_total);
  }

Could anyone help me with this? 

I'd really appreciate your help.

Kind regards.

 
Jhojan Alberto Tobon Monsalve:
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.01"
#property strict
#include <MovingAverages.mqh>


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot MyMA
#property indicator_label1  "SMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "EMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Blue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double                  MyMABuffer[];
double                  EmaBuffer[];

input int               InpPeriod=20;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MyMABuffer);
   ArraySetAsSeries(MyMABuffer,false);
   SetIndexBuffer(1,EmaBuffer);
   ArraySetAsSeries(EmaBuffer,false);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   if(prev_calculated==0)
     {
      MyMABuffer[0]= 0.;
      EmaBuffer[0] = 0.;
     }

   for(int i=0;i<rates_total;i++)
     {
      if(rates_total<InpPeriod)
        {
         return 0;
        }

      //CASE 1
      MyMABuffer[i]=SimpleMA(i,InpPeriod,close);

      if(i>0)
        {
         //CASE 2
         // position, period, prev_value, price[]
         EmaBuffer[i]=ExponentialMA(i,InpPeriod,EmaBuffer[i-1],close);
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
blue and orange
 
Anthony Garot:

Thank you so much Antony. Could you tell me how can I achieve the same in MT4? I forgot to say that. Sorry about that :)

Because in MT4 nothing is shown.

I just commented this two lines in your code:


//--- indicator buffers mapping
   SetIndexBuffer(0,MyMABuffer);
   //ArraySetAsSeries(MyMABuffer,false);
   SetIndexBuffer(1,EmaBuffer);
   //ArraySetAsSeries(EmaBuffer,false);

but this is the result in MT4

It is like the end is in the start or something like that.

 
Jhojan Alberto Tobon Monsalve:

Thank you so much Antony. Could you tell me how can I achieve the same in MT4?

Sorry. I don't use MT4.

 
Jhojan Alberto Tobon Monsalve:

I need some help with this code, I only need plot a MA (case 1) and EMA (case2) through MovingAverages.mqh but in the first case moving is drawing this way:

And the case 2 (EMA) this is the result:

This is the code.

Could anyone help me with this? 

I'd really appreciate your help.

Try this (within your 'for' loop):

      if (i<20)
         continue;
         
      //CASE 1
      MyMABuffer[i-19] = SimpleMA(i,20,close);
 
Seng Joo Thio:

Try this (within your 'for' loop):

Hi, Seng thank you so much, the MA is now working but what about EMA, I am trying to do the same but it is not working. Any idea? 

Kind regards.

 
Jhojan Alberto Tobon Monsalve:

Hi, Seng thank you so much, the MA is now working but what about EMA, I am trying to do the same but it is not working. Any idea? 

Try this for EMA:

      MyEMABuffer[rates_total-i] = ExponentialMA(rates_total-i,20,MyEMABuffer[rates_total-i+1],close);
 

So you can use this code by a little modification of @Anthony Garot code for MT4:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.01"
#property strict
#include <MovingAverages.mqh>


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot MyMA
#property indicator_label1  "SMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "EMA"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Blue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double                  MyMABuffer[];
double                  EmaBuffer[];

input int               InpPeriod=20;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MyMABuffer);
   ArraySetAsSeries(MyMABuffer,false);
   SetIndexBuffer(1,EmaBuffer);
   ArraySetAsSeries(EmaBuffer,false);

   SetIndexDrawBegin(0,InpPeriod);
   SetIndexDrawBegin(1,InpPeriod*2);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   ArraySetAsSeries(close,false);

   if(prev_calculated==0)
     {
      MyMABuffer[0]= 0.;
      EmaBuffer[0] = 0.;
     }

   for(int i=0;i<rates_total;i++)
     {
      if(rates_total<InpPeriod)
        {
         return 0;
        }

      //CASE 1
      MyMABuffer[i]=SimpleMA(i,InpPeriod,close);

      if(i>0)
        {
         //CASE 2
         // position, period, prev_value, price[]
         EmaBuffer[i]=ExponentialMA(i,InpPeriod,EmaBuffer[i-1],close);
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Mehrdad Jeddi:

So you can use this code by a little modification of @Anthony Garot code for MT4:

Dude, thank you so much! Awesome, just what I needed it, you have saved me, simple and elegant solution :)

Also Thank you so much Seng  and Anthony for your help. I reeeeeally appreciate your effort. 

If there is some place when I can qualify all of you please let me know.

Kind regards.

 
Jhojan Alberto Tobon Monsalve:

Dude, thank you so much! Awesome, just what I needed it, you have saved me, simple and elegant solution :)

Also Thank you so much Seng  and Anthony for your help. I reeeeeally appreciate your effort. 

If there is some place when I can qualify all of you please let me know.

Kind regards.

You're welcome,