In Part 58 we did a little rewrite of our ReadFile() function and looked at MQL4’s various FileReadxx() functions. In this installment we’ll finish up our discussion of the ReadFile() function and begin with the WriteFile() function.
We wrapped the last article talking about the FileReadxx() functions. Our next task is to close the file. The FileClose() function is very important. MT4 will not close the file automatically and limits the total number of open files to 32. You will be opening the file to write quite a bit and it won’t take long to hit the limit. Then…crash. So don’t forget to close your file when you’re done.
Since we’ve gotten this far without error, it’s time we set our boolean variable Success to true. If we actually passed the DFVersion test and read the file, then the function will pass the two else statements and return true to the calling function to designate the success of ReadFile().
Or course, if either of our if() statements failed; if the FileOpen() function had returned a handle of -1 signifying a failure to open the file or the DFVersion didn’t match the DataFileVersion, the success of our ReadFile() function will depend upon the success of the WriteFile() function which will be called in one of the else statements.
We start the WriteFile() function by declaring the integer variable h as our file handle and the boolean variable Success, initializing it to false. Next we’ll Print() a line to the Experts log to record the WriteFile() action. I call the WriteFile() function quite a bit – just about every change of a variable value – so most of the time I remark out the Print() line so the Experts log doesn’t get too cumbersome.
You may have noticed that I said I call the WriteFile() function a lot. It’s purpose is to be sure the data survives across a chart time frame change, an EA crash, an MT4 crash or a computer crash. When a chart time frame change occurs (or some other orderly shut down of the EA), the deinit() function will be executed, so including WriteFile() in that function will ensure your data survives over orderly shutdown events. But the very unpredictability of the other events suggests you should save your data often. I guess that goes back to the old “save early, save often” maxim. Depending on how much you’re trying to do with your EA and how many pairs on which you will be running the EA, you may have to find a balance between protecting your data and protecting your processor/hard drive performance. It may be that some of your data can be reconstructed after a crash. You have to decide where the trade-offs will take place. That’s the science part of programming.
At this point it’s time to open the file. We use the FILE_BIN|FILE_WRITE mode to open for writing. The existing file is overwritten. If the WriteFile() function was called by ReadFile() because an error occurred or the DFVersion didn’t match, the old file will be overwritten by the new file which should correct those situations. Once again, the FileOpen() function returns a file handle which we assign to the integer variable h. If there is an error opening the file, it will return -1.
We test h for -1 before attempting to read any data. If h == -1 we use the Alert() function, which pops up a window and plays a VERY annoying sound to alert the operator that a nasty error has occurred – I’ve learned to hate that sound as has anyone that uses MT4.
We’ll wrap it up right here today. Next time we’ll discuss the functions that write the actual data to the file. Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- The Secret of Taking Profit and Why it is Important - September 20, 2017
- Fading the Momentum in Forex Trading - September 14, 2017
- Trend Line Drawing With Fractals - September 8, 2017
Winner’s Edge Trading, as seen on: