MT4 CCI Bug?

 

Hi,

 Is it me or have I discovered a bug with the CCI calculation in MetaTrader 4? 

 I'm using this EA code...

//+------------------------------------------------------------------+
//|                                                      TestCCI.mq4 |
//|                                                                  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

class CNewBar
{
        private:
                datetime _time[], _lastTime;
        
        public:
                void CNewBar();
                bool CheckNewBar(string pSymbol, int pTimeframe);
};

void CNewBar::CNewBar(void)
{
        ArraySetAsSeries(_time,true);
}

bool CNewBar::CheckNewBar(string pSymbol,int pTimeframe)
{
        bool firstRun = false, newBar = false;
        CopyTime(pSymbol,pTimeframe,0,2,_time);
        
        if(_lastTime == 0) firstRun = true;
        
        if(_time[0] > _lastTime)
        {
                if(firstRun == false) newBar = true;
                _lastTime = _time[0];
        }
        
        return(newBar);
}

CNewBar NewHour;
CNewBar NewDay;

bool TradeOnBarOpen = true;

double day = 0;
double hour = 0;

bool DayBar = false;
bool HourBar = false;
int  barShift = 0;

int OnInit()
{
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   // Check for bar open
   DayBar = true;
   HourBar = true;
   
   if(TradeOnBarOpen == true)
   {
      DayBar = NewDay.CheckNewBar(_Symbol,PERIOD_D1);
      HourBar = NewHour.CheckNewBar(_Symbol,PERIOD_H1);
      barShift = 1;
   }
  
   if( DayBar == true )
   {       
      day = iCCI(NULL,PERIOD_D1,14,PRICE_CLOSE,1);
   }
   
   if( HourBar == true )
   {       
      hour = iCCI(NULL,PERIOD_H1,14,PRICE_CLOSE,1);
   }
   
   if( DayBar == true && HourBar == true )   
      Print("_Period = "+IntegerToString(_Period)+" day = "+DoubleToString(day)+" hour = "+DoubleToString(hour));
}

 which displays the CCI result for both the Day and Hour periods.

 When I compare the result with the actual periods selected within MetaTrader I get two different results. Why? Which is correct? If my code is wrong where is the error? Have a made a stupid mistake?

 Here's screenshots of the CCI indicator using the Day and Hour periods. 

Day Period 

 

Hour Period 

 

Thanks in advance for your help. 

 
imamushroom:

Hi,

 Is it me or have I discovered a bug with the CCI calculation in MetaTrader 4? 

 I'm using this EA code...

 which displays the CCI result for both the Day and Hour periods.

 When I compare the result with the actual periods selected within MetaTrader I get two different results. Why? Which is correct? If my code is wrong where is the error? Have a made a stupid mistake?

 Here's screenshots of the CCI indicator using the Day and Hour periods. 

Day Period 

 

Hour Period 

 

Thanks in advance for your help. 

Neither an MT4 bug or a bug in your code. It's you :-)

day = iCCI(NULL,PERIOD_D1,14,PRICE_CLOSE,1);

That's the previous candle.

 
Alain Verleyen:

Neither an MT4 bug or a bug in your code. It's you :-)

That's the previous candle.

Thanks and point taken. Silly mistake!

Thanks again. 

Reason: