//+------------------------------------------------------------------+
//|                                RSI-MA-BUY-SELL-SIGNAL-WALERT.mq4 |
//+------------------------------------------------------------------+

#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 2
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_color2 clrRed
#property indicator_width2 2

enum enMode
  {
   DIR=0,        // Directional filter
   EMA=1,        // EMA Diff filter
  };
  
extern enMode FilterMode = EMA;
extern int FastEMA = 14;
extern int SlowEMA = 21;
extern int RSIPeriod = 9;
extern bool AlertsOn = false;

input int                 Button_Window              = 1;  
input ENUM_BASE_CORNER    Button_Corner              = 3;  
input int                 Button_X                   = 522;
input int                 Button_Y                   = 207;
input int                 Button_Width               = 99;
input int                 Button_Height              = 36;
input int                 Button_FontSize            = 11;
input color               Button_BrdrColor           = clrChocolate;
input color               Button_OffBckColor         = C'40,40,40';
input color               Button_OnBckColor          = clrGreen;
input color               Button_OffTxtColor         = clrRed;
input color               Button_OnTxtColor          = clrChocolate;

string ButTxtOn = "Signals On";
string ButTxtOff = "Signals Off";

string Indi_ID="RSIMABSWA_";
string ONOFButtName="RSIMASig_Button";

double fema_buff;
double sema_buff;
double uparrow_buff[];
double downarrow_buff[];

int dir = 0;
int newdir = 0;
double EMADiff = 0;
double newEMADiff = 0;

//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
 {
//---
   if(id==CHARTEVENT_OBJECT_CLICK)
     { 
     if(sparam==ONOFButtName&&
        ObjectGetString(0,sparam,OBJPROP_TEXT)==ButTxtOff)
      {
      ObjectSetString(0,sparam,OBJPROP_TEXT,ButTxtOn);
      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,Button_OnBckColor);
      ObjectSetInteger(0,sparam,OBJPROP_COLOR,Button_OnTxtColor);
      ObjectSetInteger(0,sparam,OBJPROP_STATE,true);
      SetIndexStyle(0, DRAW_ARROW);
      SetIndexStyle(1, DRAW_ARROW);
      MainCode();
      }
     else
     if(sparam==ONOFButtName&&
        ObjectGetString(0,sparam,OBJPROP_TEXT)==ButTxtOn)
      {    
      ObjectSetString(0,sparam,OBJPROP_TEXT,ButTxtOff);  
      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,Button_OffBckColor);
      ObjectSetInteger(0,sparam,OBJPROP_COLOR,Button_OffTxtColor);
      ObjectSetInteger(0,sparam,OBJPROP_STATE,true);
      SetIndexStyle(0, DRAW_NONE);
      SetIndexStyle(1, DRAW_NONE);
      }
     }

 }
//+------------------------------------------------------------------+
int init()
  {
   CreateButton(ONOFButtName);
//--- indicator buffers mapping
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 104);
   SetIndexBuffer(0, uparrow_buff);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 104);
   SetIndexBuffer(1, downarrow_buff);
   SetIndexEmptyValue(1, EMPTY_VALUE);
//---
   return (0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete(0,ONOFButtName);
   Comment("");
   return (0);
  }
//+------------------------------------------------------------------+
int start()
  {
   if(ObjectGetString(0,ONOFButtName,OBJPROP_TEXT)==ButTxtOff)
    {
      SetIndexStyle(0, DRAW_NONE);
      SetIndexStyle(1, DRAW_NONE);
    }
   MainCode();
   
   return(0);

  }

//+------------------------------------------------------------------+
void CreateButton(string btn_name)
   {
                   if(ObjectFind(0,btn_name)<0)
                    {
                   ObjectCreate(0,btn_name,OBJ_BUTTON,Button_Window,0,0);
                   ObjectSetInteger(0,btn_name,OBJPROP_XDISTANCE,Button_X);
                   ObjectSetInteger(0,btn_name,OBJPROP_YDISTANCE,Button_Y);
                   ObjectSetInteger(0,btn_name,OBJPROP_XSIZE,Button_Width);
                   ObjectSetInteger(0,btn_name,OBJPROP_YSIZE,Button_Height);
                   ObjectSetInteger(0,btn_name,OBJPROP_CORNER,Button_Corner);
                   ObjectSetString(0,btn_name,OBJPROP_TEXT,ButTxtOff);
                   ObjectSetString(0,btn_name,OBJPROP_FONT,"Tahoma");
                   ObjectSetInteger(0,btn_name,OBJPROP_FONTSIZE,Button_FontSize);
                   ObjectSetInteger(0,btn_name,OBJPROP_COLOR,Button_OffTxtColor);
                   ObjectSetInteger(0,btn_name,OBJPROP_BGCOLOR,Button_OffBckColor);
                   ObjectSetInteger(0,btn_name,OBJPROP_BORDER_COLOR,Button_BrdrColor);
                   ObjectSetInteger(0,btn_name,OBJPROP_BACK,false);
                   ObjectSetInteger(0,btn_name,OBJPROP_STATE,true);
                   ObjectSetInteger(0,btn_name,OBJPROP_SELECTABLE,false);
                   ObjectSetInteger(0,btn_name,OBJPROP_SELECTED,false);
                   ObjectSetInteger(0,btn_name,OBJPROP_HIDDEN,true);
                   ObjectSetInteger(0,btn_name,OBJPROP_ZORDER,9);
                    }
   }
//+------------------------------------------------------------------+
void MainCode()
 {
   int countbars = IndicatorCounted();
   double irsi = 0;
   bool MessageAlert = false;
   double price_point = 0;

   if(countbars < 0)
      return;
   if(countbars > 0)
      countbars--;

   int newbar = Bars - countbars;
   for(int i = 0; i < newbar; i++)
     {
      downarrow_buff[i]=EMPTY_VALUE;
      uparrow_buff[i]=EMPTY_VALUE;
      
      fema_buff = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      sema_buff = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      irsi = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, i);
      EMADiff = (fema_buff - sema_buff);
      Comment("EMA Points Difference: " + DoubleToString(EMADiff/_Point,0));

      if(EMADiff > 0 && irsi > 50)
         dir = 1;
      else
         if(EMADiff < 0 && irsi < 50)
            dir = 2;
      if(FilterMode==EMA)
       {
      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;
           }
        }
       } 
      else
      if(FilterMode==DIR)
       {
      if(dir == 1 && newdir == 2)
        {
         downarrow_buff[i] = High[i]+5*Point;
         MessageAlert = true;
         price_point = Ask;
        }
      else
        {
         if(dir == 2 && newdir == 1)
           {
            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;
     }
 }
//+------------------------------------------------------------------+
