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); }
Wow! You guys are great!! Zero divide was a problem...I’m leaning so much here due to your generosity. Thank you
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");
<< why (1) and not (0)
#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.
well, looks like my interruption of this post didn't help much LOL
#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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.