//------------------------------------------------------------------
//------------------------------------------------------------------
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     Green
#property indicator_color2     Red
#property indicator_width1     2
#property indicator_width2     2
#property indicator_level1     0

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern double BoxSize         = 15;

extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double Hi[];
double Lo[];
double no[];
double kr[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

//
//
//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

int init()
{
      
   IndicatorBuffers(5);   
      SetIndexBuffer(0,kr); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexLabel(0,"XO Up");
      SetIndexBuffer(1,no); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"XO Down");
      SetIndexBuffer(2,Hi);
      SetIndexBuffer(3,Lo);
      SetIndexBuffer(4,trend);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="CalculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);

      //
      //
      //
      //
      //
      
   IndicatorShortName(timeFrameToString(timeFrame)+" I-XO-A-H ("+DoubleToStr(BoxSize,2)+")");
   return(0);
}

//
//
//
//
//

int deinit(){ return(0); }

//
//
//------------------------------------------------------------------
//
//------------------------------------------------------------------

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { kr[0] = limit+1; return(0); }
         
   //
   //
   //
   //
   //
    
   if (calculateValue || timeFrame == Period())
   {
      double pipMultiplier = 1; if (Digits==3 || Digits==5) pipMultiplier=10;
      for(int i=limit; i>=0; i--)
      {         
        if (i>=(Bars-2)) { Hi[i+1]=Close[i]; Lo[i+1]=Close[i]; continue; }
           double cur      = Close[i];
                  Hi[i]    = Hi[i+1];
                  Lo[i]    = Lo[i+1];
                  no[i]    = no[i+1];
                  kr[i]    = kr[i+1];
                  trend[i] = trend[i+1];

                  if (cur > (Hi[i]+BoxSize*Point*pipMultiplier)) 
                  {
                     Hi[i]    = cur;
                     Lo[i]    = cur-BoxSize*Point*pipMultiplier;
                     kr[i]    = kr[i+1]+1;
                     no[i]    = 0;
                }
                if (cur < (Lo[i]-BoxSize*Point*pipMultiplier)) 
                {
                     Lo[i]    = cur;
                     Hi[i]    = cur+BoxSize*Point*pipMultiplier;
                     no[i]    = no[i+1]-1;
                     kr[i]    = 0;
                }
                if (kr[i] > 0) trend[i]= 1;
                if (no[i] < 0) trend[i]=-1;
      }
      manageAlerts();
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit;i>=0;i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         kr[i]    = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",BoxSize,0,y);
         no[i]    = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",BoxSize,1,y);
         trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"CalculateValue",BoxSize,4,y);
   }
   manageAlerts();
   return(0);         
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && 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,"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(timeFrameToString(timeFrame)+" "+Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," I_XO_A_H trend changed to ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"I_XO_A_H"),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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 tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}               
                