#property copyright "miro1360"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3

//--- plot Buffer
#property indicator_label1  "CCI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGold
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- plot Buffer
#property indicator_label2  "envUp"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- plot Buffer
#property indicator_label3  "envDown"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- input parameters
input int      barsLimit=0;

input int      cciPeriod=30;
input int      cciPrice=0;

input int      envPeriod=20;
input int      envMode=0;
input int      envPrice=0;
input double   envDeviation=20.34;

bool     recalculateBars1=true;
bool     recalculateBars2=true;
bool     recalculateBars3=true;

//--- indicator buffers
double         Buffer0[];
double         Buffer1[];
double         Buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer0);
   SetIndexBuffer(1,Buffer1);
   SetIndexBuffer(2,Buffer2);
   
   recalculateBars1=true;
   recalculateBars2=true;
   recalculateBars3=true;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---

   CalculateCCI();
   
   CalculateEnvUp();
   
   CalculateEnvDown();

   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+



void CalculateCCI()
{
   int i;
   for(i=barsToCalculate1();i>=0;i--)
   {
      Buffer0[i] = iCCI(Symbol(), Period(), cciPeriod,(ENUM_APPLIED_PRICE)cciPrice, i);
   }
}


void CalculateEnvUp()
{
   int i;
   for(i=barsToCalculate2();i>=0;i--)
   {
      Buffer1[i] = iEnvelopesOnArray(Buffer0, 0, envPeriod, (ENUM_MA_METHOD)envMode, 0, envDeviation, MODE_UPPER, i); 
   }
}


void CalculateEnvDown()
{
   int i;
   for(i=barsToCalculate3();i>=0;i--)
   {
      Buffer2[i] = iEnvelopesOnArray(Buffer0, 0, envPeriod, (ENUM_MA_METHOD)envMode, 0, envDeviation, MODE_LOWER, i); 
   }
}


int barsToCalculate1()
{
   int limit;
   if(recalculateBars1==true) 
   {
      if(barsLimit==0) { limit=Bars-20; }
      else { limit=barsLimit; }
      recalculateBars1=false;
   }
   else limit=0;
   return(limit);
}


int barsToCalculate2()
{
   int limit;
   if(recalculateBars2==true) 
   {
      if(barsLimit==0) { limit=Bars-20; }
      else { limit=barsLimit; }
      recalculateBars2=false;
   }
   else limit=0;
   return(limit);
}


int barsToCalculate3()
{
   int limit;
   if(recalculateBars3==true) 
   {
      if(barsLimit==0) { limit=Bars-20; }
      else { limit=barsLimit; }
      recalculateBars3=false;
   }
   else limit=0;
   return(limit);
}
