Trader’s Tech – Writing Your Own EA Part 8 – Program Structure

Forex RobotLast time (Part 7) we discussed pseudo-code and some other programming concepts. Now we’ll jump into structuring our EA.

In our initial version of this EA, we will only put some identifying information in the init() section of the program. Primarily we’ll use the Print() function to put some version information in the experts log.  Not much structure to discuss here yet. In later modifications to the program we’ll add more stuff here.

Also, the deinit() section will be pretty clear as well, since there is nothing we need to save in files and no objects to clear from the screen. Again, in later modifications, we’ll probably have to delete some chart objects when we shut the EA down and this is where we’ll code that stuff.

So, our main discussion will involve the start() section of the program. If you recall, the start() section executes each time a market tick comes in for the pair in question. As we set this up and make decisions about what should take place, we have to think in terms of what needs to happen whenever a tick comes in.

Now, on to the program structure. First decision, do we want to allow multiple positions and hedging in our EA. Well, for the purposes of the simplicity of this EA, and the fact that many of our readers are using US-based platforms that won’t allow hedging, we’re only going to allow a single trade to be open in any single pair.  So the first thing we need to do when a tick comes in is to determine if there is an open position already on this pair. We’ll want to use a separate function that returns a bool (a boolean variable is a one that allows a true or false – specified in MQL4 as bool) to do this, so for the start section we’ll only need to use the function name and we’ll define the function later (that’s my way of doing pseudo-code.) So our start() section will begin to shape up like this:


Now a brief explanation. The if keyword must be in lower case and must be followed by a boolean expression surrounded by parentheses. A boolean expression is one that would return a true or false response. It can be a function (a function is specified by the open/close parentheses following it), as in this case, or it can be a simple formulaic expression such as:

a > b

What this formula says is: a is greater than b – if a is bigger than b this will return true, if a is smaller than b it will return false.

If the expression contained in the parentheses following the if are true, then the program code contained in the curly braces will be executed. If the expression is false, the code contained in the curly braces will be skipped.

So the beginning of our program says if this function called IsThereAnOpenTrade() returns a true, then the code in the curly braces will be executed, otherwise it will not be executed. It’s as simple as that. For now, we don’t have to figure out how to determine if there is a trade open – we’ll tackle that a little later. So, this function name acts as a place holder in our program structure and also reminds us what information the place holder must return.

One other small detail about syntax and structure. Programmers indent code that is dependent upon a loop or conditional statement (like if.) As I mentioned earlier, every programmer has their opinion about spacing, indentation, where to put the curly braces, how much to comment, etc. Unless a customer asks for it to be done a certain way, I usually use four spaces for my indentations and I put the curly braces on separate lines indented with the code it contains. Unless the people you are coding for/with have a certain specification for this, use what is most convenient for yourself. I just find it much easier to identify code groups this way. I will also put a lot of horizontal spacing in my code. We call this white-spacing. White spaces are ignored by your compiler – just like comments. To me the more white space there is (within reason) the easier the code is to decipher, so I use a lot of it.

Wow! I’m way over again today. OK. I think I’ve covered most of the trivial syntax sorta stuff, so we can add to our pseudo-code tomorrow. Thanks for following along. And don’t hesitate to comment/question anything I do. If you’re a programmer, I don’t mind a nudge if I screw up. 🙂 I’m not infallible. If I was, I wouldn’t depend so much on inline debuggers. 😉

And follow me on Twitter. And here’s a link to the beginning of the Programming EAs Series if you’re jumping into the middle.





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

  • Thanks Oriol.

  • oriol papiol

    Thanks a lot, so well explained…XD