iClose passing incorrect data

 

I am writing a program that populates a matrix in MQL4 via iClose() and then passes the matrix to R. The data that R is receiving is incorrect for all currency pairs. I have gone over the code 1000 times and cannot figure it out. Anyone else ever experienced iClose() passing bad values?

Here is the code I am using. ArrayName is an array of string names for the currency pairs. I put this section at the bottom of the code (for easier reading) even though it comes first in my program.

double LongAzzArray[34000];
int Counter=0;
int ArrayNameIndex=0;

Comment("MQL4 has began to populate LongAzzArray !");

// THIS SECTION IS NOT WORKING PROPERLY - The Closing values being passed to R are INCORRECT values!!!
for(int k=0; k<34000; k++){

   if(Counter == 1000){
      Counter=0;
      ArrayNameIndex++;
   } // End if loop 
   
   
   LongAzzArray[k] = iClose(ArrayName[ArrayNameIndex], PERIOD_M5, Counter+1);
   Counter++;
   Comment("k = " + k);
}

 
RAssignVector(rhandle, "LongAzzArray", LongAzzArray, ArraySize(LongAzzArray));
RExecute(rhandle, "plot(LongAzzArray[32001:33000]);");
ArrayName[0]="AUDCAD";
ArrayName[1]="AUDJPY";
ArrayName[2]="AUDNZD";
ArrayName[3]="AUDUSD";
ArrayName[4]="AUDCHF";
ArrayName[5]="CADCHF";
ArrayName[6]="CADJPY";
ArrayName[7]="CADSGD";
ArrayName[8]="CHFJPY";
ArrayName[9]="EURUSD";
ArrayName[10]="EURAUD";
ArrayName[11]="EURCAD";
ArrayName[12]="EURCHF";
ArrayName[13]="EURGBP";
ArrayName[14]="EURJPY";
ArrayName[15]="EURSGD";
ArrayName[16]="GBPAUD";
ArrayName[17]="GBPCAD";
ArrayName[18]="GBPCHF";
ArrayName[19]="GBPJPY";
ArrayName[20]="GBPNZD";
ArrayName[21]="GBPUSD";
ArrayName[22]="GBPSGD";
ArrayName[23]="NZDCAD";
ArrayName[24]="NZDJPY";
ArrayName[25]="NZDUSD";
ArrayName[26]="NZDSGD";
ArrayName[27]="NZDCHF";
ArrayName[28]="USDCAD";
ArrayName[29]="USDCHF";
ArrayName[30]="USDJPY";
ArrayName[31]="USDSGD";
ArrayName[32]="SGDCHF";
ArrayName[33]="SGDJPY";
 
pamexx:

I am writing a program that populates a matrix in MQL4 via iClose() and then passes the matrix to R. The data that R is receiving is incorrect for all currency pairs. I have gone over the code 1000 times and cannot figure it out. Anyone else ever experienced iClose() passing bad values?

Here is the code I am using. ArrayName is an array of string names for the currency pairs. I put this section at the bottom of the code (for easier reading) even though it comes first in my program.


What is  R  ?  check that your data is correct before you pass it,  it may well be being broken when you pass it. 
 
I will check. R is an open source math/stat computing language similar to MATLAB. I am using R to perform computations that are not available in MQL4 (Matrix Multiplication, Inversion, etc.) without a TON of programming. The code above is used in a program that will examine cointegration of currency pairs.
 
RaptorUK:
What is  R  ?  check that your data is correct before you pass it,  it may well be being broken when you pass it. 

pamexx:
I will check. R is an open source math/stat computing language similar to MATLAB. I am using R to perform computations that are not available in MQL4 (Matrix Multiplication, Inversion, etc.) without a TON of programming. The code above is used in a program that will examine cointegration of currency pairs.
See R (programming language) on Wikipedia.
 
   LongAzzArray[k] = iClose(ArrayName[ArrayNameIndex], PERIOD_M5, Counter+1);
Standard procedure for other pairs/TFs. Test iTime, if zero, test GetLastError. if it's 4066 sleep 10 seconds and retry iTime.
 
WHRoeder:
Standard procedure for other pairs/TFs. Test iTime, if zero, test GetLastError. if it's 4066 sleep 10 seconds and retry iTime.
I updated the code with a check for the 4066 error but it does not correct the problem. Maybe I am not implementing the error check correctly. The values in the history center are correct but the values that iClose() produces are not correct.
double LongAzzArray[34000];
int Counter=0;
int ArrayNameIndex=0;

Comment("MQL4 has began to populate LongAzzArray !");

// THIS SECTION IS NOT WORKING PROPERLY - The Closing values being passed to R are INCORRECT values!!!
for(int k=0; k<34000; k++){

   if(Counter == 1000){
      Counter=0;
      ArrayNameIndex++;
   } // End if loop 
   

if(iTime(ArrayName[ArrayNameIndex],PERIOD_M5,Counter+1) == 0 && GetLastError() == 4066){
      Sleep(5000);
   } // End if statement
   
   LongAzzArray[k] = iClose(ArrayName[ArrayNameIndex], PERIOD_M5, Counter+1);
   Counter++;
   Comment("k = " + k);
}

 
RAssignVector(rhandle, "LongAzzArray", LongAzzArray, ArraySize(LongAzzArray));
RExecute(rhandle, "plot(LongAzzArray[32001:33000]);");

 
Test iTime, if zero, test GetLastError. if it's 4066 sleep 10 seconds and retry iTime.

if it's not 4066 no error handling

no sleep for 10

no retest

 
pamexx:
I updated the code with a check for the 4066 error but it does not correct the problem. Maybe I am not implementing the error check correctly. The values in the history center are correct but the values that iClose() produces are not correct.

First, instead of:

if(iTime(ArrayName[ArrayNameIndex],PERIOD_M5,Counter+1) == 0 && GetLastError() == 4066){
      Sleep(5000);
   } // End if statement
   
   LongAzzArray[k] = iClose(ArrayName[ArrayNameIndex], PERIOD_M5, Counter+1);
   Counter++;
   Comment("k = " + k);

try the following (not compiled or tested):

datetime i_time = 0; int error_code = 0;
GetLastError();   // clear error flag

for (int i = 1; i <= 6; i++) {
   i_time = iTime(ArrayName[ArrayNameIndex], PERIOD_M5, Counter + 1);
   error_code = GetLastError();
   if (i_time == 0 || error_code > 0) {
      if (error_code > 0 && error_code != 4066)
         continue;      // put any other error handling here
      sleep(2000);
   }
   else {
      LongAzzArray[k] = iClose(ArrayName[ArrayNameIndex], PERIOD_M5, Counter + 1);
      Counter++;
      Comment("k = " + k);
      break;
   }
}

Second, if the value received from iClose() is not what you are expecting, then double check see if you are passing the correct shift (your Counter) for the period which you are trying to retrieve data.

 
pamexx:
I updated the code with a check for the 4066 error but it does not correct the problem. Maybe I am not implementing the error check correctly. The values in the history center are correct but the values that iClose() produces are not correct.

How do you know this to be true ?  how have you checked ?
Reason: