//+------------------------------------------------------------------+
//|                                    Sidus v.3 Entry Indicator.mq4 |
//|                                                                  |
//|                                                   Ideas by Sidus |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Aqua
#property indicator_color2 Yellow
#property indicator_color3 Aqua
#property indicator_color4 Yellow
#property indicator_width1 1  
#property indicator_width2 1   
#property indicator_width3 2  
#property indicator_width4 2   

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    FastEMA          = 5;
extern int    SlowEMA          = 8;
extern string ex               = "0=SMA,1=EMA,2=SSMA,3=LWMA";
extern int    mamode           = MODE_LWMA;
extern string ex2              = "0=Close,1=Open,2=High,3=Low";
extern string ex2a             = "4=Median,5=Typical,6=Weighted";
extern int    maprice          = PRICE_CLOSE;
extern int    RSIPeriod        = 14;

extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = false;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsEmail      = false;
extern string soundfile        = "alert2.wav";
//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double trend[];
string indicatorFileName;
bool   returnBars;
int    timeFrame;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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);
 
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
  
   return(0);
}
int deinit() 
{

     Comment("");
     
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);
         if (returnBars) { ExtMapBuffer1[0] = MathMin(limit+1,Bars-1); return(0); }
         if (timeFrame!=Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));

   //
   //
   //
   //
   //
   
   for(int i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      int x = iBarShift(NULL,timeFrame,Time[i+1]);
      ExtMapBuffer1[i] = iMA(NULL,timeFrame,FastEMA,0,mamode,maprice,y);
      ExtMapBuffer2[i] = iMA(NULL,timeFrame,SlowEMA,0,mamode,maprice,y);
      ExtMapBuffer3[i] = EMPTY_VALUE;
      ExtMapBuffer4[i] = EMPTY_VALUE;
      trend[i]         = trend[i+1];

      //
      //
      //
      //
      //
      
      double dist = iATR(NULL,timeFrame,20,y);
      double rsi  = iRSI(NULL,timeFrame,RSIPeriod, PRICE_CLOSE, y);
      double diff = (ExtMapBuffer1[i]-ExtMapBuffer2[i]);
      Comment("pipdiffCurrent = " + diff + " ");

         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;  
   }
   if (alertsOn)
   {
     if (alertsOnCurrent)
           int whichBar = 0;
     else      whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
     

     //
     //
     //
     //
     //
         
     if (trend[whichBar] != trend[whichBar+1])
     if (trend[whichBar] == 1)
           doAlert(whichBar,"uptrend");
     else  doAlert(whichBar,"downtrend");       
   }
   
   return(0);
}
//+------------------------------------------------------------------+


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(timeFrameToString(timeFrame)," ",Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," sidus ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," sidus "),message);
             if (alertsSound)   PlaySound(soundfile);
      }
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

        