Do you know CCI formula - https://www.metatrader5.com/en/terminal/help/indicators/oscillators/cci?
Calculation:
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.
SMA(TP, N) = SUM[TP, N]/N
To subtract the received SMA(TP, N) from Typical Prices.
D = TP — SMA(TP, N)
To calculate the n-period Simple Moving Average of absolute D values.
SMA(D, N) = SUM[D, N]/N
To multiply the received SMA(D, N) by 0,015.
M = SMA(D, N) * 0,015
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...
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
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
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.