# Multiple variations of the same condition

13

Hello guys!

I was wondering if there is a way to check the same condition with a different variable.

For example:

I want to buy when Open[1] is lower than a certain price and  Close[1] is higher than the same price. Lets say the price is 1.2500

I know how to write this condition, but what if i want the same condition to work for multiple variations of the price? For example 1.2500 or 1.2600 or 1.2700 etc.

Do i have to write the same contition for each price or there is a cleaner solution ?

Moderator
10614

Globalscope

```input string PricesInput="1.2500:1.2600:1.2700";
double Prices[];
int AS;```
```int OnInit()
{
string PricesStringArray[];
StringSplit(PricesInput,':',PricesStringArray);
AS=ArraySize(PricesStringArray);
ArrayResize(Prices,AS);
for(int x=0;x<AS;x++)
Prices[x]=NormalizeDouble(StrToDouble(PricesStringArray[x]),Digits);

for(int x=0;x<AS;x++)
Print(Prices[x]);

//---
return(INIT_SUCCEEDED);
}```
```void OnTick()
{

for(int x=0;x<AS;x++)
{
if(Open[1]<Prices[x] && Close[1]>Prices[x])
{
//Do Something
}
}```

Something like this should work. Input the numbers as a string of numbers separated by :

You'd probably want to add code so that it is only executed when a new bar opens

13

GumRai, thanks for the quick answer. So, correct me if i'm wrong (im quite new to mql4), what we are doing here is that we set an array, which in this case is an array of strings.

Then we convert the strings to doubles and make a loop to use each index in the calculation.

What will happen if we dont have the specific numbers (1.2500, 1.2600, 1.2700) set as inputs and instead we have doubles a,b,c,d,e to which we assigned values from previous calculations?

For example: a is a third of the previous week's range, b is a quarter, c is half etc. and these values change for example every week ?

Can we make an array with these variables, instead of predefined constants?

Moderator
10614

GumRai, thanks for the quick answer. So, correct me if i'm wrong (im quite new to mql4), what we are doing here is that we set an array, which in this case is an array of strings.

Then we convert the strings to doubles and make a loop to use each index in the calculation.

What will happen if we dont have the specific numbers (1.2500, 1.2600, 1.2700) set as inputs and instead we have doubles a,b,c,d,e to which we assigned values from previous calculations?

For example: a is a third of the previous week's range, b is a quarter, c is half etc. and these values change for example every week ?

Can we make an array with these variables, instead of predefined constants?

You probably wouldn't want to use an array as you would be using different calculations for each variable.

Maybe calculate each variable and then use a function to carry out the repetitive checks with the variable.

13

GumRai:

You probably wouldn't want to use an array as you would be using different calculations for each variable.

Maybe calculate each variable and then use a function to carry out the repetitive checks with the variable.

I did calculate each value in a void function. The values are actually a percentage of the previous weeks range and are like 15-20 of them. I defined them on global scope. My question is how to check if  my condition is true for any of the values i have already calculated. It seems very inconvenient to write 15-20 times the same condition with an if statement for buy and then another 15-20 times for sell... What might be this "func to carry out the repetitive checks" that u are talking about?
Moderator
10614

```void OnTick()
{
//Code to calculate variable A
CheckCondition(A);

//Code to calculate variable B
CheckCondition(B);

//etc etc
//---
}
//+------------------------------------------------------------------+
//| Function                                             |
//+------------------------------------------------------------------+
bool CheckCondition(double variable_to_be_checked)
{
if(Open[1]<variable_to_be_checked && Close[1]>variable_to_be_checked)
{
//Do anything else that is required
return(true);
}
else
if(Open[1]>variable_to_be_checked && Close[1]<variable_to_be_checked)
{
//Open a Sell order?
//Do anything else that is required
return(true);
}

return(false);
}```

By making the function a bool, you can code it to stop the checks as soon as one of the condition checks is true

```void OnTick()
{
while(true)
{
//Code to calculate variable A
if(CheckCondition(A))
break;             //If the function returns true, the remainder of the while loop is not executed

//Code to calculate variable B
if(CheckCondition(B))
break;

// etc etc

break;                //break ensures that while loop is only executed once in case no condition returns true
}
//---
}```

.

13

Thanks alot GumRai! This while cycle is exactly what i needed. Very helpful.