//+------------------------------------------------------------------+
//|                                           rainbow oscillator.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1  PaleVioletRed
#property indicator_width1  2
#property indicator_minimum -1.1
#property indicator_maximum  1.1

//
//
//
//
//

extern int    RmaPeriod   = 2;
extern int    RmaDepth    = 10;
extern int    RmaPrice    = PRICE_CLOSE;

//
//
//
//
//

#define MAX_depth 50
double  trend[];
double  pBuffer[][MAX_depth];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,trend);
         RmaPeriod         = MathMax(RmaPeriod,2);
         RmaDepth          = MathMax(MathMin(RmaDepth,MAX_depth),5);
   IndicatorShortName("Binary rainbow oscillator ("+RmaPeriod+","+RmaDepth+")");
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define initialMin  EMPTY_VALUE
#define initialMax -EMPTY_VALUE

int start()
{
   int counted_bars=IndicatorCounted();
   int i,k,l,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);
      
   //
   //
   //
   //
   //
      
      for(i=limit, r=Bars-limit-1; i>=0; i--,r++)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,RmaPrice,i);
         double minp  = initialMin;
         double maxp  = initialMax;
         double sum   = 0;

            for (k=0; k<RmaDepth; k++)
            {
               pBuffer[r][k] = price;
               if (r>RmaPeriod)
               {
                  for (l=0, price=0; l<RmaPeriod; l++) price += pBuffer[r-l][k];
                                                       price /= RmaPeriod;
               }
               sum  += price;
               minp = MathMin(minp,pBuffer[r-k][0]);
               maxp = MathMax(maxp,pBuffer[r-k][0]);
            }
            double rma = sum/RmaDepth;
            
            //
            //
            //
            //
            //
            
            trend[i] = trend[i+1];
            double rangep = maxp-minp;
            if (rangep !=0)
            {
               double rangeo = 100*(pBuffer[r][0]-rma)/rangep;            
                  if (rangeo > 0) trend[i] =  1;
                  if (rangeo < 0) trend[i] = -1;
            }                  
   }
   return(0);
}