Need help with backtesting CCI values

 

I am backtesting with downloaded data, using "Open prices only" model, with iCCI more or less as follow:


int BarBefore = 10;

int MyCCI ()
{
double CCINow, CCIPrev, CCILimit;
int i = 0;
double CCI_array [10];

ArrayInitialize(CCI_array,0);

CCINow = iCCI(NULL,0,14,MODE_CLOSE,0);

Text = StringConcatenate(TimeToStr (LocalTime()), ". CCINow= ", DoubleToStr (CCINow,4), "<br>" );
if (LocalTime() >= TimeToCheck1 && LocalTime() <= TimeToCheck2)
WriteFile (MyPath, Text);


if ( CCINow > CCILimit )
{
while (i < BarBefore)
{
CCI_array [i] = iCCI(NULL,0,14,MODE_CLOSE,i);
i++;
}

}

... ...

}


The code did not give the result I expected. So, I try to write the values of each CCI at each point of time (hour, H1). It turned out that the values were all vastly different from the values I obtained from the chart (using mouse-over values).


This is the result:


From calculation From chart (rounded)
2008.04.03 08:00. CCINow= -93.6565 -123
2008.04.03 09:00. CCINow= -15.1045 -137
2008.04.03 10:00. CCINow= -62.3239 -148
2008.04.03 11:00. CCINow= -117.3994 -192
2008.04.03 12:00. CCINow= -149.6646 -178
2008.04.03 13:00. CCINow= -108.5500 -174
2008.04.03 14:00. CCINow= -116.7225 -126
2008.04.03 15:00. CCINow= -4.6005 -16
2008.04.03 16:00. CCINow= 47.9989 -30
2008.04.03 17:00. CCINow= 84.8964 66
2008.04.03 18:00. CCINow= 138.8473 117
2008.04.03 19:00. CCINow= 119.9013 117
2008.04.03 20:00. CCINow= 118.9405 113



What has gone wrong? I expect slight variations in values, but not like this.

The last values are close to the chart values, but further up, the values are vastly different.


I tried to insert a CCI chart using open/close/low mode, all with the same weird result.

 

Do you know CCI formula - https://www.metatrader5.com/en/terminal/help/indicators/oscillators/cci?

Calculation:

  1. To find a Typical Price. You need to add the HIGH, the LOW, and the CLOSE prices of each bar and then divide the result by 3.

    TP = (HIGH + LOW +CLOSE)/3

    To calculate the n-period Simple Moving Average of typical prices.

  2. SMA(TP, N) = SUM[TP, N]/N

    To subtract the received SMA(TP, N) from Typical Prices.

  3. D = TP — SMA(TP, N)

    To calculate the n-period Simple Moving Average of absolute D values.

  4. SMA(D, N) = SUM[D, N]/N

    To multiply the received SMA(D, N) by 0,015.

  5. M = SMA(D, N) * 0,015

  6. To divide M by D

    CCI = M/D

Have you ever read article Strategy Tester: Modes of Modeling during Testing ?


Open Price

Some traders do not wish to depend on particluarities of intrabar modeling, so they create Expert Advisors trading on the bars already completed. The fact that the current price bar is fully completed can only be known when the next one appears. These are the Expert Advisors, for which the mode of "Open Price" modeling is intended.




In this mode, first, bar is opened (Open = High = Low = Close, Volume=1), and this allows the Expert Advisor to identify the end of completion of the preceding price bar. It is this incipient bar, on which the Expert Advisor's testing is launched. At the next stage, the current bar fully completed is yielded, but no testing is performed on it!

 

Rosh,


Thanks so much for your prompt reply.

I had read the article on Modes and Modelling before, twice or 3 times, but still could not fully understand it. My logic says, if no testing is performed on the current bar, the tester should be able to evaluate the previous bar. But CCIPrev = iCCI(NULL,0,14,MODE_CLOSE,1) also gives the same result as before, except that it is shifted one period (i.e. still different from the chart). Do I have to calculate the CCI using TP = (HIGH + LOW +CLOSE)/3 ... etc. and not using iCCI ?


Could you recommend some code to get around the limitation? Probably this is what is needed by many MT4 users.

I believe MT4 is a very very great program, but everything has limitation. Again, thanks...

 
nugroho2 :

Rosh,


Thanks so much for your prompt reply.

I had read the article on Modes and Modelling before, twice or 3 times, but still could not fully understand it. My logic says, if no testing is performed on the current bar, the tester should be able to evaluate the previous bar. But CCIPrev = iCCI(NULL,0,14,MODE_CLOSE,1) also gives the same result as before, except that it is shifted one period (i.e. still different from the chart). Do I have to calculate the CCI using TP = (HIGH + LOW +CLOSE)/3 ... etc. and not using iCCI ?


Or could you recommend some code to get around the limitation? Probably this is what is needed by many MT4 users.

I believe MT4 is a very very great program, but everything has limitation. Again, thanks...

 

Simple script for checking of CCI

//+------------------------------------------------------------------+
//|                                             ScriptCCI_values.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   for (int i=10; i>0; i--)
     {
      Print(TimeToStr(Time[i]),":  iCCI(NULL,0,14,MODE_CLOSE,i) = ",iCCI(NULL,0,14,MODE_CLOSE,i));
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Results

08:47:27 ScriptCCI_values EURUSD,H4: loaded successfully
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.19 12:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 257.6812
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.19 16:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 217.9781
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.19 20:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 141.6138
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 00:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 63.0297
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 04:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -2.6316
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 08:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -28.1261
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 12:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -36.9979
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 16:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 29.7255
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.20 20:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 248.7518
08:47:27 ScriptCCI_values EURUSD,H4: 2009.02.23 00:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 161.7292
08:47:27 ScriptCCI_values EURUSD,H4: uninit reason 0
08:47:27 ScriptCCI_values EURUSD,H4: removed

 

This is EA for checking of CCI value


//+------------------------------------------------------------------+
//|                                             ExpertCCI_values.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   Print(TimeToStr(Time[1]),":  iCCI(NULL,0,14,MODE_CLOSE,i) = ",iCCI(NULL,0,14,MODE_CLOSE,1));
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Test that one on EURUSD H4 "Open Price" Mode.

Results


08:58:09 2009.02.19 16:00 ExpertCCI_values EURUSD,H4: 2009.02.19 12:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 257.6812
08:58:09 2009.02.19 20:00 ExpertCCI_values EURUSD,H4: 2009.02.19 16:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 217.9781
08:58:09 2009.02.20 00:00 ExpertCCI_values EURUSD,H4: 2009.02.19 20:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 141.6138
08:58:09 2009.02.20 04:00 ExpertCCI_values EURUSD,H4: 2009.02.20 00:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 63.0297
08:58:09 2009.02.20 08:00 ExpertCCI_values EURUSD,H4: 2009.02.20 04:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -2.6316
08:58:09 2009.02.20 12:00 ExpertCCI_values EURUSD,H4: 2009.02.20 08:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -28.1261
08:58:09 2009.02.20 16:00 ExpertCCI_values EURUSD,H4: 2009.02.20 12:00: iCCI(NULL,0,14,MODE_CLOSE,i) = -36.9979
08:58:09 2009.02.20 20:00 ExpertCCI_values EURUSD,H4: 2009.02.20 16:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 29.7255
08:58:09 2009.02.20 23:59 ExpertCCI_values EURUSD,H4: 2009.02.20 16:00: iCCI(NULL,0,14,MODE_CLOSE,i) = 29.7255

 
Check both programs yourself. Ask question about MQL4 language on our forum in MQL4.community, please.
 

Rosh,


Thanks so much.

Now I understand more about the way MT4 works. I am able to fill my array properly.

Conclusion is :

  1. each bar must obtain its value directly from the start() function;
  2. each bar must obtain the value from the previous shift.
Also, thanks for your info about the proper forum.