Control structures guide the flow of a program. They determine the sequence in which statements are executed. In this lesson, we'll explore the most common control structures in MQL4: loops and conditional statements.
Conditional statements allow you to execute certain blocks of code based on whether a condition is true or false.
if
Statement
if(condition) {
// Code to be executed if the condition is true
}
if-else
Statement
if(condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
switch
StatementThe switch
statement in MQL4 allows for executing different code blocks based on the value of a variable. It's an efficient alternative to multiple if-else
conditions.
Use switch when you need to compare a single variable against a series of distinct constant values... Use if-else when you have more complex conditions, like range checks, comparisons between different variables, logical operations (AND, OR), or when your conditions are not just about checking a variable against constant values.
int orderType = OrderType(); // Get the type of the currently selected order
switch(orderType) {
case OP_BUY:
Print("Handling a Buy order");
break;
case OP_SELL:
Print("Handling a Sell order");
break;
default:
Print("Order type is not Buy or Sell");
}
The for
loop is a common control structure in programming used to repeat a block of code multiple times. Let's break down a simple example to understand how it works, especially for those new to programming.
for
Loopfor(int i = 0; i < 10; i++) {
total += Close[i];
}
i
and setting its initial value to 0. This counter will keep track of how many times the loop has run.i
is less than 10.i
is increased by 1. This is written as i++
, which is a shorthand way of writing i = i + 1
.{ }
is what gets executed each time the loop runs. In this case, total += Close[i]
means "add the closing price at index i
to the total
." Close[i]
refers to the closing price of the i
th bar on the chart.i
is 0, so Close[0]
(the most recent closing price) is added to total
. With each iteration, i
increases, adding the closing price of each successive bar to total
. After 10 iterations, when i
reaches 10, the condition i < 10
becomes false, and the loop stops.This for
loop pattern is widely used in programming, particularly in MQL4 for tasks like summing up values or applying calculations across a series of data points, such as financial market prices.
while
Loopwhile
Loop with an Example
The while
loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.
Example Code:
Find the first candlestick with a closing price higher than the opening price.
int index = 0;
while(index < Bars && Close[index] <= Open[index]) {
index++;
}
Print("First bullish candlestick found at index: ", index);
Explanation:
int index = 0;
: Declares an integer variable named 'index' and initializes it to 0. This variable will be used to track the position of the candlestick in the chart.while(index < Bars && Close[index] <= Open[index])
: This is the beginning of the 'while' loop. The loop continues as long as two conditions are met:
index < Bars
: Ensures the loop continues only if the 'index' is less than the total number of bars (candlesticks) on the chart.Close[index] <= Open[index]
: Compares the closing price with the opening price of the candlestick at the current 'index'. If the closing price is less than or equal to the opening price, the loop continues.index++;
: Increments the 'index' by 1. This moves the check to the next candlestick each time the loop runs.Print("First bullish candlestick found at index: ", index);
: Once a bullish candlestick (where the closing price is higher than the opening price) is found, the loop stops, and this line prints the index of that candlestick.do-while
Loop with an Example
The do-while
loop is similar to a while loop, but the code inside the loop body is executed at least once before the condition is tested.
Example Code:
int counter = 0;
do {
Print("Counter value: ", counter);
counter++;
} while(counter < 5);
Explanation:
int counter = 0;
: Initializes an integer variable 'counter' with a value of 0. This variable will keep track of the number of iterations.do { ... } while(counter < 5);
: This is the 'do-while' loop.
Print("Counter value: ", counter);
: Inside the loop, this line prints the current value of 'counter'.counter++;
: After printing, this line increments the 'counter' by 1.These loop structures are fundamental in MQL4 and are used widely in developing trading algorithms and strategies.
Understanding control structures is fundamental to programming in any language. They provide the logic that allows your algorithm to make decisions and repeat actions. Mastering loops and conditional statements in MQL4 will greatly enhance your ability to create powerful and efficient trading algorithms.
NEXT UP: PreDefined and Custom Functions in MQL4