Trader’s Tech – Writing Your Own EA Part 60 – 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 59 we finished the discussion of the ReadFile() function and began discussion of the WriteFile() function. This time we’ll continue with WriteFile().

After testing to be sure h is not equal to -1, we can begin writing our variables to the file. It may be obvious to some, but I want to stress here that the order in which you read the variables must be EXACTLY the same as the order in which you write the variables, otherwise you’re going to have problems. So bench check your ReadFile() and WriteFile() functions and be sure they match. In fact, what I usually do is write one of the two functions, then copy/paste that code into the other one and modify it.

Most of the FileWritexx() functions have two parameters, the file handle (h in our case) and the variable to be written to the file.  In this case, we have two exceptions. As before, the FileWriteInteger() function has a third parameter representing the length of the integer in bytes (1, 2 or 4.) Again, when not specified, the integer length defaults to 4-bytes. Additionally, the FileWriteString() function has a third parameter representing the length of the string in bytes. For the string length parameter, it’s a simple matter to use the built-in MQL4 function StringLen() to return the length of the string you are writing:

FileWriteString(h,PairName,StringLen(PairName));

 

Where pairname is the string you are writing to the file and h is the handle of the file.

As before this may be obvious to some of our readers, but just for clarity I want to point out that I have been (in this and prior articles) lumping all the individual variable file writing functions into one name FileWritexx(). There isn’t actually a function with that name, the xx is meant to take the place of the words Integer, Double or String. The same applies for the individual variable file reading functions, FileReadxx().

In a prior installment I mentioned the special cases of boolean and datetime. An additional variable type that I haven’t menioned yet is color. All these variable types are actually special types of integers. According to this document, which I researched specifically for this article, all these variables are stored as 4-byte integers. I’ve stored boolean type variables in a single byte integer in the past, but after reading this document it may be best to use the default 4-byte integer for all these variables – which brings us to another change in our code. Change:

TradeLockedIn = FileReadInteger(h,1);

 

and

FileWriteInteger(h,TradeLockedIn,1);

 

to:

TradeLockedIn = FileReadInteger(h);

 

and

FileWriteInteger(h,TradeLockedIn);

 

I recommend following the official documentation when writing code because future changes to the language may assume that these variables and functions are being used as documented. That will reduce confusion when updating code to conform to future language updates. Plus, as I’ve said before, in six months you will have no idea what was in your mind as you wrote the code, so making the code conform to standards will make future updates to your EA much easier.

In any case, booleans, datetimes and colors are simply integers and can be stored as such in your file. I would just use the default integer length of 4 bytes to store them.

And that brings us to a stopping point for this week. We’ll continue this next week. 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