Help with this EMA Fan Indicator

 

          Can anyone help me figure out what is wrong with this Indicator that I am trying to write? I tried numerous methods for pulling the EMA moving averages but at this point, I think the problem is somewhere in the main loop of reading the candles and the buffer sizes of the EMA's. I am a heavy java programmer so I thought this would be simple, but it is proving challenging as I spent weeks trying to figure this out before posting for help. I tested the TrendDetector() function and it works as it should. I even tested all the EMA logic and the values that are being read in are correct so I can't figure out why the values in the Histogram are not correct. All it is suppose to do is compare 4 EMA moving averages and show when there is fan up or fan down on the histogram. I would like to eventually use colors for the Bullish/Bearish lines and also add in checking the trend compared to the 200EMA on two higher timeframes, which is what I started to set this up for now, but is not being used yet.  I like trading bounces off any of the EMA's on the shorter timeframe, only when all three things are in alignment. Thanks for any help..

Files:
Fan.mq5  5 kb
 

CopyBuffer() is called in the main function.

void OnInit()
  {
//      Displayed indicator buffer:
   SetIndexBuffer(0,TrendBuffer,INDICATOR_DATA);
   ArraySetAsSeries(TrendBuffer,true);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,EMA200);
   PlotIndexSetString(0,PLOT_LABEL,"FanTrendDetector");
   handle18 = iMA(NULL,PERIOD_CURRENT,EMA18,0,MODE_EMA,PRICE_CLOSE);
   handle50 = iMA(NULL,PERIOD_CURRENT,EMA50,0,MODE_EMA,PRICE_CLOSE);
   handle100 = iMA(NULL,PERIOD_CURRENT,EMA100,0,MODE_EMA,PRICE_CLOSE);
   handle200 = iMA(NULL,PERIOD_CURRENT,EMA200,0,MODE_EMA,PRICE_CLOSE);
   ArraySetAsSeries(Buffer18,true);
   ArraySetAsSeries(Buffer50,true);
   ArraySetAsSeries(Buffer100,true);
   ArraySetAsSeries(Buffer200,true);
   min_rates_total=2;
  }
//---------------------------------------------------------------------
//      Need for indicator recalculation event handler:
//---------------------------------------------------------------------
int OnCalculate(const int rates_total, 
                const int prev_calculated, 
                const datetime &time[], 
                const double &open[], 
                const double &high[], 
                const double &low[], 
                const double &close[], 
                const long &tick_volume[], 
                const long &volume[], 
                const int &spread[])
  {
   int first,bar;

   if(rates_total<=EMA200 || BarsCalculated(handle200)<=EMA200) return(0);
   
   int toCopy=(prev_calculated<=0)?rates_total:rates_total-prev_calculated+1;
   
   if(CopyBuffer(handle18,0,0,toCopy,Buffer18)!=toCopy ||
      CopyBuffer(handle50,0,0,toCopy,Buffer50)!=toCopy ||        //Using BarsCalculated to get around Array out of bounds errors...
      CopyBuffer(handle100,0,0,toCopy,Buffer100)!=toCopy ||
      CopyBuffer(handle200,0,0,toCopy,Buffer200)!=toCopy)
      return(0);
      
   if(prev_calculated>rates_total || prev_calculated<=0)
     {
      first=rates_total-EMA200;
     }
   else first=rates_total-prev_calculated; 

   for(bar=first; bar>=0 && !IsStopped(); bar--)
     {
     TrendBuffer[bar]=TrendDetector(bar);
     }
       
   return(rates_total);
  }

 
Ernst Van Der Merwe:

CopyBuffer() is called in the main function.


Wow, that's all it was. Thanks A lot for your help. it works perfect now   :<)
Reason: