Trader’s Tech – Writing Your Own EA Part 61 – Reading and Writing Files

Forex Robot

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 60 we continued our discussion of the WriteFile() function. We’ll be wrapping that up in this installment.

We talked about the FileWritexx() functions and how to use them to write each individual variable to your file. All that’s left now is to FileClose() your file. Again, don’t forget to close your file as MT4 limits the total number of files you can have open at one time and each time your forget to close a file, it is counted toward that total. If you call the WriteFile() function as much as I do, your EA will soon crash with too many open files.

After closing the file, we set the variable Success to true to be returned to the calling program – indicating the successful completion of the WriteFile() function. As I mentioned earlier, if your ReadFile() function returned an error while opening or the DFVersion number in the file didn’t agree with the code’s DataFileVersion variable – suggesting an old, incompatible data file – the WriteFile() function may have been called from the ReadFile() function. In that case, a successful return from WriteFile() to ReadFile() will cause the Success variable in ReadFile() to be set and will return a successful completion to that calling function. Whew! That’s convoluted. It may be obvious to some readers, but for those who don’t quite get it, here’s the code to which I’m referring:

         FileClose(h);
         Success = true;
         } //if(DFVersion == DataFileVersion)
      else
         Success = WriteFile();
      } //if(h != -1)
   else
      Success = WriteFile();

   return(Success);

 

This is the last few lines of the ReadFile() function.  Notice that if h == -1 or the DFVersion != DataFileVersion, we use the line Success = WriteFile(). That sets the variable Success in ReadFile() to the returned result of WriteFile(). So a successful conclusion of WriteFile() results in a successful conclusion of ReadFile().

While we’re here, I may have not covered the simple, but unconventional notation that is used in MQL4. It comes straight from the C programming language, so I’ve been doing it for so long, I no longer think about it. When testing equalities using the if() function, MQL4 uses the double equal sign (==) to test for “is equal to” and the exclamation point/equal sign (!=) to test for “is not equal to”. So the following:

if(h != -1)

is the equivalent of saying “If h is not equal to -1”. And

if(DFVersion == DataFileVersion)

is the equivalent of saying “If DFVersion is equal to DataFileVersion)”.

I hope that clears it up for anyone that may have been confused. Please don’t hesitate to ask questions if you have any.

That wraps our discussion of the ReadFile() and WriteFile() functions. We’ll be discussing file handling more in a future installment, but in the next article we’ll be discussing Event Detection in MT4.
Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Connect_With_Winners_Edge

Get trade set ups everyday!

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news