Posted By:
30 Jul, 2014 9:18 pm PDT

Conditions in MetaTrader

CONDITIONS

There is one more important thing that you will always use – conditions. Our life contains a lot of conditions and action that follow these conditions. Very often we think by conditional clause. For example: “If I have enough time, I will read this book. If not, then it’s better to read a magazine”. You can generate hundreds of such conditions and actions. But how can we write them in MQL4? Here is an example:

// of course the conditionsn should be written in MQL4

if( I have enough time )

{

// here we place any actions, directions in MQL4

will read this book;

}

// if the first condition is not fulfilled,

// the second is fulfilled

else

{

read a magazine;// code

}

Now you should understand the syntax of conditions. Let’s view conditions, fully written in MQL4 language:

inta = 10;
intb = 0;
 
if(a > 10)
{
b = 1;
}
else
{
b = 2;
}
 
MessageBox("b=" + b,".");

Everything is quite easy. What is the value of b after the completion? Of course b=2, because the condition a > 10 is not fulfilled. It is easy. By the way, it is not necessary to use the key word else:

inta = 10;
intb = 0;
 
if(a > 10)
{
b = 1;
}

We know that different types of variables can acquire different values, for example:

]>·  int - integers (1, 60, 772);

]>· double – with a floating point (1.0021, 0.221);

]>· string – only lines ("Word", "Some text.");

]>· bool – only true or false (true, false).

So, the conclusion is: in conditions compare variables only with the values, they can accept, for example:

intInteger=10;
doubleDouble=1.0;
stringString="JustWord";
boolBool=true;
 
if(Integer<10) // the condition is correct
{
MessageBox("It works!","Really!");
}
 
if(Double!=1.0) // the condition is correct
{
MessageBox("works!","Simple!");
}
 
if(String==10) // this is nonsense!! 
// we found the variable 
//String with string type, but 10 is int
{
MessageBox("Wrong type","u can't c this");
}
 
if(String!="Word") // ok
{
// ...
}
 
if(Bool==true) // correct
{
// ...
}

Note, that we make comparisons using condition operators (==, !=, >, <, >=, <=). For string and bool types use only == and != comparisons.

Let us see one example:

inta=0;
intresult;
 
if(a==0)
{
result=1;
}
elseif(a==1)
{
result=2;
}
elseif(a==2)
{
result=3;
}
else
{
result=4;
}

 

Attachment:None


Complex conditions:

Very often one condition is not enough. You need to compare many parameters; here you have to do with complex conditions. For example, if I have enough time and patience, I will learn MQL4 language. We can write it as a code:

if((enough time) && (enough oatience))
{
I will learn MQL4 language;
}

It means you should first divide complex conditions into simple conditions, put them into brackets, and put && (logical AND) or || (logical OR) between them. If both conditions should be true, put && (AND). If only one conditions should be true, put || (OR). Here is an example:

inta=10;
intb=20;
 
if((a>5) && (b<50))
MessageBox("Its works!","Yes");

Beside you can enclose and combine as much conditions, as you need:

inta=10;
intb=20;
 
if(((a>5) || (a<15)) && (b==20))
MessageBox("It works, too!","Yes");
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud