Very weird issue with custom indicator.

 

I made an indicator derived from the MACD custom indicator that comes with metatrader, the code is attached. It basically just takes the MACD data and marks a buy or sell point with color coded arrows where the signal line reverses.


Here's my problem. When I start the indicator, this is what comes up:




If i open the indicator up and compile it while its running in the chart, it corrects itself to this:







So what is the deal here?

//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 4
#property  indicator_color4  Red
#property  indicator_color3  LightBlue
#property  indicator_width4  1
#property  indicator_width3  1
 
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
 
double     MacdBuffer[];
double     SignalBuffer[];
double     BuyAlert[];
double     SellAlert[];
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,241);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,242);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer);
   SetIndexBuffer(1,SignalBuffer);
   SetIndexBuffer(2,BuyAlert); 
   SetIndexBuffer(3,SellAlert);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
   SetIndexLabel(2,"BuyAlert"); 
   SetIndexLabel(3,"SellAlert");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   double BAlert;
   double SAlert;
   
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   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);
      }
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      {
      SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
      BAlert=0;
      SAlert=0;
      if (SignalBuffer[i+2]<=SignalBuffer[i+1] && SignalBuffer[i+1]>=SignalBuffer[i]) {SAlert=(Close[i]*1.002);}
      if (SignalBuffer[i+2]>=SignalBuffer[i+1] && SignalBuffer[i+1]<=SignalBuffer[i]) {BAlert=(Close[i]*0.998);}
      BuyAlert[i]=BAlert;
      SellAlert[i]=SAlert;
      }
//---- done
   return(0);
  }
 
 
//+------------------------------------------------------------------+
 
has anyone ever had an issue like this before?
 
Reverse the direction of your loops, and try again.