How to get MovingAverage from Indicator in MQ4?

 

Hello,

this should be a trivial, but unfortunately I can not find it anywhere.

I would like to obtain a MovingAverage of some indicator, for example Stoch(5,3,3). How can it by done?

double stoch1 = iStochastic(Symbol(), Period(),5,3,2, MODE_SMA,0,MODE_MAIN,shift); 
double stochEMA1 = iMA(symbol, Period(), 9, 0, MODE_EMA, stoch1, shift);  
   

Please advice.

Thanks.
 
Tomáš:

Hello,

this should be a trivial, but unfortunately I can not find it anywhere.

I would like to obtain a MovingAverage of some indicator, for example Stoch(5,3,3). How can it by done?

Please advice.

Thanks.

use iMAOnArray() after you have the indicator values saved in a buffer or an array

 

Hello Keith,


thanks for your reply. I'm getting closer, but still not there. Please have a look on this piece of code.

A buffer is filled correctly, but average calculated seems to be in different order.


int ma_period = 2;
   //--- buffers
   double Stoch[];
   ArrayResize(Stoch, ma_period + 1);

   int counted_bars = ma_period + 1;
    
   // fill the buffer
   for(int i=0; i< ArraySize(Stoch); i++) Stoch[i]     = iStochastic(Symbol(), Period(),5,3,2, MODE_SMA,0,MODE_MAIN,i); 
   
   
   int as=ArraySize(Stoch);
   for(int x=0;x<as;x++)
      Print("Stoch[",(string)x,"]=",DoubleToStr(Stoch[x],Digits));
      
      
   
   double stoch0 = Stoch[0];
   double stoch1 = Stoch[1];
    
   double Stoch_EMA0 = iMAOnArray(Stoch, Period(), ma_period, 0, MODE_SMA,0); 
   double Stoch_EMA1 = iMAOnArray(Stoch, Period(), ma_period, 0, MODE_SMA,1); 
   
     
   Print("Time = ",MarketInfo(Symbol(), MODE_TIME));
   Print("stoch0= ",stoch0);
   Print("stoch1= ",stoch1);
   Print("Stoch_SMA0= ",Stoch_EMA0);
   Print("Stoch_SMA1= ",Stoch_EMA1);
 

I tried also to change filling order for buffer to:

 // fill the buffer
   //for(int i=0; i< ArraySize(Stoch); i++) Stoch[i]     = iStochastic(Symbol(), Period(),5,3,2, MODE_SMA,0,MODE_MAIN,i); 
   for(int i=ArraySize(Stoch)-1; i>=0; i--) Stoch[i]     = iStochastic(Symbol(), Period(),5,3,2, MODE_SMA,0,MODE_MAIN,i); 
   


Here is a print log:

2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Stoch_SMA1= 45.17634672815446
2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Stoch_SMA0= 24.03977231115201
2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Time = 2018.08.01 23:59
2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Stoch[2]=13.14386
2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Stoch[1]=34.93568
2018.08.18 12:23:58.173 2018.08.01 23:59:36  USDZAR_stp,Daily: Stoch[0]=55.41701
2018.08.18 12:23:58.173 2018.08.01 23:59:34  USDZAR_stp,Daily: Stoch_SMA1= 46.06948425470843
2018.08.18 12:23:58.173 2018.08.01 23:59:34  USDZAR_stp,Daily: Stoch_SMA0= 24.03977231115201


Stoch array is ok, but Stoch_SMA1 and Stoch_SMA0 are swapped, as a correctly I would expect it:

Stoch_EMA0 = 45.17634672815446  
Stoch_SMA1= 24.03977231115201
 

You should not be using  Period() in the iMAOnArray function. You should be setting the parameter as 

 The number of items to be counted. 0 means the whole array. 

I normally only use this function on buffers in an indicator, where the buffer is already a series array.

If you are using ordinary arrays I think that you will need to set it as a series array before using it.

ArraySetAsSeries() 

 

Hello Keith,

yes, this helped partialy.


Now when I use a Simple moving average, then it works fine. When changing to MODE_EMA then I'm getting an incorrect values.

 int ma_period = 2;
   //--- buffers 
   double Stoch[];
   ArrayResize(Stoch, ma_period + 1);
   ArraySetAsSeries(Stoch,true); 

   int counted_bars = ma_period + 1;
    
   // fill the buffer
   for(int i=0; i< ArraySize(Stoch); i++) Stoch[i] = iStochastic(Symbol(), 0, 5,3,2, MODE_SMA,0,MODE_MAIN,i); 
   //for(int i=ArraySize(Stoch)-1; i>=0; i--) Stoch[i]     = iStochastic(Symbol(), 0, 5,3,2, MODE_SMA,0,MODE_MAIN,i); 
      
   int as=ArraySize(Stoch);
   for(int x=0;x<as;x++)
      Print("Stoch[",(string)x,"]=",DoubleToStr(Stoch[x],Digits));
      
   double Stoch_EMA[];
   ArrayResize(Stoch_EMA, ma_period + 1);
   ArraySetAsSeries(Stoch_EMA,true); 
   
   for(int i=0; i< ArraySize(Stoch_EMA); i++) Stoch_EMA[i]     = iMAOnArray(Stoch, 0, ma_period, 0, MODE_EMA, i); 
   //for(int i=ArraySize(Stoch_EMA)-1; i>=0; i--) Stoch_EMA[i]     = iMAOnArray(Stoch, 0, ma_period, 0, MODE_EMA, i); 
   
   as=ArraySize(Stoch_EMA);
   for(int x=0;x<as;x++)
      Print("Stoch_EMA[",(string)x,"]=",DoubleToStr(Stoch_EMA[x],Digits));
   
   
      
   double stoch0 = Stoch[0];
   double stoch1 = Stoch[1];
    
   //double Stoch_EMA0 = iMAOnArray(Stoch, 0, ma_period, 0, MODE_SMA,0); 
   //double Stoch_EMA1 = iMAOnArray(Stoch, 0, ma_period, 0, MODE_SMA,1); 
   double Stoch_EMA0 = Stoch_EMA[0];
   double Stoch_EMA1 = Stoch_EMA[1];
     
   Print("Time = ", TimeToString( MarketInfo(Symbol(), MODE_TIME), TIME_DATE|TIME_MINUTES) );
   //Print("stoch0= ",stoch0);
   //Print("stoch1= ",stoch1);
   Print("Stoch_EMA0= ",Stoch_EMA0);
   Print("Stoch_EMA1= ",Stoch_EMA1);
 

I think that possibly your array size is too small for an EMA because of the way the EMA is calculated.

Try your code in an indicator and use buffers, that way you can easily calculate using all the bars.

 
Keith Watford:

I think that possibly your array size is too small for an EMA because of the way the EMA is calculated.

Try your code in an indicator and use buffers, that way you can easily calculate using all the bars.

Increasing an array size for Stoch[] helped.

Thank you Keith

Reason: