Trader’s Tech – Writing Your Own EA Part 55 – 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 54 we covered the HeartBeat() function. Today we’ll wrap our discussion of the Generic Start Code with a look at the start() function.

I usually like to keep the start() function simple, no complex code, just a simple outline-like structure of the program itself. Then I put the more complex code into functions called from the start() function. The only code I put there in our generic start code is a conditional call to the HeartBeat() function.

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
   {
  //----
   if(HeartBeat)
      HeartBeat();
  //----
   return(0);
   }  //int start()
//+------------------------------------------------------------------+

 

In looking at this code now, I see a problem.  So, here is a little test. What problem do you see here?

.

.

.

.

Spoilers ahead: One of our goals as MQL4 programmers is to reduce the code executed on each market tick. As you know, the start() function code will be executed on each market tick, so we want to reduce that. In the interest of reducing executed code, I got overzealous and tested the boolean HeartBeat before calling the HeartBeat() code. But one of the features of HeartBeat() is to switch the HeartBeat feature on using a Global Variable. If the boolean HeartBeat is false, the code to test for the GV is never executed and therefore will never turn the HeartBeat feature on.

Actually, the reason this happened is I decided to combine code without thinking through the consequences. I added the GV test to the HeartBeat() function (I used to keep them separate) thinking that would be more efficient and organized. So, for now, the solution is to remove the test of the boolean HeartBeat before executing the HeartBeat() function.

So here is our corrected start() function:

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
   {
  //----
   HeartBeat();
  //----
   return(0);
   }  //int start()
//+------------------------------------------------------------------+

 

This, of course, will cause the code to look at the GV every single market tick. That shouldn’t be a problem if you don’t have much complex code executing each time. But if your EA is complex, you may want to forego the ability to turn the HeartBeat on and off with a GV. You can still use an external variable to turn your HeartBeat on and restart your EA. Sometimes there are trade-offs in the name of program efficiency. Like I said, Art not Science.

Of course, if your EA only requires code to be executed at the close of a bar, you can test the bar times and execute the HeartBeat() function on the opening of a new bar. Just remember though, if you have trades with stop losses and take profits that may be executed by the broker, they may trigger in the middle of your bar. If you then have code that must execute on the close of a trade, you will have to execute that code on each market tick to be sure you don’t miss the close. We’ll talk about how to detect a triggered SL or TP in a future installment.

That wraps us up for today. 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

  • Great idea Dave. Thanks for the input!

    Tim

  • Dave Hanna

    Or you could test (and change) the value of HeartBeat (and also Debug) only on the opening of a NewBar. It means you might have to wait a little while for a change to the global variables to take effect, but it would cut down on checking them on every tick.