//+------------------------------------------------------------------+
//|                                RSI-MA-BUY-SELL-SIGNAL-WALERT.mq4 |
//+------------------------------------------------------------------+

#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 4
#property indicator_color1 clrWhite
#property indicator_color2 clrGold
#property indicator_color3 clrWhite
#property indicator_width3 2
#property indicator_color4 clrWhite
#property indicator_width4 2

extern int FastEMA = 14;
extern int SlowEMA = 21;
extern int RSIPeriod = 9;
extern bool AlertsOn = true;

double fema_buff[];
double sema_buff[];
double uparrow_buff[];
double downarrow_buff[];

int dir = 0;
int newdir = 0;
double EMADiff = 0;
double newEMADiff = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
//--- indicator buffers mapping
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, fema_buff);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, sema_buff);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexStyle(2, DRAW_ARROW);
   SetIndexArrow(2, 233);
   SetIndexBuffer(2, uparrow_buff);
   SetIndexEmptyValue(2, EMPTY_VALUE);
   SetIndexStyle(3, DRAW_ARROW);
   SetIndexArrow(3, 234);
   SetIndexBuffer(3, downarrow_buff);
   SetIndexEmptyValue(3, EMPTY_VALUE);
//---
   return (0);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   return (0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int countbars = IndicatorCounted();
   double irsi = 0;
   bool MessageAlert = false;
   double price_point = 0;

   if(countbars < 0)
      return(-1);
   if(countbars > 0)
      countbars--;

   int newbar = Bars - countbars;

   for(int i = 0; i < newbar; i++)
     {
      fema_buff[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      sema_buff[i] = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      irsi = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, i);
      EMADiff = (fema_buff[i] - sema_buff[i]);
      Comment("EMA Points Difference: " + DoubleToString(EMADiff/_Point,0));

      if(EMADiff > 0 && irsi > 50)
         dir = 1;
      else
         if(EMADiff < 0 && irsi < 50)
            dir = 2;

      if(dir == 1 && newdir == 2 && newEMADiff > 0)
        {
         downarrow_buff[i] = High[i]+5*Point;
         MessageAlert = true;
         price_point = Ask;
        }
      else
        {
         if(dir == 2 && newdir == 1 && newEMADiff < 0)
           {
            uparrow_buff[i] = Low[i]-5*Point;
            MessageAlert = true;
            price_point = Bid;
           }
        }
      newdir = dir;
      newEMADiff = EMADiff;
     }

   if(AlertsOn && MessageAlert)
     {
      PlaySound("alert.wav");
      if(newdir == 1)
         MessageBox("Buy @: " + (string)price_point + "", "Entry Point", 0);
      else
         if(newdir == 2)
            MessageBox("Sell @ : " + (string)price_point + "", "Entry Point", 0);
      MessageAlert = false;
     }

   return(0);

  }

//+------------------------------------------------------------------+
