//+------------------------------------------------------------------+
//|                                                          rmo.mq4 |
//|                                                           mladen |
//| Rahul Mohindar Oscillator                                        |
//| originaly developed by : ????????                                |
//+------------------------------------------------------------------+
#property copyright "copyleft mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 DimGray
#property indicator_color4 Lime
#property indicator_color5 Gold
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_style5 STYLE_DOT

//
//
//
//
//

extern int  MALength      = 2;
extern int  Depth         = 10;
extern int  SignalsSmooth = 30;
extern int  Smooth        = 81;
extern int  Price         = PRICE_CLOSE;
extern bool ShowRMO       = true;
extern bool ShowSignals   = true;

//
//
//
//
//

#define MAX_depth 30
double  buffer1[];
double  buffer2[];
double  buffer3[];
double  buffer4[];
double  buffer5[];
double  buffer6[];
double  buffer7[];
double  trend[];
double  pBuffer[][MAX_depth];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,buffer1);
      SetIndexBuffer(1,buffer2);
      SetIndexBuffer(2,buffer3);
      SetIndexBuffer(3,buffer4);
      SetIndexBuffer(4,buffer5);
      SetIndexBuffer(5,buffer6);
      SetIndexBuffer(6,buffer7);
      SetIndexBuffer(7,trend);
      
      //
      //
      //
      //
      //

      if (!ShowSignals) ShowRMO = true;      
      if (ShowRMO)
      {
         SetIndexStyle(0,DRAW_HISTOGRAM);
         SetIndexStyle(1,DRAW_HISTOGRAM);
         SetIndexStyle(2,DRAW_NONE);
      }
      else
      {
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_NONE);
         SetIndexStyle(2,DRAW_NONE);
      }
      if (ShowSignals)
      {
         SetIndexStyle(3,DRAW_NONE);
         SetIndexStyle(4,DRAW_NONE);
      }
      else
      {
         SetIndexStyle(3,DRAW_NONE);
         SetIndexStyle(4,DRAW_NONE);
      }
      
      //
      //
      //
      //
      //
      
         MALength = MathMax(MALength,2);
         Depth    = MathMax(MathMin(Depth,MAX_depth),2);
   IndicatorShortName("rmo ("+MALength+","+Depth+","+Smooth+")");
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double alpha = 2.0/(Smooth+1.0);
   double alphs = 2.0/(SignalsSmooth+1.0);
   int    counted_bars=IndicatorCounted();
   int    i,l,r;


   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           int 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++)
   {
      buffer7[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
      double high   = buffer7[ArrayMaximum(buffer7,Depth,i)];
      double low    = buffer7[ArrayMinimum(buffer7,Depth,i)];
      double price  = buffer7[i];
      double sum    = 0;
      
      //
      //
      //
      //
      //
      
      for (int k=1; k<=Depth; k++)
      {
         pBuffer[r][k-1] = price;
         if (r>MALength)
         {
            for (l=0, price=0; l<MALength; l++) price += pBuffer[r-l][k-1];
                                                price /= MALength;
         }               
         sum += price;
      }
      
      //
      //
      //
      //
      //
      
      if ((high-low) != 0)
            buffer6[i] = 100*(buffer7[i]-sum/Depth)/(high-low);
      else  buffer6[i] = 0;
      buffer4[i] = buffer4[i+1]+alphs*(buffer6[i]-buffer4[i+1]);
      buffer5[i] = buffer5[i+1]+alphs*(buffer4[i]-buffer5[i+1]);
      buffer3[i] = buffer3[i+1]+alpha*(buffer6[i]-buffer3[i+1]);
      trend[i] = trend[i+1];
      if (buffer3[i]>buffer3[i+1]) trend[i] = 1;
      if (buffer3[i]<buffer3[i+1]) trend[i] =-1;
      if (trend[i]==1)
            buffer1[i] = buffer3[i];
      else  buffer2[i] = buffer3[i];
      
   }
   return(0);
}