Odd behavior when coloring indicator plots

 

I am attempting to color a MACD histogram indicator. I needed a MACD histogram using all exponential moving averages so I basically took the builtin OsMA indicator and changed the smoothing function to EMA from SMA. I then wanted to plot all values above zero blue and all values below zero red. What I find is that the values that draw on the chart are correct for historical bars but get *skewed* going forward. If I edit the applied indicator on the chart (and change nothing but simply click OK out of it) the indicator magically changes to the correct values up to the currently plotting price bar. I have included the code below. If I remove the if statement to assign the values to the correct indicator buffer and plot the values in the MyMACD[] array, the values plot correctly historically and going forward.

Any insight into what is going on and why this won't work would be greatly appreciated.

(and yes, I should be able to include the last if{} statement inside the last loop but since it wasn't working correctly I tried a separate loop for that determination too)

=======================

#property copyright "Copyright © 2007"
#property link "http://www.yo.com"

#include <WinUser32.mqh>

#import "my.dll"
bool CheckMetaTrader(int acctNumer, int indcatorId);
#import

//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1
//---- indicator parameters
extern int FastEMA=25;
extern int SlowEMA=170;
extern int MACDLength=25;
//---- indicator buffers
double MacdBuffer[];
double MacdAvgBuffer[];
double MyMacd[];
double MacdDiffUp[];
double MacdDiffDn[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
IndicatorDigits(Digits+1);
IndicatorShortName("MY_MACD");

SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);

SetIndexBuffer(0,MacdDiffUp);
SetIndexLabel(0,"NP_MACDUp");
SetIndexBuffer(1,MacdDiffDn);
SetIndexLabel(1,"NP_MACDDn");

ArraySetAsSeries(MyMacd, true);
ArraySetAsSeries(MacdBuffer, true);
ArraySetAsSeries(MacdAvgBuffer, true);

ArrayResize(MyMacd, Bars);
ArrayResize(MacdBuffer, Bars);
ArrayResize(MacdAvgBuffer, Bars);

//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

for(int i=0; i<limit; i++)
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
for(i=0; i<limit; i++)
MacdAvgBuffer[i]=iMAOnArray(MacdBuffer,Bars,MACDLength,0,MODE_EMA,i);
for(i=0; i<limit; i++)
MyMacd[i]=MacdBuffer[i]-MacdAvgBuffer[i];

// Determine color for plots (Blue above zero / Red below zero)
for(i=1; i<limit; i++) {
if (MyMacd[i] >= 0) {
MacdDiffDn[i] = NULL;
MacdDiffUp[i] = MyMacd[i];
}
else {
MacdDiffUp[i] = NULL;
MacdDiffDn[i] = MyMacd[i];
}
}

return(0);
}
//+------------------------------------------------------------------+