Conditional operator


for example, forex

We will continue to consider our first expert advisor and I will speak about a conditional operator if-else, which I used in the init() function.

   int init()
{
//----
if (iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0) > Close[0])
CurrentState = STATE_SHORT;
else CurrentState = STATE_LONG;

MyOrderTicket = 0;
//----
return(0);
}

Format of the conditional operator if-else:

   if (expression)
the first operator
else
the second operator

The principle of the operator’s work: the value of the expression in brackets is calculated; if it is true, the «first_operator» is executed, otherwise — «the second_operator» is used.

In our first expert advisor in the init() function there is such a line:

      if (iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0) > Close[0])
CurrentState = STATE_SHORT;
else CurrentState = STATE_LONG;

In this line the expression iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0) > Close[0] is calculated. Being somewhat previous I can say that in this expression the value of exponential moving average of closing price with the period defined by the external variable MAPeriod is compared with the closing price of the current bar ( Close[0] ).

If the value of the moving average is greater than the closing price then the first operator is executed:

   CurrentState = STATE_SHORT;

If it is less, then the second operator is executed:

   CurrentState = STATE_LONG;

If it is required to execute several operators at once as an operator, it is possible to use a compound operator, i.e. to enclose the required operators in braces. At that a semicolon isn’t put after the closing brace.

We can find an example of a compound operator in our expert advisor:

   if (!IsTesting()) 
return(MarketInfo(s, MODE_LOTSIZE)*MarketInfo(StringSubstr(s, 0, 3)+"USD",
MODE_BID)/AccountLeverage());
else
{
p = iClose(StringSubstr(s, 0, 3)+"USD", Period(),
iBarShift(StringSubstr(s, 0, 3)+"USD", Period(), CurTime(), true));
return(MarketInfo(s, MODE_LOTSIZE)*p/AccountLeverage());
}

If there is no need in execution of the second operator (in case if the expression in brackets isn’t true), the else part can be omitted:

   if (expression)
the first operator

Example from our expert advisor:

   if (s == "CHFJPY")
{
p = iClose("USDCHF", Period(), iBarShift("USDCHF", Period(), CurTime(), true));
return(MarketInfo(s, MODE_LOTSIZE)/(AccountLeverage()*p));
}

If several if-else operators are nested in each other, and some of the operators have the omitted else part, then else is always connected with the nearest previous if operator in the same block, which doesn’t have the else part.

Usually operators on the code of an expert are executed successively — one by one. However it is often required to change the order of their execution depending on some conditions. In this article I consider one of the possible ways to change this order: conditional operator if-else.


Next article: "While loop"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?