Trader’s Tech – Writing Your Own EA Part 58 – 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 57 we talked about the ReadFile() and WriteFile() functions, more specifically we discussed opening files. In this installment we’ll talk more about file handling.

Picking up where we left off, after opening the file, we test h (our file handle) for a -1 signifying a FileOpen() error. If h is not equal to -1, we proceed to read our data from the file. We will use an else statement below to handle the h == -1 situation.

And now I notice an issue with my code. This may work, but it could generate an error – which I like to avoid. 😉  Allow me to explain. The first variable we read in the file is the Data File Version number – we assign it to integer DFVersion. Then we compare it to DataFileVersion, the global (not GV) variable containing the most recent version number for the data file. This is assigned at the top of the EA and is updated when we change the data file. As I mentioned, this will make it much easier to do updates on the EA without having to replace all the old data files. If DFVersion doesn’t agree with DataFileVersion, we assume we have an old data file and set BadFile = true. That’s great, but then we continue to read the bad data file. If we have changed the variables or the order of the variables, we could get an error (for example, trying to read double data as a string or something.) At best, the data is meaningless and we have wasted precious processor/hard drive time reading a meaningless file. So I propose we skip all this reading when the version numbers don’t agree. An added “benefit” is that we can do away with the boolean variable BadFile.  Here is the updated ReadFile() function:

bool ReadFile()
   int h;
   int DFVersion;
   bool Success = false;

   Print("*** Reading Data File ",DataFileName," ***");
   h = FileOpen(DataFileName,FILE_BIN|FILE_READ);

   if(h != -1)
      //Data File Version
      DFVersion = FileReadInteger(h);
      if(DFVersion == DataFileVersion)
         //integer PairStatus
         PairStatus = FileReadInteger(h);

         //double LastPrice
         LastPrice = FileReadDouble(h);

         //boolean TradeLockedIn
         TradeLockedIn = FileReadInteger(h,1);

         //string PairName 
         PairName = FileReadString(h);

         //dateTime TradeTime
         TradeTime = FileReadInteger(h,4);

         Success = true;
         }  //if(DFVersion == DataFileVersion)
         Success = WriteFile();
      } //if(Handle != -1)
      Success = WriteFile();

   } //void ReadTradeFile()

You can download the updated FileHandling.mqh file here.

Now that we’ve got that fixed, I can proceed with my explanation. I hope everyone “gets” what’s going on with the data file version number. These are the sort of automation features I like to add to my EAs that make my life easier. No point in manually tracking down a bunch of old data files. Isn’t that what computers are supposed to do?

Reading integers, doubles and strings are all very straightforward. Use the appropriate function (FileReadInteger(), FileReadDouble(), FileReadString()), include the file handle as the parameter (now you know why I made the file handle variable short – you’re going to have to type it a lot) and the function returns the next value in the file. Remember your File Reads and your File Writes must be in the same order. This is just like a line at the grocery store. The first one always comes first, second always comes second and so on. You can’t change the file when you read it  simply by changing the order of the variables, it must first be changed when you write it.

The FileReadInteger() function also has a second parameter allowing you to specify the length of the integer that you are reading. Again, this must agree with the value used in the WriteFile() function for each particular integer. The allowable values are 1, 2 and 4, with 4 being the default (if you do not specify.) I usually specify the file length of integers used to store datetime- and bool-type variables. We’ll discuss all of that further when we get to the WriteFile() function.

Let’s wrap it up right here for today and continue with the ReadFile() function in our next installment. Thanks for your attention and please follow me on Twitter and LinkedIn.




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