Trader’s Tech – Writing Your Own EA Part 57 – 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 56 we talked about the variables I use to control the ReadFile() and WriteFile() functions in my EAs. This time we’ll look at the actual functions.

As I mentioned, the actual data saved in these functions will change based upon your needs. I’ve tried to include each type of variable in this code so you can see how to handle them.

 

bool ReadFile()
   {
   int h;
   int DFVersion;
   bool BadFile = false;
   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)
         BadFile = true;

      //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);

      FileClose(h);

      if(BadFile == true)
         Success = WriteFile(); 
      else
         Success = true;
      } //if(Handle != -1)
   else
      Success = WriteFile();

   return(Success);
   } //void ReadTradeFile()
bool WriteFile()
   {
   int h;
   bool Success = false;

   Print("***** Writing Data File ",DataFileName," ***");

   h = FileOpen(DataFileName,FILE_BIN|FILE_WRITE);

   if(h != -1)
      {
      //Data File Version
      FileWriteInteger(h,DataFileVersion);

      //integer PairStatus
      FileWriteInteger(h,PairStatus);

      //double LastPrice
      FileWriteDouble(h,LastPrice);

      //boolean TradeLockedIn 
       FileWriteInteger(h,TradeLockedIn,1);

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

      //datetime TradeTime
      FileWriteInteger(h,TradeTime,4);

      FileClose(h);
      Success = true;
      } //if(Handle != -1)
   else
      Alert("Error Writing Data File ",DataFileName," Error:",GetLastError());

   return(Success);
   } //void WriteTradeFile()

 

You can download this code from my GitHub site here.

In the ReadFile() function we start by declaring an integer variable called h (for handle, see what I did there? ;)) to store the File Handle integer assigned by the FileOpen() function. Later on it will be obvious why I use a single character variable name here. We also declare DFVersion to retrieve from the file and compare to the current Data File Version. We also initialize two bool variables BadFile and Success as false. We’ll use BadFile to determine if our file has an old file version and we’ll use Success to pass the results of ReadFile() to the calling function.

Next we Print() a line to the Experts Log to document the ReadFile() call. This doesn’t cause a performance problem for me since I usually only call ReadFile() at the beginning of the EA. If you use it more often, you may want to consider whether it will clog up the Experts log or cause performance hits on your platform.  If in doubt you can remove the line (by preceding the line with remark slashes //).

Finally, we come to the FileOpen() function. This function requires two parameters. The physical file name and two constants separated by a pipe character (|). If you are opening a CSV file, you can also assign the separator character as an optional third parameter. In our case, the physical file name was assigned to the string variable DataFileName. MT4 will not assign a file extension to the name unless you provide one in the string. As I mentioned, I usually use .bin for binary files and .csv for CSV files – Windows standards. You can assign anything that makes sense to you. Remember the file will be put in the %MT4%/experts/files folder. This is one of those times when you will be glad you installed MT4 in a folder other than the default Program Files folder. If you did that and you’re using a later version of Windows, you will have to track your file down in your Virtual Store folders.

The constant before the pipe character is the file type. Your options are FILE_CSV or FILE_BIN. Since we are opening a Binary file, we’ll use FILE_BIN. The constant after the pipe is the file mode. There are various combinations that you can study in the documentation. We will only concern ourselves with FILE_WRITE and FILE_READ. Since this is the ReadFile() function, I’ll be you can guess we’ll use FILE_READ. 😉 That may be obvious, but in some situations, we may use a combination. For the purposes of this discussion, we’ll stick with the straightforward code.

If our FileOpen() is successful, it will return the relevant file handle which we will assign to h for use in the FileReadxx() functions. If the FileOpen() generated an error, the function will return a file handle of -1. We can then return the error code using the function GetLastError(). Since this code is only a demonstration of how to store data so it survives a crash of the EA and to maintain simplicity we will not deal with error codes, we’ll just create a new file if an error occurs.  A more complex program may require handling of the error codes.

Well, that’s a wrap for today. We’ll continue our discussion of ReadFile() next time. 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.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news