//+------------------------------------------------------------------+
//|                                                          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 6
#property indicator_color1 Red
#property indicator_color2 DimGray
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Gold
#property indicator_width2 2
#property indicator_style6 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  buffer8[];
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,buffer8);
      
      
      SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexStyle(3,DRAW_HISTOGRAM);
      //
      //
      //
      //
      //

      if (!ShowSignals) ShowRMO = true;      
      if (ShowRMO)
      {
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_LINE);
      }
      else
      {
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_NONE);
      }
      if (ShowSignals)
      {
         SetIndexStyle(4,DRAW_LINE);
         SetIndexStyle(5,DRAW_LINE);
      }
      else
      {
         SetIndexStyle(4,DRAW_NONE);
         SetIndexStyle(5,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,limit;


   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);

   //
   //
   //
   //
   //
   
   for(i=limit, r=Bars-limit-1; i>=0; i--,r++)
   {
      buffer8[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
      double high   = buffer8[ArrayMaximum(buffer8,Depth,i)];
      double low    = buffer8[ArrayMinimum(buffer8,Depth,i)];
      double price  = buffer8[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)
            buffer7[i] = 100*(buffer8[i]-sum/Depth)/(high-low);
      else  buffer7[i] = 0;
      buffer5[i] = buffer5[i+1]+alphs*(buffer7[i]-buffer5[i+1]);
      buffer6[i] = buffer6[i+1]+alphs*(buffer5[i]-buffer6[i+1]);
      buffer2[i] = buffer2[i+1]+alpha*(buffer7[i]-buffer2[i+1]);
      buffer1[i] = buffer2[i];
      
      if(buffer1[i]>0){buffer3[i]=buffer1[i];buffer4[i]=EMPTY_VALUE;}
      if(buffer1[i]<0){buffer4[i]=buffer1[i];buffer3[i]=EMPTY_VALUE;}
   }
   return(0);
}