Trader’s Tech – Writing Your Own EA Part 3 – EA Structure

Forex RobotIn Part 2 of our series we discussed installing MT4 and getting started with the MetaEditor for programming. Now it’s time to interrupt the process of “how things work” to take the most important step. As a trader, you know the importance of having a trading plan. As you a programmer, you will discover that a programming plan is even more vital. As I mentioned, a computer is very precise in it’s syntax, spelling, grammar and punctuation. In addition, you need to spend time thinking about what your program should do and how it will go about doing it. So now we’re going to discuss what steps you should take to identify how your program should work.

There are three events in an MQL4 program for which you need to plan.

1) The Initialization (init()) – this is the program code that will execute whenever the EA is initialized. Initialization occurs whenever the EA restarts. If the EA is part of the default MT4 setup, it will initialize when you start MT4. It will initialize when you add the EA to the chart. It will also initialize whenever you change the time frame of a chart that has the EA running on it.  The initialization portion of the program will set up all the parameters of the program that you will need to have running during the program execution.

2) The Deinitialization (deinit()) – this is the program code that’s used to “clean up” the chart and variables when the EA is shut down. This occurs when you delete the EA from the chart, shut down MT4 or change time frames. This is usually used for clearing objects from the chart and saving information in a file for the next run of the EA.

3) The Start code (start()) – this is the program code that’s executed each time a “tick” comes in from the market. You may have noticed that EAs seem to stall during slow market times. If you have an onscreen candle countdown timer indicator, this is most obvious. Watch it during the slow period between the New York close and the Asia open. You will see that it only updates when a price tick shows. That’s because, due to the structure of MT4, no EA code can be executed when there are no trades taking place. There are workarounds for this problem, but they are beyond the scope of our current discussion. We’ll save that for a more advanced programming course.

Next time, we’ll take a look at a simple trade strategy, see what needs to be put in each of these sections and how we will need to plan the structure of the EA.

Thanks for your attention. Please don’t hesitate to ask questions if you have any. And please follow me on Twitter.

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