FileReadArray issue - page 2

 
Seng Joo Thio:

Apparently the FileReadArray function cannot handle multi-dimensional arrays well. I tried using a single dimensional array, and one FileReadArray is enough to get all the data out. Once I increase to two dimension, the same problem comes. And when I print the error code, it says 4053 - ERR_SOME_ARRAY_ERROR, which is VERY INFORMATIVE... LOL

You can make your code nicer (somewhat) if u repeat the read only when the previous one returns error 4053... which, it appears, is only causing slight inconvenience at the moment.

EXACTLY ! all you just mentioned is exactly what I faced with my own code and I tried numerous times but didn't find any nice and absolute solution for this issue.

As stated earlier in my previous posts, so far the only solution that I find for using "FileReadArray" with dynamic multidimensional arrays properly, is using this functions twice in a row in your code so that in the first call, the dynamic array size is set to the appropriate size based on the data that are saved in the .bin file, then in the second call of the "FileReadArray" function, it fills the array with the data that are taken from the .bin file.

By the way, thank you so much for your time and kind response.

 
lippmaje:
Maybe you should point this out to MetaQuotes so they can fix it.

I'm not still sure whether it is an MT4 bug or there is something wrong with my own code.

 
parham.trader:

I'm not still sure whether it is an MT4 bug or there is something wrong with my own code.

It's an mql4 bug (there is not this problem with mql5), and you work around it correctly. An other way would be to resize the array before using FileReadArray(), you would need to know the dimension or to read it from the file.
 
Alain Verleyen:
It's an mql4 bug (there is not this problem with mql5), and you work around it correctly. An other way would be to resize the array before using FileReadArray(), you would need to know the dimension or to read it from the file.

Thank you so much for your informative guidance,

The solution you suggested would also solve the issue but in that case you need to know the array size and this may not cool because I'm using dynamic array and constantly reduce or increase its size. 

Again -as stated earlier in my previous posts- I think the best way to overcome the bug related to using "FileReadArray" with dynamic multidimensional arrays, is using the FileReadArray function twice in a row in the code so that in the first call, the dynamic array size is set to the appropriate size based on the data that are saved in the .bin file, then in the second call of the "FileReadArray" function, it fills the array with the data that are taken from the .bin file.

All your kind guidance is highly appreciated,

 
parham.trader:

Thank you so much for your informative guidance,

The solution you suggested would also solve the issue but in that case you need to know the array size and this may not cool because I'm using dynamic array and constantly reduce or increase its size. 

Again -as stated earlier in my previous posts- I think the best way to overcome the bug related to using "FileReadArray" with dynamic multidimensional arrays, is using the FileReadArray function twice in a row in the code so that in the first call, the dynamic array size is set to the appropriate size based on the data that are saved in the .bin file, then in the second call of the "FileReadArray" function, it fills the array with the data that are taken from the .bin file.

All your kind guidance is highly appreciated,

The problem of your solution is it relies on a bug. If one day the bug is fixed, and you don't know it to adjust your code, your code will lead to a bug.

The solution I propose has not this problem, and is easy to do. Just save the size of the array in the file, then read it and resize your array before using FileReadArray(). Small annoyance, but you will never have a bug if MT4 is fixed.

 
Alain Verleyen:

The problem of your solution is it relies on a bug. If one day the bug is fixed, and you don't know it to adjust your code, your code will lead to a bug.

The solution I propose has not this problem, and is easy to do. Just save the size of the array in the file, then read it and resize your array before using FileReadArray(). Small annoyance, but you will never have a bug if MT4 is fixed.

You're 100% correct and I admit that your solution is more suitable and more logical than mine but even if the mentioned MT4 bug is fixed in future my solution would still work fine and the only annoying part of my code is that it writes the file contents in the array two time in a row and the second write would overwrite the previous one which should not make any significant problem.

Again I admit that your solution is more logical than mine.

Thank you very very much for your time and kind guidance,

 
parham.trader:

Hi friends,
I'm trying to write some info from an array into a .bin file using FileWriteArray function and then read from that file and copy the file contents into another array using FileReadArray. As I checked the MQL4 documentation, I found out that it is not necessary to define the array size when using FileReadArray and therefore I can use dynamic array when I want to use FileReadArray. The problem is that when I don't define the arraysize of the array and use a dynamic array, the code won't work properly but as soon as I define the array size for the array, it works properly.

Here it is my code:

Your kind guidance is highly appreciated.

Sure, reading files takes quite sometime when it comes to trading... I don't think you have any time to waste, so look at this code...

function run()
{
  set(PARAMETERS);  // generate and use optimized parameters
  StartDate = 20160801;
  TickTime = 100;
  BarPeriod = 5;  // 5 minutes bars
  LookBack = 500;   // maximum time period
 
// calculate the buy/sell signal with optimized parameters
  vars Price = series(price());
  vars Filtered = series(BandPass(Price,optimize(5,1,240,1),0.5));
  vars Signal = series(FisherN(Filtered,500));
  var Threshold = optimize(1,0.5,1.5,0.1);
 
// buy and sell
  Stop  = optimize(100,1,200) * ATR(optimize(100,1,200));
  Trail = optimize(100,1,200) * ATR(optimize(100,1,200));
  if(crossUnder(Signal,-Threshold))
    enterLong();
  else if(crossOver(Signal,Threshold))
    enterShort();
}
Reason: