CCI Calculation

 

Hello,


for an expert i try to calculate the CCI by myself. What i wonder, i never get the value from the CCI indicator. Is the iCCI(..) function somethins special to the original calculation of CCI?


input int CCI_Periode=8;
input int MN=123;
input string Text="Trailing";

double CCI1[];
int   CCIHandle=0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   CCIHandle= iCCI(_Symbol,PERIOD_M5,8,PRICE_CLOSE);
   ArraySetAsSeries(CCI1,true);
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {//--- destroy timer

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
  CopyBuffer(CCIHandle,0,0,20,CCI1);
  
   MqlTradeRequest request;
   MqlTradeResult  result;

   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick)) GetLastError();

   MqlRates rates[];
   ArraySetAsSeries(rates,true); 
   int copied=CopyRates(NULL,0,0,16,rates);
   if(copied<=0)
      Print("Fehler des Kopieren von Preisdaten ",GetLastError());



   double hoch[],tief[],close[],TP[], Hoch[],Tief[],Close[] ;
   

   ArrayResize(TP,24);

   
   CopyHigh(_Symbol,PERIOD_M5,0,24,Hoch);
   CopyLow(_Symbol,PERIOD_M5,0,24,Tief);
   CopyClose(_Symbol,PERIOD_M5,0,24,Close);

   for(int i=0;i<24; i++)
     {

      TP[i]=(Hoch[i]+Tief[i]+Close[i])/3;
      
      Print(i, "= Hoch: ",Hoch[i], " Tief: ", Tief[i], " Close: " ,Close[i], " TP: ", TP[i]   );
 
     }


      double AVG1 =(TP[1]+TP[2]+TP[3]+TP[4]+TP[5]+TP[6]+TP[7]+TP[8])/8;
      double AVG2 =(TP[2]+TP[3]+TP[4]+TP[5]+TP[6]+TP[7]+TP[8]+TP[9])/8;
      double AVG3 =(TP[3]+TP[4]+TP[5]+TP[6]+TP[7]+TP[8]+TP[9]+TP[10])/8;
      double AVG4 =(TP[4]+TP[5]+TP[6]+TP[7]+TP[8]+TP[9]+TP[10]+TP[11])/8;
      double AVG5 =(TP[5]+TP[6]+TP[7]+TP[8]+TP[9]+TP[10]+TP[11]+TP[12])/8;
      double AVG6 =(TP[6]+TP[7]+TP[8]+TP[9]+TP[10]+TP[11]+TP[12]+TP[13])/8;
      double AVG7 =(TP[7]+TP[8]+TP[9]+TP[10]+TP[11]+TP[12]+TP[13]+TP[14])/8;
      double AVG8 =(TP[8]+TP[9]+TP[10]+TP[11]+TP[12]+TP[13]+TP[14]+TP[15])/8;
      
      double AVG9  =(TP[9]+TP[10] +TP[11]+TP[12]+TP[13]+TP[14]+TP[15]+TP[16])/8;
      double AVG10 =(TP[10]+TP[11]+TP[12]+TP[13]+TP[14]+TP[15]+TP[16]+TP[17])/8;
      double AVG11 =(TP[11]+TP[12]+TP[13]+TP[14]+TP[15]+TP[16]+TP[17]+TP[18])/8;
      double AVG12 =(TP[12]+TP[13]+TP[14]+TP[15]+TP[16]+TP[17]+TP[18]+TP[19])/8;
      double AVG13 =(TP[13]+TP[14]+TP[15]+TP[16]+TP[17]+TP[18]+TP[19]+TP[20])/8;
      double AVG14 =(TP[14]+TP[15]+TP[16]+TP[17]+TP[18]+TP[19]+TP[20]+TP[21])/8;
      double AVG15 =(TP[15]+TP[16]+TP[17]+TP[18]+TP[19]+TP[20]+TP[21]+TP[22])/8;
      double AVG16 =(TP[16]+TP[17]+TP[18]+TP[19]+TP[20]+TP[21]+TP[22]+TP[23])/8;
      
   
      
      double SumTP = TP[16]+TP[17]+TP[18]+TP[19]+TP[20]+TP[21]+TP[22]+TP[23];
      double SumAVG = AVG9+AVG10+AVG11+AVG12+AVG13+AVG14+AVG15+AVG16;
      double sAVG = MathAbs(SumTP-SumAVG)/8;
      
      
      double CCI = (TP[23] - AVG16 ) / (0.0015*sAVG);

      Print("TP: ",TP[23], " AVG: ",AVG16, " SAVG: ",sAVG, " CCI: ",  CCI);
      Comment(CCI);
      
      

  }
//+------------------------------------------------------------------+


original calculation i found herehttps://en.wikipedia.org/wiki/Commodity_channel_index


can someone help and explain my problem?


thanks 

amando

 
Here is an Excel sheet of the CCI - compare their formulas of the cells with yours.