//+------------------------------------------------------------------+
//|                                    Sidus v.3 Entry Indicator.mq4 |
//|                                                                  |
//|                                                   Ideas by Sidus |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Aqua
#property indicator_color4 Yellow
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3  
#property indicator_width4 3   

//
//
//
//
//

extern int  FastEMA          =15;//14
extern int  SlowEMA          =34;//21
extern int  RSIPeriod        = 8;//17
extern bool alertsOn         = false;
extern bool alertsOnCurrent  = false;
extern bool alertsMessage    = true;
extern bool alertsSound      = false;
extern bool alertsEmail      = false;
extern bool alertsPush       = false;

//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3); SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,233);
   SetIndexBuffer(3,ExtMapBuffer4); SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,234);
   SetIndexBuffer(4,trend);
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int limit;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   for(int i=limit; i>=0; i--)
   {
      ExtMapBuffer1[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i);
      ExtMapBuffer2[i] = iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
      ExtMapBuffer3[i] = EMPTY_VALUE;
      ExtMapBuffer4[i] = EMPTY_VALUE;
      trend[i]         = trend[i+1];

      //
      //
      //
      //
      //
      
      double dist = iATR(NULL,0,20,i);
      double rsi  = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, i);
      double diff = (ExtMapBuffer1[i]-ExtMapBuffer2[i]);

         if (diff>0 && rsi>50) trend[i] =  1;
         if (diff<0 && rsi<50) trend[i] = -1;
         if (trend[i]!=trend[i+1])
            if (trend[i]==1)
                  ExtMapBuffer3[i] = ExtMapBuffer1[i]-dist;
            else  ExtMapBuffer4[i] = ExtMapBuffer1[i]+dist;
   }
   manageAlerts();
   return(0);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] ==  1) doAlert(whichBar,"up");
         if (trend[whichBar] == -1) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," ",timeFrameToString(Period())+" sidus 3 trend changed to ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"sidus 3 "),message);
          if (alertsSound)   PlaySound("alert2.wav");
          if (alertsPush)    SendNotification(StringConcatenate(Symbol(),"sidus 3 ")+message);
   }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}