EMA?

 

I'm having a heck of a time writing a 20 period exponential moving average to plot across this histogram. Any help would be greatly appreciated.

//+------------------------------------------------------------------+
//|                                                       zscore.mq4 |
//|                                                        modulatum |
//+------------------------------------------------------------------+
#property copyright "Mod"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Indigo
#property indicator_level1 0
//---- input parameters
extern int       barsToCount = 2000;
//---- buffers
double zscore[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,3);
   SetIndexBuffer(0,zscore);
   IndicatorShortName("Z Score");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit = barsToCount;
   
//----
   
   for(int i = 0 ;i <= limit-20 ;i++)
   {  
      
      zscore[i] = (iClose(NULL,0,i)-iMA(NULL,0,60,0,0,0,i))/iStdDev(NULL,0,60,0,0,0,i);  
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
stryker251:

I'm having a heck of a time writing a 20 period exponential moving average to plot across this histogram. Any help would be greatly appreciated.

Firstly, your current code seems to recalculate the indicator values for the last 1,980 bars on each tick. I can't see why this is necessary, and why you don't just use the code from https://docs.mql4.com/customind/indicatorcounted to calculate only those values which are "dirty". If it's meant to be a way of avoiding iStdDev() returning 0, and thus causing a zero-divide error, then you should check for that explicitly, rather than trying to work round the problem.

You can then add an EMA as follows, using a period defined by a parameter called EMAPeriod, and storing its results in the array zscore_ema[]:

int start()
{
   // Standard code from https://docs.mql4.com/customind/indicatorcounted for determining 
   // which bars are "dirty", i.e. need to be recalculated
   int limit;
   int counted_bars=IndicatorCounted();
   if (counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   // Loops from oldest "dirty" bar to newest
   for (int i = limit - 1; i >= 0 ; i--) {
      // Get the standard deviation
      double stdev = iStdDev(NULL,0,60,0,0,0,i);

      // Check that the stdev is not zero - because division by zero would
      // terminate the indicator with an error 
      if (stdev != 0) {
         zscore[i] = (iClose(NULL,0,i)-iMA(NULL,0,60,0,0,0,i)) / stdev;  

         // If the previous EMA value is blank, use the current z-score. 
         // Otherwise, calculate the EMA per http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
         if (zscore_ema[i + 1] == EMPTY_VALUE) {
            zscore_ema[i] = zscore[i];
         } else {
            // N.B. (2.0 / (1.0 + EMAPeriod)) is different to (2 / (1 + EMAPeriod)). The latter
            // is interpreted by MQ4 as an integer calculation, and therefore returns 0.
            // Using 2.0 rather than 2 etc tells MQ4 to treat the values as doubles. 
            zscore_ema[i] = ((zscore[i] - zscore_ema[i + 1]) * (2.0 / (1.0 + EMAPeriod))) + zscore_ema[i + 1];
         }
      }
   }
   return(0);
}
 
Your stdDev is looking back 60 bars. This is how you code the loop limits. The counted-- is unnecessary.
 

Wow! You guys are great!! Zero divide was a problem...I’m leaning so much here due to your generosity. Thank you

 
stryker251:

I'm having a heck of a time writing a 20 period exponential moving average to plot across this histogram. Any help would be greatly appreciated.

//---- indicators
   IndicatorBuffers(1);  << why (1) and not (0)
   SetIndexStyle(0,DRAW_HISTOGRAM,0,3);
   SetIndexBuffer(0,zscore);
   IndicatorShortName("Z Score");
 
Agent86:
<< why (1) and not (0)
https://docs.mql4.com/customind/IndicatorBuffers
 
#property  indicator_buffers 1
I know but I thought 1 means starting at number 0 ?
 
"Allocates memory for buffers used for custom indicator calculations. " if you allocate memory for 0 buffers that is 0 bytes of memory.
 
RaptorUK:
"Allocates memory for buffers used for custom indicator calculations. " if you allocate memory for 0 buffers that is 0 bytes of memory.
I see, thanks
well, looks like my interruption of this post didn't help much LOL
 
Agent86:
#property  indicator_buffers 1
I know but I thought 1 means starting at number 0 ?

1 is the count. buffers numbered 0 to count-1

same as double array[10] has 10 elements numbers 0 to 9

Reason: