FileWriteArray() creating empty .bin file

 

I am currently using MT4 version 4.00 build 1260

I am trying to get FileWriteArray to store all the values of an array into a .bin file

The way the script is supposed to work is there is an array called secondsArray[] that stores the actual seconds of the day in a particular week.  Towards the last hour of the week, it should write the array to a file.

Values are assigned to the array via secondsArray[trueSeconds] = trueSeconds;    So that the element number is driven by the corresponding second of the week.  ex: secondsArray[300] outputs 300

At first I suspected that it had something to so there not always being a tick for every seconds, and therefore OnTick() would not fill the array with a value for that particular element.  However, there should be an array being assigned value, regardless.

The .bin files are generated in "/tester/files/" but they come up empty.  Zero bytes.  Upon futher inspection using np.fromfile("seconds.bin") in Python, the shape of the array is verified to come up zero.  

I also verified that there are no 5002 and 5004 errors in writing the file.

If someone could please help me figure out what is wrong with this, it would be greatly appreaciated.  Thank you kindly!

int secondsArray[430201];
string filename;

void OnTick()
{
   datetime clock = TimeCurrent();
   MqlDateTime clockStruct;
   TimeToStruct(clock, clockStruct);
   int trueSeconds = ((clockStruct.day_of_week - 1)*86400)+(clockStruct.hour*3600) + (clockStruct.min*60) + clockStruct.sec;
   
   secondsArray[trueSeconds] = trueSeconds;
   
   if (trueSeconds >= 430200 && trueSeconds < 430220)
   {
      filename = "seconds.bin";
      
      int file_handle=FileOpen(filename,FILE_WRITE|FILE_BIN); 
      if(file_handle!=INVALID_HANDLE) 
      { 
         FileWriteArray(filename,secondsArray, 0, WHOLE_ARRAY); 
         FileClose(file_handle); 
      }
      else
      {
         Print(GetLastError());
      }
   }
}
 

You are using the filename instead of the file handle.

If you had used property strict you would have seen a warning about this.

#property strict

void OnTick()
{
   MqlDateTime clockStruct;
   datetime clock = TimeCurrent(clockStruct);
...
   if(FileWriteArray(fileHandle, secondsArray, 0, WHOLE_ARRAY) != ArraySize(secondsArray))
      Print("could not write all array elements!");

 

thank you, that did the trick!

Now one more problem.  The array is meant to have a shape of 430201 but it is coming out to 215100.  I understand the OnTick() function is driven by new ticks coming in but

nonetheless, shouldn't there be zeroes for secondsArray[x] in moments in time when there are no ticks coming in?

If I ran this same script with secondsArray[trueSeconds] = Bid;  I can see that the array is 430201 but with zeros where there wasn't a tick coming in in that particular moment in time.

 
I solved my own problem.  Since trueSeconds is an integer, if i use np.fromfile("seconds.bin", dtype='int32') then it will have the proper shape by declaring the proper datatype.
Reason: