Download MetaTrader 5

CCI on Array , What am I doing Wrong?

To add comments, please log in or register
Michalis Phylactou
12989
Michalis Phylactou  

Hi guys. 

I am trying to calculate CCI values applied on an Array of Heiken Ashi closing values.

 I generate an Array with values taken from the Heiken Ashi closing values

int CCI_Period=50;
double HeikenClose[];
ArrayResize(HeikenClose,10+CCI_Period);

  //develop the Heiken Values
  for (int i=0; i<( CCI_Period+5); i++)
   {
         HeikenClose[i]= iCustom(Symbol(),HeikenAshi_TF,"Heiken Ashi",3,i);
   }


 and then to calculate the CCI values of the last 3 

 


double CCIvalue_now=iCCIOnArray(HeikenClose,CCI_Period,CCI_Period,0);

double CCIvalue_before=iCCIOnArray(HeikenClose,CCI_Period,CCI_Period,1);

double CCIvalue_before2=iCCIOnArray(HeikenClose,CCI_Period,CCI_Period,2);

 

The first one generates a value while the other 2 give 0. 

Something has to do with the shifting I am getting.

Can someone advise.

Thank you 

whroeder1
16563
whroeder1  
  1. Try setting the array as series before filling the array so OnArray knows how to process it.
  2. Tell OnArray the real size of your data.
  3. Why is the array sized to 10+CCI_Period but only filled to CCI_Period+5?
Michalis Phylactou
12989
Michalis Phylactou  
whroeder1:
  1. Try setting the array as series before filling the array so OnArray knows how to process it.
  2. Tell OnArray the real size of your data.
  3. Why is the array sized to 10+CCI_Period but only filled to CCI_Period+5?

I put it bigger in case I need more data or to prevent array out range. It shouldn't really matter as the averaging is done on the first "CCI_Period" elements of the array and shifted for the other 2 values.

Any idea why the first returns a value while the other 2 return 0 ?

ArraySetAsSeries - Array Functions - MQL4 Reference
ArraySetAsSeries - Array Functions - MQL4 Reference
  • docs.mql4.com
ArraySetAsSeries - Array Functions - MQL4 Reference
Mladen Rakic
65744
Mladen Rakic  
Michalis Phylactou:

I put it bigger in case I need more data or to prevent array out range. It shouldn't really matter as the averaging is done on the first "CCI_Period" elements of the array and shifted for the other 2 values.

Any idea why the first returns a value while the other 2 return 0 ?

But you have un-initialized elements of an array that the iCCIOnArray() does not "know" about and you are going to have (depending on the cci period) values that have nothing in common with the real values when those elements are used
whroeder1
16563
whroeder1  
Michalis Phylactou: Any idea why the first returns a value while the other 2 return 0 ?
Asked and answered. #1 #2
Michalis Phylactou
12989
Michalis Phylactou  

I have modified my code as following.

Please provide feedback

 Defining the arrays where I story the Heiken Ashi closed values under variables. 

double HeikenClose_0[]; 
double HeikenClose_1[]; 
double HeikenClose_2[]; 

extern int CCI_Period=50;

double CCIvalue_now;

double CCIvalue_before;

 double CCIvalue_before2;


 Setting the array equal to the CCI Periods under init()

   ArrayResize(HeikenClose_0,CCI_Period);
   ArrayResize(HeikenClose_1,CCI_Period);
   ArrayResize(HeikenClose_2,CCI_Period);

Performing the following under onTick()

 

//storing the Heiken Ashi closed values in each array with 1,2 candles lag between them
  for (int i=0; i<CCI_Period; i++)
   {
         HeikenClose_0[i]=iCustom(Symbol(),HeikenAshi_TF,"Heiken Ashi",3,i);
         HeikenClose_1[i]=iCustom(Symbol(),HeikenAshi_TF,"Heiken Ashi",3,i+1);
         HeikenClose_2[i]=iCustom(Symbol(),HeikenAshi_TF,"Heiken Ashi",3,i+2);
   }

//setting the arrays as series as recommended
ArraySetAsSeries(HeikenClose_0,False);
ArraySetAsSeries(HeikenClose_1,False);
ArraySetAsSeries(HeikenClose_2,False);

//final calculation of the last 3 CCI values on the data
  CCIvalue_now=iCCIOnArray(HeikenClose_0,CCI_Period,CCI_Period,0);
  CCIvalue_before=iCCIOnArray(HeikenClose_1,CCI_Period,CCI_Period,0);
  CCIvalue_before2=iCCIOnArray(HeikenClose_2,CCI_Period ,CCI_Period,0);

 

Thank you in advance

To add comments, please log in or register