All MQL expert advisors and indicators contain a few essential components. The general organization of MQL programs does not vary too often.
Files usually start with a declaration of #defines (pronounced pound define) global variables and external variables, also known as an extern data type. They appear near the top of the code to help the read gain an understanding of the variables that will run in the program. Ideally, the names of the variables and how they are organized should assist the programmer with form a general understanding of what the expert advisor or indicator might do.
The next section is usually the init() function, which is the word initialize abbreviated. This section of the code is particularly relevant to programming custom indicators. Most of the general indicator settings like declaring the indicator buffers, the colors to use and other basic features are set within this section. I use init() in every expert advisor that we build to convert the inputs into an appropriate setting for the broker’s pricing. If a client inputs a stop loss of 50 into an EA, I don’t need to do anything if it’s a 4 digit broker. I do, however, need to convert the input to work with a 5 digit broker. I run a quick check within init() to see if Digits == 3 || Digits == 5. If so, then I multiply inputs affected by that setting by 10.
deinit() is the least important section; it’s pretty easy to deinitialize an MQL file because it usually does not take up any system resources. It’s rarely used for anything important. The only uses that I ever have for deinit() are to close an open file handle or to make some sort of closing note. This is often done either on the chart directly through the Comment() function or more often by writing directly into the log file.
The start() function is the real meat of an MQL expert advisor or indicator. Whenever MetaTrader detects an incoming tick, it alerts any MQL programs. Those programs then call the start function so that it can do whatever needs doing. All trading operations, calculations, account monitoring, etc, occur within this section.
All of the other custom functions within the program appear below start(). I usually prefer to rank them in order of their importance or the frequency with which I call them throughout the program. The order of placement of functions does not affect performance in any way at all. It’s strictly a cosmetic practice that makes programming code more legible.