In part 5 of our series, we wrapped up the discussion of planning External Variables. That’s only the beginning of the planning of our EA. The next step is to figure out what it is we want the EA to do and plan the structure in light of how MT4 will view our EA.
First, a brief discussion of functions. A function is a section of code or routine that is held in a “black box” allowing that section of code to be reused in several places in the program by referencing its name and passing variables to it. A function is designated with a name followed by open and close parentheses. If, in your function definition, you have designated parameters that must be passed to the function, they will be contained, in order, within the parentheses. Additionally, a function can return a value if it is defined to do so. Using functions simplifies your coding by allowing you to define a certain functionality and call it within your code.
As we discussed in part 3 of our series, MQL4 specifies three special functions that must exist:
1) init() or Initialization which is executed each time the EA is started (including chart time frame changes.)
2) deinit() or De-Initialization which is executed each time the EA is stopped.
3) start() which is executed each time a market tick for the current pair is received.
In addition, there are functions described in the MQL4 library that allow you to do common activities, like finding the high of the current 5 minute bar in your chart
HighPrice = iHigh(Symbol(),5,0).
Or the current value of the 20-period RSI indicator on the 60 minute chart calculated on the close of each bar
RSI = iRSI(Symbol(),60,20,PRICE_CLOSE,0).
We’ll go into more detail about how to define and use functions as we proceed with our project.
Experienced programmers will notice that MQL4 is very similar to the C programming language in structure – allowing many of C’s interesting constructs [i++ to increment, a+=b to add and specify a result and so on.])
It looks like we have once again run out of space. In our next installment, we’ll discuss what needs to be included in the init() section of our program. For simplicity’s sake (in case you haven’t figured it out, I like simplicity), we’ll limit our program to it’s pure functionality. In actual practice, I include a lot of information in the init() section to assist in debugging (finding mistakes in program logic) more complex programs – since MT4 doesn’t have an inline debugger (helps you find program “bugs” while the program is running, a common functionality in programming environments since the 80s) – but that’s a rant for another day. We’ll save our discussion of debugging for later in the series.
Once again, thanks for your attention. Please don’t hesitate to ask your questions. I’ll answer them as promptly as I possibly can. And follow me on Twitter.
Winner’s Edge Trading, as seen on: