//------------------------------------------------------------------
//------------------------------------------------------------------
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     LimeGreen
#property indicator_color2     PaleVioletRed
#property indicator_width1     2
#property indicator_width2     2
#property indicator_level1     0

//
//
//
//
//

extern double BoxPeriod         = 6.5;
extern bool   alertsOn          = true;
extern bool   alertsOnCurrent   = false;
extern bool   alertsMessage     = true;
extern bool   alertsSound       = false;
extern bool   alertsEmail       = false;
extern bool   ShowLines         = false;
extern string LinesIdentifier   = "XOLines1";
extern color  LinesColorForUp   = LimeGreen;
extern color  LinesColorForDown = Red;
extern int    LinesStyle        = STYLE_DOT;

//
//
//
//
//

double Hi[];
double Lo[];
double no[];
double kr[];
double trend[];

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

int init()
{
   IndicatorDigits(0);      
   IndicatorBuffers(5);   
      SetIndexBuffer(0,kr); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,no); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,Hi);
      SetIndexBuffer(3,Lo);
      SetIndexBuffer(4,trend);
   SetIndexEmptyValue(0,0.00);
   SetIndexEmptyValue(1,0.00);
   SetIndexEmptyValue(2,0.00);
   SetIndexEmptyValue(3,0.00);
   SetIndexLabel(0,"XO Up");
   SetIndexLabel(1,"XO Down");

   //
   //
   //
   //
   //
      
   IndicatorShortName("XO ("+DoubleToStr(BoxPeriod,2)+")");
   return(0);
}


//
//
//
//
//

int deinit()
{
   int lookForLength = StringLen(LinesIdentifier);
   for (int i=ObjectsTotal(); i>=0; i--)
      {
         string name = ObjectName(i);
         if (StringSubstr(name,0,lookForLength)==LinesIdentifier) ObjectDelete(name);
      }
   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);
           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]+BoxPeriod*Point*pipMultiplier)) 
                {
                     Hi[i]    = cur;
                     Lo[i]    = cur-BoxPeriod*Point*pipMultiplier;
                     kr[i]    = kr[i+1]+1;
                     no[i]    = 0;
                     trend[i] = 1;
                }
                if (cur < (Lo[i]-BoxPeriod*Point*pipMultiplier)) 
                {
                     Lo[i]    = cur;
                     Hi[i]    = cur+BoxPeriod*Point*pipMultiplier;
                     no[i]    = no[i+1]-1;
                     kr[i]    = 0;
                     trend[i] = -1;
                }
                if (ShowLines)
                {
                  deleteLine(i);
                     if (trend[i]!=trend[i+1])
                     if (trend[i]==1)
                           drawLine(i,LinesColorForUp);
                     else  drawLine(i,LinesColorForDown);
                }                  
   }
                
   //
   //
   //
   //
   //
       
   if (alertsOn)
   {
         if (alertsOnCurrent)
              int whichBar = 0;
         else     whichBar = 1;

         //
         //
         //
         //
         //
         
         if (trend[whichBar] != trend[whichBar+1])
         if (trend[whichBar] == 1)
               doAlert("up");
         else  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 =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," xo trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," xo "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void deleteLine(int i)
{
   ObjectDelete(LinesIdentifier+":"+Time[i]);
}
void drawLine(int i, color theColor)
{
   string name = LinesIdentifier+":"+Time[i];
   if (ObjectFind(name)<0)
       ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
       ObjectSet(name,OBJPROP_COLOR,theColor);
       ObjectSet(name,OBJPROP_BACK,true);
       ObjectSet(name,OBJPROP_STYLE,LinesStyle);
}

