//+------------------------------------------------------------------+
//|                                              All Woodies CCI.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright   "mladen"
#property link        "mladenfx@gmail.com"

//
//
//
//
//

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  Blue
#property indicator_color2  Red
#property indicator_color3  LimeGreen
#property indicator_color4  Tomato
#property indicator_color5  SpringGreen
#property indicator_color6  Magenta
#property indicator_color7  SlateGray
#property indicator_color8  YellowGreen
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_width7  2

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

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

extern string _                    = "Parameters";
extern int    CCIPeriod            = 14;
extern int    TrendPeriod          =  6;
extern int    TurboCCIPeriod       =  6;
extern int    LSMAPeriod           = 25;
extern int    LSMAPrice            = PRICE_CLOSE;
extern bool   ShowLSMAline         = false;
extern int    EMAPeriod            = 34;
extern int    EMAPrice             = PRICE_CLOSE;
extern bool   ShowEMAline          = true;
extern bool   ShowLsmaEma          = true;
extern int    LsmaEmaLevel         = 15;

extern string __                   = "Chose timeframes";
extern string timeFrames           = "M1;M5;M15;M30;H1;H4;D1;W1;MN";
extern int    barsPerTimeFrame     = 35;
extern bool   shiftRight           = False; 
extern bool   currentFirst         = true; 
extern color  txtColor             = Silver; 
extern color  separatorColor       = C'29,29,29'; 

//---- buffers
//
//
//
//
//

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

//
//
//
//
//

string shortName;
string labels[];
int    periods[];
int    Shift;
double minValue;
double maxValue;
string indicatorName;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{
      SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
      
      if(!ShowLSMAline)
      {
      SetIndexStyle(2,DRAW_ARROW);  SetIndexArrow(2,158);
      SetIndexStyle(3,DRAW_ARROW);  SetIndexArrow(3,158);
      }
      else
      {
      SetIndexStyle(2,DRAW_LINE);
      SetIndexStyle(3,DRAW_LINE);
      }


      if(!ShowEMAline)
      {
      SetIndexStyle(4,DRAW_ARROW);  SetIndexArrow(4,158);
      SetIndexStyle(5,DRAW_ARROW);  SetIndexArrow(5,158);
      }
      else
      {      
      SetIndexStyle(4,DRAW_LINE);
      SetIndexStyle(5,DRAW_LINE);
      }

      SetIndexBuffer(2,buffer3);
      SetIndexBuffer(3,buffer4);
      SetIndexBuffer(4,buffer5);
      SetIndexBuffer(5,buffer6);

      SetIndexBuffer(6,buffer7);
      SetIndexBuffer(7,buffer8);

            
            SetIndexLabel(0,"Trend up");
            SetIndexLabel(1,"Trend down");
            SetIndexLabel(2,"Close above LSMA");
            SetIndexLabel(3,"Close bellow LSMA");
            SetIndexLabel(4,"Close above EMA");
            SetIndexLabel(5,"Close bellow EMA");
            SetIndexLabel(6,"CCI");
            SetIndexLabel(7,"Turbo CCI");

      if (_=="CalculateWoodie") return(0);

      //
      //
      //
      //
      //
            
      if (shiftRight) Shift = 1;
      else            Shift = 0;
         barsPerTimeFrame = MathMax(barsPerTimeFrame,15);      
         shortName =  "All Woodies CCI ("+CCIPeriod+") ";
                     shortName = MakeUniqueName(shortName,"" );               

                     IndicatorShortName(shortName);

         //
         //
         //
         //
         //
         
         for (int k =0;k <8; k++) SetIndexShift(k,Shift*(barsPerTimeFrame+1));            

      //
      //
      //
      //
      //
      
      timeFrames = StringTrimLeft(StringTrimRight(timeFrames));
      if (StringSubstr(timeFrames,StringLen(timeFrames),1) != ";")
                       timeFrames = StringConcatenate(timeFrames,";");

         //
         //
         //
         //
         //                                   
            
         int s = 0;
         int i = StringFind(timeFrames,";",s);
         int time;
         string current;
            while (i > 0)
            {
               current = StringSubstr(timeFrames,s,i-s);
               time    = stringToTimeFrame(current);
               if (time > 0) {
                     ArrayResize(labels ,ArraySize(labels)+1);
                     ArrayResize(periods,ArraySize(periods)+1);
                                 labels[ArraySize(labels)-1] = TimeFrameToString(time); 
                                 periods[ArraySize(periods)-1] = time; }
                                 s = i + 1;
                                     i = StringFind(timeFrames,";",s);
            }
      
      //
      //
      //
      //
      //

      if(currentFirst)
         for (i=1;i<ArraySize(periods);i++)
         if (Period()==periods[i])
            {
               string tmpLbl = labels[i];
               int    tmpPer = periods[i];
               
               //
               //
               //
               //
               //
               
               for (k=i ;k>0; k--)
                     {
                        labels[k]  = labels[k-1];
                        periods[k] = periods[k-1];
                     }                     
               labels[0]  = tmpLbl;
               periods[0] = tmpPer;
            }
   indicatorName = WindowExpertName();                  
   return(0);
}

//
//
//
//
//

int deinit()
{
   for(int l=0;l<ArraySize(periods);l++) {
         ObjectDelete(shortName+l);
         ObjectDelete(shortName+l+"label");
      }         
   return(0);
}


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

int start()
{
     
     if (_=="CalculateWoodie") { CalculateWoodie(); return(0); }
     
     string separator;
     int    window=WindowFind(shortName);
     int    k=0;


     //
     //
     //
     //
     //

         minValue =  999999;
         maxValue = -999999;
            for(int p=0; p<ArraySize(periods);p++)
                  {
                     for(int i=0; i<barsPerTimeFrame;i++,k++)
                           {
                              buffer1[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,0,i);
                              buffer2[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,1,i);
                              buffer3[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,2,i);
                              buffer4[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,3,i);
                              buffer5[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,4,i);
                              buffer6[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,5,i);
                              buffer7[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,6,i);
                              buffer8[k] = iCustom(NULL,periods[p],indicatorName,"CalculateWoodie",CCIPeriod,TrendPeriod,TurboCCIPeriod,LSMAPeriod,LSMAPrice,ShowLSMAline,EMAPeriod,EMAPrice,ShowEMAline,ShowLsmaEma,LsmaEmaLevel,7,i);
                              checkMinMax(k);
                           }
                           
                           buffer1[k] = EMPTY_VALUE;
                           buffer2[k] = EMPTY_VALUE;
                           buffer3[k] = EMPTY_VALUE;
                           buffer4[k] = EMPTY_VALUE;
                           buffer5[k] = EMPTY_VALUE;
                           buffer6[k] = EMPTY_VALUE;
                           buffer7[k] = EMPTY_VALUE;
                           buffer8[k] = EMPTY_VALUE;
                           k += 1;
                           
                           //
                           //
                           //
                           //
                           //
                           
                           separator = shortName+p;
                           if(ObjectFind(separator)==-1)
                              ObjectCreate(separator,OBJ_TREND,window,0,0);
                              ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-1));
                              ObjectSet(separator,OBJPROP_TIME2,barTime(k-Shift*(barsPerTimeFrame+1)-1));
                              ObjectSet(separator,OBJPROP_PRICE1,  0);
                              ObjectSet(separator,OBJPROP_PRICE2,100);
                              ObjectSet(separator,OBJPROP_BACK  ,true);
                              ObjectSet(separator,OBJPROP_COLOR ,separatorColor);
                              ObjectSet(separator,OBJPROP_WIDTH ,2);
                           
                           separator = shortName +p+"label";
                           if(ObjectFind(separator)==-1)
                              ObjectCreate(separator,OBJ_TEXT,window,0,0);
                              ObjectSet(separator,OBJPROP_TIME1,barTime(k-(Shift*barsPerTimeFrame)-5));
                              ObjectSetText(separator,labels[p],9,"Arial",txtColor);
                  }

         //
         //
         //
         //
         //
         
         for(p=0; p<ArraySize(periods);p++) {
               separator = shortName+p;
                  ObjectSet(separator,OBJPROP_PRICE1,minValue);
                  ObjectSet(separator,OBJPROP_PRICE2,maxValue);
               separator = shortName+p+"label";
                  ObjectSet(separator,OBJPROP_PRICE1,maxValue);
               
            }                  

      //
      //
      //
      //
      //
      
      SetIndexDrawBegin(0,Bars-k);
   return(0);
}



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

//
//
//
//
//

string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

      while (WindowFind(result)>= 0)
             result = first+(MathRand()%1001)+rest;
   return(result);
}

//
//
//
//
void CalculateWoodie()
{   
   int    counted_bars=IndicatorCounted();
   int    i,limit;

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

   //
   //
   //
   //
   //
   
   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);
            SetHisto(i);  if (ShowLsmaEma) SetLsmaEma(i);
   }
}
  
//
//
//
//
//

void SetHisto(int i)
{
   int k;

   buffer1[i] = EMPTY_VALUE;
   buffer2[i] = EMPTY_VALUE;
   
   //
   //
   //
   //
   //
   
   if (buffer7[i]>0)
   {
      if (buffer1[i+1] != EMPTY_VALUE) { buffer1[i] = buffer7[i]; return; }
            for (k=1; k<TrendPeriod; k++)
                  if (buffer2[i+k] != EMPTY_VALUE) return;
      buffer1[i] = buffer7[i];
   }
   if (buffer7[i]<0)
   {
      if (buffer2[i+1] != EMPTY_VALUE) { buffer2[i] = buffer7[i]; return; }
            for (k=1; k<TrendPeriod; k++) 
                  if (buffer1[i+k] != EMPTY_VALUE) return;
      buffer2[i] = buffer7[i];
   }
}

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

void SetLsmaEma(int i)
{
   double lsmaValue = iLsma(i);
   double emaValue  = iMA(NULL,0,EMAPeriod,0,MODE_EMA,EMAPrice,i);
   
   buffer3[i] = EMPTY_VALUE;
   buffer4[i] = EMPTY_VALUE;
   buffer5[i] = EMPTY_VALUE;
   buffer6[i] = EMPTY_VALUE;
      if (Close[i] > lsmaValue) buffer3[i] =  LsmaEmaLevel +2.00;
      if (Close[i] < lsmaValue) buffer4[i] =  LsmaEmaLevel +2.00;
      if (Close[i] > emaValue)  buffer5[i] = -LsmaEmaLevel;
      if (Close[i] < emaValue)  buffer6[i] = -LsmaEmaLevel;
}

//
//
//
//
//

double iLsma(int shift)
{
   double ma1=iMA(NULL,0,LSMAPeriod,0,MODE_SMA ,LSMAPrice,shift);
   double ma2=iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,shift);
   return(3.0*ma2-2.0*ma1);
}




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

void checkMinMax(int shift)
{
   minValue = MathMin(buffer7[shift],minValue);
   maxValue = MathMax(buffer7[shift],maxValue);
}

//
//
//
//
//


int barTime(int a)
{
   if(a<0)
         return(Time[0]+Period()*60*MathAbs(a));
   else  return(Time[a]);   
}

//+------------------------------------------------------------------+
//+                                                                  +
//+------------------------------------------------------------------+
//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
       
         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);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   
   while(lenght >= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
          else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}