//+------------------------------------------------------------------+
//|                                                   GentorCCIM.mq4 |
//|                                                  Egorov Gennadiy |
//| 2005.08.20 KimIV  v.0.0                                          |
//| Немного модернизировал, добавив LSMA и поменяв её местами с ЕМА  |
//+------------------------------------------------------------------+
#property copyright "Egorov Gennadiy, FX Sniper, KimIV"
#property link      "http://www.kimiv.ru"

#property indicator_separate_window
#property indicator_buffers 7

#property indicator_color1 Red
#property indicator_color2 Black
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Red      
#property indicator_color7 Lime

#property indicator_level1 300
#property indicator_level2 200
#property indicator_level3 100
#property indicator_level4 50
#property indicator_level5 -50
#property indicator_level6 -100
#property indicator_level7 -200
#property indicator_level8 -300

//---- Внешние параметры ---------------------------------------------
extern int SlowCCIPeriod = 14;  // Период медленного CCI
extern int FastCCIPeriod = 6;   // Период быстрого CCI
extern int EMAPeriod     = 34;  // Период ЕМА
extern int LSMAPeriod    = 25;  // Период LSMA

//---- Буферы индикатора ---------------------------------------------
double FastWoodieCCI[];
double SlowWoodieCCI[];
double HistWoodieCCI[];
double LineHighEMA[];
double LineLowEMA[];
double LSMABuffer1[];
double LSMABuffer2[];
double wt[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init() {
  IndicatorBuffers(8);
  IndicatorDigits(2);

  SetIndexStyle (0, DRAW_LINE, STYLE_SOLID, 1);
  SetIndexBuffer(0, FastWoodieCCI);
  SetIndexStyle (1, DRAW_LINE, STYLE_SOLID, 2);
  SetIndexBuffer(1, SlowWoodieCCI);
  SetIndexStyle (2, DRAW_HISTOGRAM, STYLE_SOLID, 1);
  SetIndexBuffer(2, HistWoodieCCI);
  SetIndexStyle (3, DRAW_LINE, STYLE_SOLID, 3);
  SetIndexBuffer(3, LineHighEMA);
  SetIndexStyle (4, DRAW_LINE, STYLE_SOLID, 3);
  SetIndexBuffer(4, LineLowEMA);
  SetIndexBuffer(5, LSMABuffer1);
  SetIndexStyle (5, DRAW_LINE, STYLE_SOLID, 3);
  SetIndexBuffer(6, LSMABuffer2);
  SetIndexStyle (6, DRAW_LINE, STYLE_SOLID, 3);
  SetIndexBuffer(7, wt);
  Comment("");
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() {
  Comment("");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  int i, shift, counted_bars = IndicatorCounted();
  int Draw4HowLong, loopbegin;
  double sum, lengthvar, tmp ;

  if (counted_bars<0) return;
  if (counted_bars>0) counted_bars--;
  counted_bars = Bars - counted_bars;
  for (shift=0; shift<counted_bars; shift++) {
    FastWoodieCCI[shift] = iCCI(NULL, 0, FastCCIPeriod, PRICE_TYPICAL, shift);
    SlowWoodieCCI[shift] = iCCI(NULL, 0, SlowCCIPeriod, PRICE_TYPICAL, shift);
    HistWoodieCCI[shift] = iCCI(NULL, 0, SlowCCIPeriod, PRICE_TYPICAL, shift);
    //---------------- color coding ----------------------------------
    LineLowEMA[shift] = -250;
    LineHighEMA[shift] = -250;

    double EmaValue = iMA(NULL, 0, EMAPeriod, 0, MODE_EMA, PRICE_TYPICAL, shift);
    if (Close[shift] > EmaValue) LineHighEMA[shift] = EMPTY_VALUE;
    else
      if (Close[shift] < EmaValue) LineLowEMA[shift] = EMPTY_VALUE;
  }

  Draw4HowLong = Bars-LSMAPeriod - 5;
  loopbegin = Draw4HowLong - LSMAPeriod - 1;
 
  for(shift=loopbegin; shift>=0; shift--) {
    sum = 0;
    for (i=LSMAPeriod; i>=1; i--) {
      lengthvar = LSMAPeriod + 1;
      lengthvar /= 3;
      tmp = 0;
      tmp = (i - lengthvar)*Close[LSMAPeriod-i+shift];
      sum+=tmp;
    }
    wt[shift] = sum*6/(LSMAPeriod*(LSMAPeriod+1));
         
    //========== COLOR CODING ===========================================               
    LSMABuffer1[shift] = wt[shift]; //red
    LSMABuffer2[shift] = wt[shift]; //green
       
    if (wt[shift] > Close[shift]) LSMABuffer2[shift] = EMPTY_VALUE;
    else
      if (wt[shift] < Close[shift]) LSMABuffer1[shift] = EMPTY_VALUE;
  }
}
//+------------------------------------------------------------------+

