//+------------------------------------------------------------------+
//|                                                  CCI Woodie like |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  clrDarkSlateGray
#property indicator_color2  clrBlue
#property indicator_color3  clrRed
#property indicator_color4  clrYellow
#property indicator_color5  clrLimeGreen
#property indicator_color6  clrTomato
#property indicator_color7  clrDimGray
#property indicator_color8  clrYellowGreen
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width7  2
#property indicator_levelcolor clrDarkSlateGray

//
//
//
//
//


extern int                CCIPeriod       = 14;
extern int                TrendPeriod     =  6;
extern int                TurboCCIPeriod  =  6;
extern int                LSMAPeriod      = 25;
extern ENUM_APPLIED_PRICE LSMAPrice       = PRICE_CLOSE;
extern double             levelOs         = -200;
extern double             levelOb         =  200;
extern bool               alertsOn        = true;
extern bool               alertsOnCurrent = false;
extern bool               alertsMessage   = true;
extern bool               alertsSound     = true;
extern bool               alertsEmail     = false;
extern bool               alertsNotify    = false;
extern string             soundFile       = "alert2.wav";

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   IndicatorBuffers(9);
   SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_ARROW);  SetIndexArrow(4,167);
   SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_ARROW);  SetIndexArrow(5,167);
   SetIndexBuffer(6,buffer7);
   SetIndexBuffer(7,buffer8);
   SetIndexBuffer(8,trend);
   SetLevelValue(0,levelOb);
   SetLevelValue(1,levelOs);

   IndicatorShortName("CCI Woodie ("+CCIPeriod+","+TurboCCIPeriod+")");
return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int start()
{
   int i,limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
   for (i=limit;i>=0;i--)
   {
       if (TurboCCIPeriod>1) buffer8[i] = iCCI(NULL,0,TurboCCIPeriod,PRICE_TYPICAL,i); 
                             buffer7[i] = iCCI(NULL,0,CCIPeriod     ,PRICE_TYPICAL,i);
                             buffer1[i] = buffer7[i];
            SetHisto(i);
            SetLsma(i);
            trend[i] = trend[i+1];
            if (buffer7[i]<levelOs) trend[i] = 1;
            if (buffer7[i]>levelOb) trend[i] =-1;
  }
  
  //
  //
  //
  //
  //
      
  if (alertsOn)
  {
    if (alertsOnCurrent)
         int whichBar = 0;
    else     whichBar = 1;
    if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("crossing upper level");
       else  doAlert("crossing lower level");         
  } 
return(0);
}
  
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void SetHisto(int i)
{
   int k,s;

   buffer2[i] = EMPTY_VALUE;
   buffer3[i] = EMPTY_VALUE;
   buffer4[i] = EMPTY_VALUE;
   
   //
   //
   //
   //
   //

      if (buffer7[i]>0)
         if (buffer2[i+1] != EMPTY_VALUE) s = TrendPeriod;
         else
            for (k=1,s= 1; k<TrendPeriod; k++,s++) if (buffer3[i+k] != EMPTY_VALUE) break;

      if (buffer7[i]<0)
         if (buffer3[i+1] != EMPTY_VALUE) s = -TrendPeriod;
         else
            for (k=1,s=-1; k<TrendPeriod; k++,s--) if (buffer2[i+k] != EMPTY_VALUE) break;

      if (s ==  TrendPeriod) buffer2[i] = buffer7[i];
      if (s == -TrendPeriod) buffer3[i] = buffer7[i];
   
   //
   //
   //
   //
   //
   
   if (MathAbs(s) == (TrendPeriod-1)) buffer4[i] = buffer7[i];
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void SetLsma(int i)
{
   double ma1=iMA(NULL,0,LSMAPeriod,0,MODE_SMA ,LSMAPrice,i);
   double ma2=iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,i);
   double lsmaValue = 3.0*ma2-2.0*ma1;
   
   buffer5[i] = EMPTY_VALUE;
   buffer6[i] = EMPTY_VALUE;
      if (Close[i] > lsmaValue) buffer5[i] =  0.00;
      if (Close[i] < lsmaValue) buffer6[i] =  0.00;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

//
//
//
//
//
 
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)," CCI Woodie ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," CCI Woodie "),message);
             if (alertsNotify)  SendNotification(message);
             if (alertsSound)   PlaySound(soundFile);
      }
}


