Trader’s Tech – Writing Your Own EA Part 54 – 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 53 we looked at the DrawVersion() function and touched on some of MQL4’s Object functions. In this installment, we’ll take a look at the HeartBeat() function.

In Parts 47 & 48 we discussed MQL4’s Global Variables (GVs) and the GetGV() and SetGV() functions so I won’t go into detail regarding these functions except to mention that I include them in my Generic Start Code. As I’ve said, any functions that you use regularly can be included in the start code. If you don’t actually use it in your current EA, the MT4 compiler won’t include it in the compiled code, so you won’t be contributing to executable “bloat”.

As I’ve also mentioned earlier, I use a function called HeartBeat() to let me know when the EA is running. I created it as a result of a few support issues where the customer sent me copies of the Experts log and told me the EA was not taking trades when it was supposed to be taking them. Well, hindsight is difficult if there isn’t enough evidence to say one way or another whether the EA was doing what it was supposed to do or even whether the EA was running during the time of the alleged trade opportunity. The experts logs will tell you every time the EA is initialized and de-initialized – if you have complete experts logs. My solution was the HeartBeat() function. It creates a log entry every hour (or whatever time frame you choose) letting you know the EA was running and on which pairs it was running during those questionable times. It was a real time and trouble saver. It’s one of the few “debug” features that I leave operational in production versions of my EAs.

void HeartBeat(int TimeFrame=PERIOD_H1)
   {
   static datetime LastHeartBeat;
   datetime CurrentTime;

   if(GlobalVariableCheck(StringConcatenate(Prefix,"HeartBeat")))
      {
      if(GlobalVariableGet(StringConcatenate(Prefix,"HeartBeat")) == 1)
         HeartBeat = true;
      else
         HeartBeat = false;
   }  //void HeartBeat(int TimeFrame=PERIOD_H1)

   if(HeartBeat)
      { 
      CurrentTime = iTime(NULL,TimeFrame,0);
      if(CurrentTime > LastHeartBeat)
         {
         Print(Version," HeartBeat ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));
         LastHeartBeat = CurrentTime;
         } //if(CurrentTime > ...
      } //if(HeartBeat)

   } //HeartBeat()

 

As you can see, the HeartBeat() function includes a single parameter, the time frame – using the standard MQL4 constants for time frame – and defaults to hourly. We then declare a static datetime variable, LastHeartBeat, to remember the bar time of the last HeartBeat. Remember using the static keyword causes value of the variable to be retained between calls of the function. The LastHeartBeat variable will not be retained through de-initialization of the EA, so the heart beat will print each time the EA starts.

The next thing we do is check for a GV controlling the HeartBeat, just like we do in the init() function. If it exists, or the global variable HeartBeat is true, we set the variable CurrentTime to the current bar time for the specified time frame using the iTime() function and compare it to the LastHeartBeat time. If it’s time, we print the HeartBeat line in the Experts log and set the LastHeartBeat to CurrentTime and that sets us up for the next HeartBeat.

Just a brief comment about the log entry. A datetime variable is a long integer showing the number of seconds elapsed since 01/01/1970. Since that number is pretty meaningless for our log, we use the TimeToStr() function to convert the time to a meaningful string.  The constants TIME_DATE and TIME_MINUTES tell the function what format to use for the string.

And that wraps another riveting episode of Writing Your Own EA. 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