Download MetaTrader 5

CCI on chart is different than iCCI result

To add comments, please log in or register
Dor Kor
43
Dor Kor  

does anyone know how the CCI on the chart is calculated?

look at the attached pictures

on chart - 14/Nov 10:00 CCI is -208.7495

in my EA - 14/Nov 10:00 iCCI returns -149.5238

in my EA, I'm using the: iCCI(NULL,0,CciPeriod,PRICE_CLOSE,0) CciPeriod=14

I also tryed using PRICE_TYPICAL and PRICE_OPEN, but yet failed to get the CCI value that I see in the chart

please help...

Tjipke de Vries
4473
Tjipke de Vries  

price_close and you take bar 0

do you get right result on bar 1 ??

Dor Kor
43
Dor Kor  
deVries:

price_close and you take bar 0

do you get right result on bar 1 ??


No, other bars are wrong as well.

the code is below. maybe you can help point to the issue


//+--------------------------------------------------+

//| Generic variables |

//+--------------------------------------------------+

double pips; //define pip size





//+--------------------------------------------------+

//| variables for BUY/SELL |

//+--------------------------------------------------+

extern int StopLossLimit=0.0050; //--- Defined the stop loss limit = 50 pips

extern int TakeProfitLimit=0.0100; //--- Defined the take profit limit = 100 pips

extern double LotSize=0.1; //--- Defines the trade lot size = 0.1 lot >> 10,000 >> pips = 1

extern int MagicNumber = 2222;



//+--------------------------------------------------+

//| variables for CCI |

//+--------------------------------------------------+

double CCI[50];

int CciPeriod = 14;

double CciGapForBuy = 0.1;

double CciGapForClose = -0.1;





//+------------------------------------------------------------------+

int init()

{

// Define PIPS size. check if graphs tick is 5 digits or 3 digits then PIPS is tick*10 //

double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);

        if (ticksize == 0.00001 || ticksize == 0.001)

         pips = ticksize*10;

         else pips =ticksize;

//+--------------------------------------------+

//| for CCI check - create initial CCI array |

//+--------------------------------------------+



int CciLoopIndex1=0;

int CciLoopLimit=ArraySize(CCI);



for(CciLoopIndex1=0; CciLoopIndex1<CciLoopLimit; CciLoopIndex1++)

{

CCI[CciLoopIndex1]=iCCI(NULL,0,CciPeriod,PRICE_CLOSE,CciLoopIndex1);

}

return(0);

}



//+------------------------------------------------------------------+

int deinit()

{

return(0);

}



//+------------------------------------------------------------------+

int start()

{

//When new candle starts, checks if Rule for Buy/Sell is met //

if(IsNewCandle())

{

//Print ("New candle");

CheckForRule();

}

return(0);

}



//+------------------------------------------------------------------+

//| checks if any orders open on this currency pair. |

//+------------------------------------------------------------------+

int OpenOrdersThisPair(string pair)

{

int total=0;

for(int i=OrdersTotal()-1; i >= 0; i--)

         {

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()== pair) total++;

         }

         return (total);

}



//+------------------------------------------------------------------+

//| Returns Order number for this currency pair. |

//+------------------------------------------------------------------+

int OrderNumberThisPair(string pair)

{

int OrderTicketNumber=0;

for(int i=OrdersTotal()-1; i >= 0; i--)

         {

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()== pair) OrderTicketNumber=OrderTicket();

         }

         return (OrderTicketNumber);

}



//+------------------------------------------------------------------+

//| Verify its a new candle function |

//+------------------------------------------------------------------+

bool IsNewCandle()

{

static int BarsOnChart=0;

        if (Bars == BarsOnChart)

        return (false);

        BarsOnChart = Bars;

        return(true);

}

//+------------------------------------------------------------------+

//| order entry function |

//+------------------------------------------------------------------+

void OrderEntry(string direction)

{

if(direction=="BUY") //--- for BUY

{

if(OpenOrdersThisPair(Symbol())==0) //--- verifies no other open trades for same currancy pair in parallel

{

int buyticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-0.0050,Ask+0.0200,NULL,MagicNumber,0,Green);

}

}

if(direction=="SELL")

{

if(OpenOrdersThisPair(Symbol())==0)

{

int sellticket = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+0.0050,Bid-0.0200,NULL,MagicNumber,0,Red);

}

}

if(direction=="CLOSE")

{

if(OpenOrdersThisPair(Symbol())>0)

{

OrderClose(OrderNumberThisPair(Symbol()),OrderLots(),Bid,3,Blue);

}

}

}





//+--------------------------------------------------------------------------+

//| This function checks whether the defined rule for Buy or Sell is met. |

//| if rule meets BUY criteria then OrderEntry function is trigerred with 1 |

//| if rule meets SELL criteria then OrderEntry function is trigerred with 0 |

//+--------------------------------------------------------------------------+

void CheckForRule()

{

string CheckCciReturn;



CheckCciReturn = CheckCCI();

if(CheckCciReturn=="BUY") OrderEntry("BUY");

if(CheckCciReturn=="CLOSE") OrderEntry("CLOSE");

}



//+--------------------------------------------------------------------------+

//| This function checks BUY/SELL triggers, based on CCI cross its MA |

//| |

//| |

//+--------------------------------------------------------------------------+

string CheckCCI()

{

int CciLoopIndex1=0;

int CciLoopLimit=ArraySize(CCI);

double PreviousPeriodMA;

double CurrentPeriodMA;



PreviousPeriodMA=iMAOnArray(CCI,CciPeriod,CciPeriod,0,MODE_SMA,0);



//--- move all CCI array cells, to previous cell, in order to free CCI[0] for current CCI info

for(CciLoopIndex1=CciLoopLimit; CciLoopIndex1>0; CciLoopIndex1--)

{

CCI[CciLoopIndex1]=CCI[CciLoopIndex1-1];

}

//--- Retrieve current CCI to CCI[0]

CCI[0]=iCCI(NULL,0,CciPeriod,PRICE_CLOSE,0);

//--- Calculate MA on CCI array, for MA_Period days (currently MA(14))

CurrentPeriodMA=iMAOnArray(CCI,0,CciPeriod,0,MODE_SMA,0);

Print("CCI:"+CCI[0]+" MA:"+CurrentPeriodMA);

if (CCI[0]>CurrentPeriodMA+(MathAbs(CurrentPeriodMA)*CciGapForBuy) && CCI[1]<PreviousPeriodMA) return("BUY");

if (CCI[0]<CurrentPeriodMA+(MathAbs(CurrentPeriodMA)*CciGapForClose))

{

return("CLOSE");

}

return ("NO");



}





//+------------------------------------------------------------------+
whroeder1
17109
whroeder1  
dorkor22: does anyone know how the CCI on the chart is calculated?
  1. src
  2. cci.mq4 has
           MovBuffer[i] = iMA(NULL, 0, CCIPeriod, 0, MODE_SMA, PRICE_TYPICAL, i);       
           :
           price = (High[i] + Low[i] + Close[i]) / 3;
           RelBuffer[i] = price - MovBuffer[i];
Dor Kor
43
Dor Kor  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. cci.mq4 has


thanks for comments

I edited the code as you metioned in #1

in #2, I'm not sure I got your suggestion.

even before the iMA that I'm using, simple iCCI in my EA (look for the print statement) shows different value that CCI indicator.

I attached a picture that shows the different values.

Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
dorkor22:


I attached a picture that shows the different values.

No you didn't . . .

Tjipke de Vries
4473
Tjipke de Vries  
int start()

{

//When new candle starts, checks if Rule for Buy/Sell is met //

if(IsNewCandle())

{

//Print ("New candle");

CheckForRule();

}

double CCI_0 =iCCI(NULL,0,CciPeriod,PRICE_CLOSE,0);
Comment("CCI_0  = ",CCI_0);


return(0);

}

put the green lines inside your code

Check this and you will see the value of CCI at bar 0 every tick....

So why is it different ????

whroeder1
17109
whroeder1  
dorkor22: in #2, I'm not sure I got your suggestion.
#2 was not a suggestion. You asked "dorkor22: does anyone know how the CCI on the chart is calculated?"Answer using PRICE_TYPICAL
To add comments, please log in or register