MQL4 Exercises Functions
MQL4 Exercises Functions
Void Functions
1. TerminalClose() is a built-in function that sets a flag for the exit of the trading platform. This
function takes a single integer argument. Create a script that makes a single call to this
function, with an argument value of 0.
2. Create a script that is similar to the Hello World script. However, this time, rather than using
the Print() function, Use the Comment() built-in function instead.
3. Create a new version of the script described in the 1 st exercise in this section. This time,
however, modify the script so it would only call the TerminalClose() function after 10 seconds.
Use the Sleep() built-in function to accomplish this.
Hint: The Sleep() function takes a single argument of type integer, expressed in milliseconds.
4. Create a new version of the script described in the 3 rd exercise in this section. This time,
however, the statements within the OnStart() function should be moved to a user-defined
function. Give the function a name of "terminal_close" (without quotes). Then, make a single
call of the function within the OnStart() handler.
5. Create a new version of the script described in the 4 th exercise in this section, but this time,
as an expert advisor. Make a function call to the user-defined function terminal_close within
the OnDeinit() handler.
Recommended: Observe what happens after removing the expert advisor from the chart.
1. Create a new script. Within the OnStart() function, declare a integer variable named
account_number. Initialize this variable to the return value of the AccountNumber() built-in
function. Then, print the account number using the Print() function.
2. Create a new version of the Hello World script. Replace the Print function with PlaySound,
another built-in function. Rather than using the argument "Hello world!", replace it with
"alert.wav". Store the output of the PlaySound() function in a boolean variable named
"play_sound" (without the double quotes).
3. Create a new script. Declare a datetime variable named "current_time" (without the quotes)
and initialize it to the return value of the TimeCurrent() function. Print the value of the
variable.
4. Create a new expert advisor. Within the OnTick() function, declare an integer named
"random" (without quotes). Initialize this integer variable with the return value of the
MathRand() built-in function. On the next statement, print the value of the variable.
Recommended: See whether or not the expert advisor is printing random numbers every tick.
Note: You may wonder where all the built-in functions come from. These are all described in
the documentation. We will start using it once we go to more advanced lessons.