Trader’s Tech – Writing Your Own EA Part 51 – Generic Start Code

Forex RobotIf 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 50 we discussed the include files I use. There are others, but I only include them if I actually use them. One nice thing about MQL4, it will remove unused functions from the compiled code, so you don’t have a lot of “bloat” in your executable. For that reason, I’ve considered just taking all my include functions and putting them into a single include file. Just one more thing I don’t have time to do. 😉 In this installment we’ll discuss the int() function of our Generic Start Code.

I start the init() function with a couple of Print() statements so it’s very clear where the current run of the EA starts in the Experts log. I’ve spent a lot of time trying to figure out where the latest EA run began in the log. It’s a pain. So I just make it very clear with lines and “Initializing” and so on.

   Print("-----",Title," ",Version," Initializing ",Symbol(),"-----");


Next comes the code for calculating the AdjPoint (Adjusted Point) variable. As I’ve said, this replaces the built-in variable Point which relates Points to Price, and relates Pips to Price. As you know, on 4-digit broker’s platforms Points equals Pips, but on 5-digit broker’s platforms, one Point equals 1/10 Pip. This code makes allowance for that. It’s one of the handiest code segments I’ve got. I could probably shorten it, removing the FiveDig variable, but I sometimes use FiveDig in other code.

      FiveDig = 10;
      FiveDig = 1;
   AdjPoint = Point * FiveDig;


Next I simply call the DrawVersion() function which displays the version number in the lower left-hand corner of the chart. I find this very useful to be sure I’m running the latest version of the code on all the charts. Code versioning can be very confusing when you use your EA on several different charts on several different platforms.  I’m always passing EA executables to Casey and Nathan as well, so this eases the confusion considerably.  The reason I  put the Version variable so high up in the code is that I can easily increment it each time I update the code. I use version number 0.xx for alpha and beta test version and 1.xx and higher for production code.

The next code segment is another I use to try to standardize between brokers. Each broker uses a different precision for its lot sizes in trades. I use the LotDigits variable the way I use the Digits variable in the NormalizeDouble() function for trade sizes in orders.  This code looks at the current chart symbol to determine the LotDigits. If you’re using an EA that executes trades on other pairs, be sure to recalculate this variable for the other pairs.

   if(MarketInfo(Symbol(),MODE_LOTSTEP) < 0.1)
      LotDigits = 2;
   else if(MarketInfo(Symbol(),MODE_LOTSTEP) < 1.0)
      LotDigits = 1;
      LotDigits = 0;


The next slice of code is to check in the program start to see if there is a debug GV in place. As I mentioned here, this code can be included as a function that can be called during the regular running of the EA (in start() code) to allow you to start and stop debugging on the fly.

      if(GlobalVariableGet(StringConcatenate(Prefix,"debug")) == 1)
         debug = true;
         debug = false;


And the last piece of init() code is to test for the HeartBeat GV. I use the HeartBeat() function to show in the Experts log that the EA was running during a time when no other activities were taking place. I’ve written EAs that do very little for days – looking for the ultimate position on H4 and Daily charts – and sometimes it’s hard to tell when the EA was actually running and when it wasn’t. The HeartBeat() function prints a “HeartBeat” line in the Experts log every hour (by default) to show that the EA was actually running at that time. It’s saved me a ton of “figuring” when I was trying to find a particularly elusive bug.

That covers the init() function. We’ll continue with the Generic Start Code next time.

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