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

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  DarkSlateGray
#property indicator_color2  Blue
#property indicator_color3  Red
#property indicator_color4  Yellow
#property indicator_color5  LimeGreen
#property indicator_color6  Tomato
#property indicator_color7  DimGray
#property indicator_color8  YellowGreen
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width7  2

//
//
//
//
//

#property indicator_level1  -100
#property indicator_level2     0
#property indicator_level3   100
#property indicator_level4   200
#property indicator_level5  -200
#property indicator_levelcolor DarkSlateGray

//
//
//
//
//

extern string TimeFrame            = "Current time frame";
extern int    CCIPeriod            = 14;
extern int    TrendPeriod          =  6;
extern int    TurboCCIPeriod       =  6;
extern int    LSMAPeriod           = 25;
extern int    LSMAPrice            = PRICE_CLOSE;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
string IndicatorFileName;
int    timeFrame;

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

int init()
{
   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);

   //
   //
   //
   //
   //
   
   timeFrame = stringToTimeFrame(TimeFrame);
      if (timeFrame < Period()) timeFrame = Period();
      
   //
   //
   //
   //
   //
         
   IndicatorShortName("CCI Woodie ("+TimeFrameToString(timeFrame)+") ("+CCIPeriod+","+TurboCCIPeriod+")");
   IndicatorFileName = WindowExpertName();
   return(0);
}
int deinit()
{
   return(0);
}

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

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

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


   //
   //
   //
   //
   //

      if (timeFrame != Period())
      {
         limit = MathMax(limit,timeFrame/Period());
         
         //
         //
         //
         //
         //
         
         for(i=limit; i>=0; i--)
         {
            int y = iBarShift(NULL,timeFrame,Time[i]);
                  buffer1[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,0,y);
                  buffer2[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,1,y);
                  buffer3[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,2,y);
                  buffer4[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,3,y);
                  buffer5[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,4,y);
                  buffer6[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,5,y);
                  buffer7[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,6,y);
                  buffer8[i] = iCustom(NULL,timeFrame,IndicatorFileName,"",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,7,y);
         }
         return(0);         
      }

   //
   //
   //
   //
   //
   
   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);
  }
   
   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;
}

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

int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int char = StringGetChar(tfs,l);
          if((char > 96 && char < 123) || (char > 223 && char < 256))
               tfs = StringSetChar(tfs, l, char - 32);
          else 
              if(char > -33 && char < 0)
                  tfs = StringSetChar(tfs, l, char + 224);
   }

   //
   //
   //
   //
   //
   
   int tf=Period();
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
   return(tf);
}

//
//
//
//
//
//

string TimeFrameToString(int tf)
{
   string tfs="Current time frame";
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}