//+------------------------------------------------------------------+
//|                                                 A Hanafy_mod.mq4 |
//|                                                              A H |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "A H"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_width1 1
#property indicator_color2 Magenta
#property indicator_width2 1
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
extern int RSI         =14;
extern double PSAR_1   =0.02; 
extern double PSAR_2   =0.2;
extern int Dist        =50;  
extern bool AllSig = false;
extern string FontName="Arial Narrow";
extern int FontSize=14;
extern color FontColor=Gold;
extern int Corner=0;// values [0-3] - опорный угол
extern int XDistance=250;// x расстояние от опорного угла
extern int YDistance=0;// y расстояние от опорного угла     

extern bool AlertsMessage = true; 
extern bool AlertsSound   = false;
extern bool AlertsEmail   = false;
extern bool AlertsMobile  = false;
extern int  SignalBar     = 0;

datetime TimeBar;
int lenbase;
string s_base=":...:...:...:...:";// строка для формирования индикатора с ползунком 
double trend[];

int init()
  {
//---- indicators
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_ARROW,0,1);
   SetIndexArrow(0,241);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW,0,1);
   SetIndexArrow(1,242);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexBuffer(2,trend);
//----
   lenbase=StringLen(s_base);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
  if (ObjectFind("BarTimer") != -1) ObjectDelete("BarTimer");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   if (counted_bars<0) return(-1); 

//---- last counted bar will be recounted 

if (counted_bars>0) counted_bars--; 

int pos=Bars-counted_bars; 

while(pos>=0)
 {
   int i=0,sec=0;
   double pc=0.0;
   string time="",s_end="",s_beg="";
   if (ObjectFind("BarTimer") == -1) {// если объект не найден - создаем и задаем параметры
     ObjectCreate("BarTimer" , OBJ_LABEL,0,0,0);
     ObjectSet("BarTimer", OBJPROP_XDISTANCE, XDistance);
     ObjectSet("BarTimer", OBJPROP_YDISTANCE, YDistance);
     ObjectSet("BarTimer", OBJPROP_CORNER, Corner);
   }

 {  
  
  if(!AllSig) trend[pos] = trend[pos+1]; else trend[pos] = 0;
 //buy 
  if((iRSI(NULL,0,RSI,PRICE_CLOSE,pos)>=30 &&iSAR(NULL,0,PSAR_1,PSAR_2,pos)<Low[pos])
  && iRSI(NULL,0,RSI,PRICE_CLOSE,pos)>iRSI(NULL,0,RSI,PRICE_CLOSE,pos+1)
  && iRSI(NULL,0,RSI,PRICE_CLOSE,pos)>iRSI(NULL,0,RSI,PRICE_CLOSE,pos+2))
  {
    trend[pos] = 1;
  } 
  //sell

  if((iRSI(NULL,0,RSI,PRICE_CLOSE,pos)<=70 &&iSAR(NULL,0,PSAR_1,PSAR_2,pos)> High[pos])
  && iRSI(NULL,0,RSI,PRICE_CLOSE,pos)<iRSI(NULL,0,RSI,PRICE_CLOSE,pos+1)
  && iRSI(NULL,0,RSI,PRICE_CLOSE,pos)<iRSI(NULL,0,RSI,PRICE_CLOSE,pos+2))
  {
    trend[pos] = -1;
  } 
  
  ExtMapBuffer1[pos]=0;
  ExtMapBuffer2[pos]=0;
  
  if((trend[pos]!=trend[pos+1] || AllSig) && trend[pos]==1)  ExtMapBuffer1[pos]=Low[pos]- Dist*Point;
  if((trend[pos]!=trend[pos+1] || AllSig) && trend[pos]==-1) ExtMapBuffer2[pos]=High[pos]+Dist*Point;

    pos--; 
  }
//---
 if(AlertsMessage || AlertsSound || AlertsEmail || AlertsMobile)
  { 
   string message1 = (WindowExpertName()+" - "+Symbol()+"  "+PeriodString()+" - Signal Up");
   string message2 = (WindowExpertName()+" - "+Symbol()+"  "+PeriodString()+" - Signal Dn");
       
    if(TimeBar!=Time[0] && ExtMapBuffer1[SignalBar]!=0 && ExtMapBuffer1[SignalBar]!=EMPTY_VALUE)
     { 
        if (AlertsMessage) Alert(message1);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message1);
        if (AlertsMobile)  SendNotification(message1);
        TimeBar=Time[0];
     }
    if(TimeBar!=Time[0] && ExtMapBuffer2[SignalBar]!=0 && ExtMapBuffer2[SignalBar]!=EMPTY_VALUE)
     { 
        if (AlertsMessage) Alert(message2);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message2);
        if (AlertsMobile)  SendNotification(message2);
        TimeBar=Time[0];
    }
  }
//----
   sec=TimeCurrent()-Time[0];// время в секундах от начала бара
   i=(lenbase-1)*sec/(Period()*60);// позиция ползунка
   pc=100.0*sec/(Period()*60);// время от начала бара в процентах
   if(i>lenbase-1) i=lenbase-1;// возможно излишний контроль границы
   if(i>0) s_beg=StringSubstr(s_base,0,i);
   if(i<lenbase-1) s_end=StringSubstr(s_base,i+1,lenbase-i-1);
   time=StringConcatenate(s_beg,"|",s_end,"  ",DoubleToStr(pc,0),"%");
   ObjectSetText("BarTimer", time, FontSize, FontName, FontColor);
//----
  }
 return(0);
}
//+------------------------------------------------------------------+
//| Period String                                                    |
//+------------------------------------------------------------------+
string PeriodString()
  {
    switch (_Period) 
     {
        case PERIOD_M1:  return("M1");
        case PERIOD_M5:  return("M5");
        case PERIOD_M15: return("M15");
        case PERIOD_M30: return("M30");
        case PERIOD_H1:  return("H1");
        case PERIOD_H4:  return("H4");
        case PERIOD_D1:  return("D1");
        case PERIOD_W1:  return("W1");
        case PERIOD_MN1: return("MN1");
        default: return("M"+(string)_Period);
     }  
    return("M"+(string)_Period); 
  }
//+------------------------------------------------------------------+