Change to a Multi Time Frame indicator

 

I was trying to adapt the divergence formula to a multi time frame indicator, but I'm having some problems to make it works correctly. The formula is below. Any idea where I'm committing a mistake?

Thanks


//+------------------------------------------------------------------+
//|                                    Divergência MultiTimeFrame.mq4|
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright " "
#property link      " "

#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum +1
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//-Parametros
extern int periodo=20;
extern int periodoRSI=20;
extern int TimeFrame=0; // PERIOD_M1=1; PERIOD_M5=5; PERIOD_M15=15; PERIOD_M30=30; PERIOD_H1=60; PERIOD_H4=240; PERIOD_D1=1440; PERIOD_W1=10080; PERIOD_MN1=43200; 0 (zero)=Timeframe used on the chart

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double RSI_Buffer[];
double RSI_Buffer2[];
double a1_Buffer[];
double a2_Buffer[];
double b1_Buffer[];
double b2_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
//--- Internal Variables   

SetIndexBuffer(2,RSI_Buffer); 
SetIndexBuffer(3,a1_Buffer); 
SetIndexBuffer(4,a2_Buffer); 
SetIndexBuffer(5,b1_Buffer); 
SetIndexBuffer(6,b2_Buffer); 
SetIndexBuffer(7,RSI_Buffer2);    
   
//--- Plot a line   
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

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


//---- Current RSI value
      for(int i = limit-1; i>=0; i--)// Count down for one pass 
         RSI_Buffer[i]=iRSI(NULL,TimeFrame,periodoRSI,PRICE_CLOSE,i);
//---- Highest price in 20 periods         
      for(i = limit-1; i>=0; i--)// Count down for one pass 
         a1_Buffer[i]=High[iHighest(NULL,TimeFrame,MODE_HIGH,periodo,i)];
//---- Highest RSI value in 20 periods                  
      for(i = limit-1; i>=0; i--) // Count down for one pass 
         a2_Buffer[i] = RSI_Buffer[ArrayMaximum(RSI_Buffer, periodo, i)];
//---- Lowest price in 20 periods
      for(i = 0; i<limit; i++)   
         b1_Buffer[i]=Low[iLowest(NULL,TimeFrame,MODE_LOW,periodo,i)];
//---- Lowest RSI value in 20 periods
      for(i= limit-1; i>=0; i--)
         b2_Buffer[i] = RSI_Buffer[ArrayMinimum(RSI_Buffer, periodo, i)];
         
//---- Bullish Divergence 
      for(i= limit-1; i>=0; i--)
      {
         if((iClose(NULL,TimeFrame,i)<=b1_Buffer[i] && RSI_Buffer[i]>b2_Buffer[i]) || (RSI_Buffer[i]<=b2_Buffer[i] && iClose(NULL,TimeFrame,i)>b1_Buffer[i]))
            ExtMapBuffer1[i]=-1;
            else
               ExtMapBuffer1[i]=0;
               
      }          
      
//---  Bearish Divergence      
      for(i= limit-1; i>=0; i--)
      {
         if((iClose(NULL,TimeFrame,i)>=a1_Buffer[i] && RSI_Buffer[i]<a2_Buffer[i])|| (RSI_Buffer[i]>=a2_Buffer[i] && iClose(NULL,TimeFrame,i)<a1_Buffer[i]))
            ExtMapBuffer2[i]=1;
            else
               ExtMapBuffer2[i]=0;
      
      }
        
//----
   return(0);
  }
//+------------------------------------------------------------------+
Reason: