How to save dynamic array to file

 

Hello guys

My ea creates 2 dimensional structure dynamic array while it is running.

How I can save this array into a file and read this file OnInit to recover array, if ea removed and added to terminal

 
Narek Kamalyan: My ea creates 2 dimensional structure dynamic array while it is running. How I can save this array into a file and read this file OnInit to recover array, if ea removed and added to terminal
Please read the documentation on file operations, namely FileOpen, FileWrite, FileClose, etc. and specific for your needs, FileWriteArray and FileReadArray.
Documentation on MQL5: File Functions / FileWriteArray
Documentation on MQL5: File Functions / FileWriteArray
  • www.mql5.com
FileWriteArray - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Thx Fernando
I have seen this article. But my array contains also string along with integer double and date time. The article says it cannot be used for string arrays. 
 
Narek Kamalyan: I have seen this article. But my array contains also string along with integer double and date time. The article says it cannot be used for string arrays. 

Then write your own Write (or Read) function, that loops through the array elements and writes (or reads) them individually. Then just reuse that function in the rest of your code.

Coding is about creating "recipies" for your code problems. If the function does not exist, you create it. It is all about problem solving by dividing the problems into smaller chunks and creating mini solutions for them. Then combining it all in the overall "recipe".

 
Can I use txt to write string int and double, all together in a single file?
 
Narek Kamalyan: Can I use txt to write string int and double, all together in a single file?

You can, but it will not be as efficient, both in disk usage and CPU usage as there will be a "binary to text" and a "text to binary" conversion taking place respectively for writing and reading.

EDIT: An example of what you are suggesting is the typical CSV files that can easily be read by most Spreadsheet applications. So there is an advantage to that but it is more resource heavy.

As long as you handle it properly, there is no problem mixing binary data (for ints and doubles) and text data for strings in the same file. As longer as it is properly structured, either as fixed field size for strings or by using dynamic length string text (using a size header or NULL ending).

It is all about following a logical and reproducible "recipe" for the handling of the data.

 
Another option is to encode your array into a JSON and write/read this from/to file.

https://www.mql5.com/de/code/13663


 
EDIT: An example of what you are suggesting is the typical CSV files that can easily be read by most Spreadsheet applications. So there is an advantage to that but it is more resource heavy.
In my case dynamic array is not going to exceed 1000 elements (100 indexes in the first dimension). What you think is it going to have significant impact on the reading/writing time?
Is there any way to approximate how long it may take. 
 
Dominik Egert:
Another option is to encode your array into a JSON and write/read this from/to file.
Thanks Dominik. 
JSON file is reading/writing is more efficient type compared to CSV or txt?
 
Narek Kamalyan: In my case dynamic array is not going to exceed 1000 elements (100 indexes in the first dimension). What you think is it going to have significant impact on the reading/writing time?
Is there any way to approximate how long it may take. 

Instead of asking, experiment for yourself and find out! The best way to learn is by gaining the experience instead of asking others to provide you every single step for all of your queries. Learn by doing! Even if you do something wrong, you learn from the error and become a better coder for it.

 
Narek Kamalyan:
Thanks Dominik. 
JSON file is reading/writing is more efficient type compared to CSV or txt?

Here is a code snippet to find out how fast a code is executed.

const ulong perf_start = GetMicrosecondCount();

/* Here goes your code to be examined */

printf("Execution time: %llu microseconds.", GetMicrosecondCount() - perf_start);

If the result sis NULL then your code is executed below one millionth of a second, which is very possible. If this happens, you should include your code in a loop to iterate the code a multiple of 10, this way you can still measure the execution time. and divide it by the iterations used.

Results mya vary, depending on your systems load and the ability of the operating system to assign ressources to your thread/EA.

Hope this helps.

BTW: Why not look into the sources and see yourself how the developer has solved reading/writing a file. At least this way you can make a conclusion on the options in use to perform the task.