#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 White
#property indicator_color4 White
#property indicator_color5 Gold
#property indicator_color6 Magenta
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 2
#property indicator_width6 2

extern int p = 10;
extern int s = 5;
extern int cb = 1000;
extern int ATR = 1000;
extern double distance = 2.0;
extern int arrots = 15;
extern int barsig = 1;
input bool               alertsOn        = false;        // Turn alerts on/off?
input bool               alertsOnCurrent = true;         // Alerts on still opened bar on/off?
input bool               alertsMessage   = true;         // Alerts message on/off?
input bool               alertsSound     = false;        // Alerts sound on/off?
input bool               alertsPushNotif = false;        // Alerts push notification on/off?
input bool               alertsEmail     = false;        // Alerts email on/off?
input string             soundFile       = "alert2.wav"; // Sound file

double fx1[],fx2[],hp[],trend[];
double z1,z2,ki;
int fs;

double upper[],lower[];
double upar[],dnar[];

int init()
{
IndicatorBuffers(8);
SetIndexBuffer(0,fx1);
SetIndexBuffer(1,fx2);
SetIndexBuffer(2,lower);
SetIndexBuffer(3,upper);

SetIndexBuffer(4,upar);
SetIndexStyle (4,DRAW_ARROW);
SetIndexArrow (4,233);
SetIndexBuffer(5,dnar);
SetIndexStyle (5,DRAW_ARROW);
SetIndexArrow (5,234);

SetIndexBuffer(6,hp);
SetIndexBuffer(7,trend);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);

ki=2.0/(p+1);

return(0);
}

int start()
{
SetIndexDrawBegin(0,Bars-cb);
SetIndexDrawBegin(1,Bars-cb);

double avg;

for (int i=cb; i>=0; i--) {fx1[i]=Close[i];}

for (int m=0; m<=s; m++)
{
z1=fx1[0];
for (i=0; i<=cb; i++) {z1=z1+(fx1[i]-z1)*ki; hp[i]=z1;}

z2=fx1[cb];
for (i=cb; i>=0; i--) {z2=z2+(fx1[i]-z2)*ki; fx1[i]=(hp[i]+z2)/2;}
}

fs=0;
for (i=cb; i>=0; i--)
{
if (fx1[i]>fx1[i+1]) fs=1;
if (fx1[i]<fx1[i+1]) {if (fs==1) fx2[i+1]=fx1[i+1]; fs=2;}
if (fs==2) fx2[i]=fx1[i]; else fx2[i]=0.0;

avg = iATR(NULL,0,ATR, i+10);
upper[i] = hp[i] + distance*avg;
lower[i] = hp[i] - distance*avg;

trend[i] = 0;
upar[i] = EMPTY_VALUE;
dnar[i] = EMPTY_VALUE;
if(Close[i+1+barsig]<upper[i+1+barsig] && Close[i+barsig]>upper[i+barsig]) trend[i] =-1;
if(Close[i+1+barsig]>lower[i+1+barsig] && Close[i+barsig]<lower[i+barsig]) trend[i] = 1; 
 if (trend[i]!=trend[i+1])
 {
    if (trend[i] ==  1) upar[i] =  Low[i]-arrots*Point;
    if (trend[i] == -1) dnar[i] = High[i]+arrots*Point;
 }
 
}
if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0; 
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(" up");
         if (trend[whichBar] ==-1) doAlert(" down");       
       }         
    }       
return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" sniper "+doWhat;
             if (alertsMessage)    Alert(message);
             if (alertsPushNotif)  SendNotification(message);
             if (alertsEmail)      SendMail(_Symbol+" sniper ",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};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
