Trader’s Tech – Writing Your Own EA Part 93 – Error Trapping

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 92, we began a discussion of Error Trapping. We’ll continue that discussion in this installment.

As I’ve said, error trapping must take place in all the areas you would anticipate having errors. One of the primary areas with potential problems would be in file reading and writing. These are fairly straightforward to handle.

Firstly, when you open a file to read or write, check the returned file handle:

int h;
string FileName = "filename.bin";

h = FileOpen(FileName,FILE_BIN|FILE_WRITE);

if(h != -1)
   //File Write Functions
   }  //if(h != -1)
   Alert("Error Generated While Opening File. Error Code=",GetLastError());


We simply check the returned file handle to see if it’s -1. If so, there was an error in opening the file and we can alert the user to the problem (or, if feasible, handle it automatically.) If you want to go to great detail, you can do the same with each of the File Write functions as well. Add this declaration to the top of the function:

int fw;


Then put the following under //File Write Functions:

   fw = FileWriteIntegers(h,FileVersion);
   if(fw == -1)
      Print("Error writing file element: FileVersion=",FileVersion);


Although I usually just do this:



Because it’s very unusual for a file to open properly and not allow proper writing of the elements of the file.  It’s never created a problem for me to do things that way and saves quite a bit of coding.

Reading the file is similar to writing. The difference is I usually just rewrite the file if I get an error opening the file for reading. Just remember that doing that will erase any data that may already be there. If your file cannot be safely overwritten (has sensitive data) then just generate an alert condition if it can’t be opened for reading like we did for the WriteFile() function above.. Also, you may recall that I version control my data files so the file is automatically rewritten if the version of the file doesn’t agree with the file version set in the EA’s header:

void ReadFile()
   int h;
   int FVersion;

   Print("*** Reading Trade File ",FileName," ***");
   h =  FileOpen(FileName,FILE_BIN|FILE_READ);

   if(h != -1)
      FVersion = FileReadInteger(h);
      if(FVersion == TradeFileVersion)
         TradeStatus = FileReadInteger(h);
         LineStatus = FileReadInteger(h);
         //Read Additional program lines

         }  //if(FVersion == TradeFileVersion)
      }  //if(Handle != -1)
   }  //void ReadTradeFile()


I test the file handle for a -1. If it’s not -1, I proceed to read the file version. It the handle is -1, I call the WriteFile() function to rewrite the file.

If the file versions match, I proceed to read the rest of the file elements. If they don’t match, I call the WriteFile() function to rewrite the file.

That covers most of the error trapping issues with file reading and writing.  Next installment we’ll talk more about handling order error trapping. 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