//+------------------------------------------------------------------+
//|                                                          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 clrGreen
#property indicator_color2 clrRed
#property indicator_color3 clrDimGray
#property indicator_color4 clrLime
#property indicator_color5 clrGold
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_style5 STYLE_DOT
//#property strict

//
//
//
//
//

extern int                MALength                   = 2;
extern int                Depth                      = 10;
extern int                SignalsSmooth              = 30;
extern int                Smooth                     = 80;
extern ENUM_APPLIED_PRICE Price                      = PRICE_CLOSE;
extern bool               ShowRMO                    = true;
extern bool               ShowSignals                = true;
extern bool               alertsOn                   = false;            // Turn alerts on?
extern bool               alertsOnCurrent            = false;            // Alerts on still opened bar?
extern bool               alertsMessage              = true;             // Alerts should display message?
extern bool               alertsOnSignalCross        = true;             // Alerts on slope
extern bool               alertsOnZeroCross          = true;             // Alerts on zero cross
extern bool               alertsSound                = false;            // Alerts should play a sound?
extern bool               alertsNotify               = false;            // Alerts should send a notification?
extern bool               alertsEmail                = false;            // Alerts should send an email?
extern string             soundFile                  = "alert2.wav";     // Sound file
extern bool               arrowsVisible              = false;            // Show arrows?
extern string             arrowsIdentifier           = "RMO arrows1";    // Arrows ID
extern double             arrowsUpperGap             = 0.5;              // Arrows Upper Gap
extern double             arrowsLowerGap             = 0.5;              // Arrows lower gap
extern bool               arrowsOnZeroCross          = true;             // Show arrows on zero cross?
extern color              arrowsOnZeroCrossUpColor   = clrLimeGreen;     // Zero cross Up arrow color
extern color              arrowsOnZeroCrossDnColor   = clrRed;           // Zero cross Down arrows color
extern int                arrowsOnZeroCrossUpCode    = 241;              // Zero cross Up arrow code
extern int                arrowsOnZeroCrossDnCode    = 242;              // Zero cross Down arrow code
extern int                arrowsOnZeroCrossUpSize    = 2;                // Zero cross Up arrow size
extern int                arrowsOnZeroCrossDnSize    = 2;                // Zero cross Down arrow size
extern bool               arrowsOnSignalCross        = false;            // Show arrows on slope change?      
extern color              arrowsOnSignalCrossUpColor = clrLimeGreen;     // Slope change Up arrow color
extern color              arrowsOnSignalCrossDnColor = clrRed;           // Slope change Down arrows color
extern int                arrowsOnSignalCrossUpCode  = 159;              // Slope change Up arrow code
extern int                arrowsOnSignalCrossDnCode  = 159;              // Slope change Down arrow code
extern int                arrowsOnSignalCrossUpSize  = 2;                // Slope change Up arrow size
extern int                arrowsOnSignalCrossDnSize  = 2;                // Slope change Down arrow size  

//
//
//
//
//

#define MAX_depth 30
double  buffer1[];
double  buffer2[];
double  buffer3[];
double  buffer4[];
double  buffer5[];
double  buffer6[];
double  buffer7[];
double  trend1[];
double  trend2[];
double  pBuffer[][MAX_depth];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(9);
      SetIndexBuffer(0,buffer1);
      SetIndexBuffer(1,buffer2);
      SetIndexBuffer(2,buffer3);
      SetIndexBuffer(3,buffer4);
      SetIndexBuffer(4,buffer5);
      SetIndexBuffer(5,buffer6);
      SetIndexBuffer(6,buffer7);
      SetIndexBuffer(7,trend1);
      SetIndexBuffer(8,trend2);
      
      //
      //
      //
      //
      //

      if (!ShowSignals) ShowRMO = true;      
      if (ShowRMO)
      {
         SetIndexStyle(0,DRAW_HISTOGRAM);
         SetIndexStyle(1,DRAW_HISTOGRAM);
         SetIndexStyle(2,DRAW_LINE);
      }
      else
      {
         SetIndexStyle(0,DRAW_NONE);
         SetIndexStyle(1,DRAW_NONE);
         SetIndexStyle(2,DRAW_NONE);
      }
      if (ShowSignals)
      {
         SetIndexStyle(3,DRAW_LINE);
         SetIndexStyle(4,DRAW_LINE);
      }
      else
      {
         SetIndexStyle(3,DRAW_NONE);
         SetIndexStyle(4,DRAW_NONE);
      }
      
      //
      //
      //
      //
      //
      
         MALength = MathMax(MALength,2);
         Depth    = MathMax(MathMin(Depth,MAX_depth),2);
   IndicatorShortName("rmo ("+(string)MALength+","+(string)Depth+","+(string)Smooth+")");
   return(0);
}
int deinit()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
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]);
      trend1[i] = (i<Bars-1) ? (buffer3[i]>0)         ? 1 : (buffer3[i]<0)          ? -1 :  trend1[i+1] : 0;//zero
      trend2[i] = (i<Bars-1) ? (buffer4[i]>buffer5[i])? 1 : (buffer4[i]<buffer5[i]) ? -1 :  trend2[i+1] : 0;//sig
      buffer1[i] = (trend1[i] == 1) ? buffer3[i] : EMPTY_VALUE;
      buffer2[i] = (trend1[i] ==-1) ? buffer3[i] : EMPTY_VALUE;
      
      //
      //
      //
      //
      //
         
      if (arrowsVisible)
      {
         ObjectDelete(arrowsIdentifier+":1:"+(string)Time[i]);
         ObjectDelete(arrowsIdentifier+":2:"+(string)Time[i]);
         string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);
         if (i<(Bars-1) && arrowsOnZeroCross && trend1[i] != trend1[i+1])
         {
            if (trend1[i] == 1) drawArrow("1",0.5,i,arrowsOnZeroCrossUpColor,arrowsOnZeroCrossUpCode,arrowsOnZeroCrossUpSize,false);
            if (trend1[i] ==-1) drawArrow("1",0.5,i,arrowsOnZeroCrossDnColor,arrowsOnZeroCrossDnCode,arrowsOnZeroCrossDnSize,true);
          }
          if (i<(Bars-1) && arrowsOnSignalCross && trend2[i] != trend2[i+1])
          {
             if (trend2[i] == 1) drawArrow("2",1,i,arrowsOnSignalCrossUpColor,arrowsOnSignalCrossUpCode,arrowsOnSignalCrossUpSize,false);
             if (trend2[i] ==-1) drawArrow("2",1,i,arrowsOnSignalCrossDnColor,arrowsOnSignalCrossDnCode,arrowsOnSignalCrossDnSize,true);
           }
             
              
        }
   }

   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0; 
      static datetime time1 = 0;
      static string   mess1 = "";
      if (alertsOnZeroCross && trend1[whichBar] != trend1[whichBar+1])
      {
         if (trend1[whichBar] == 1) doAlert(time1,mess1,whichBar," crossing zero up");
         if (trend1[whichBar] ==-1) doAlert(time1,mess1,whichBar," crossing zero down");
      }         
      static datetime time2 = 0;
      static string   mess2 = "";
      if (alertsOnSignalCross && trend2[whichBar] != trend2[whichBar+1])
      {
         if (trend2[whichBar] ==  1) doAlert(time2,mess2,whichBar," crossing signal up");
         if (trend2[whichBar] == -1) doAlert(time2,mess2,whichBar," crossing signal down");
      }         
      
    }         
return(0);
}

//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, int forBar, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];
       
       //
       //
       //
       //
       //

       message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" RMO "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(_Symbol+" RMO ",message);
          if (alertsSound)   PlaySound(soundFile);
      }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void drawArrow(string nameAdd, double gapMul, int i,color theColor, int theCode, int theWidth, bool up)
{
   string name = arrowsIdentifier+":"+nameAdd+":"+(string)Time[i];
   double gap  = iATR(NULL,0,20,i)*gapMul;   
   
      //
      //
      //
      //
      //
      
      //int add = 0; if (!arrowsOnNewest) add = _Period*60-1;
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_WIDTH,theWidth);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

