# How to execute block of code only if all conditions of loop are true in MQL5 ?

As an example lets take 10 candles, candles 1-10 and their OHLC data. On the chart this forms a perfect "V" shaped pattern. This in code for me would be
```( (close[10]>close[9])&&(close[9]>close[8])&&(close[8]>close[7])&&(close[7]>close[6]) ) &&
( (close[6]<close[5])&&(close[5]<close[4])&&(close[4]<close[3])&&(close[3]<close[2])&&(close[2]<close[1]) )

OR

for(int i=10; i>6; i--)
{
for(int j=5; j>1; j--)
{
(close[i]>close[i-1]) && (close[j]<close[j-1])
}
}```

This would form a " V " shape and hopefully it made sense, in my strategy the number of candles could be 10 or even 50, I use array minimum to find the lowest candle close. I'd tried to loop it but the loop will check only if any one of the condition is true and execute the block of code inside the loop, I want the loop to check whether all the conditions are true and if yes only then execute the block of code.

Of course, a perfect V would be formed with an odd number of bars so that there is an equal number of higher closes to the right and left of the low close.

```//+------------------------------------------------------------------+
//|                                                    V_Pattern.mq5 |
//|                                                    Keith Watford |
//+------------------------------------------------------------------+
#property version   "1.00"

//--- input parameters
input int      NumberOfBarsForVpattern=11; //Number Of Bars For V Pattern
input int      CheckStartBar=1;            //Check Start Bar (Just for testing, normally will be 1)
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
CheckForVpattern(CheckStartBar); //In OnInit just for testing while the market is closed and no incoming ticks
//Normally in OnTick() after a check for a new bar.
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
bool CheckForVpattern(int startBar=1)
{
double closeArray[];
ArraySetAsSeries(closeArray,true);
int copied=CopyClose(_Symbol,PERIOD_CURRENT,startBar,NumberOfBarsForVpattern,closeArray);
if(copied!=NumberOfBarsForVpattern)
{
Print("Error copying close values. Error code ",GetLastError());
return(false);
}

int centerBar=(NumberOfBarsForVpattern-1)/2;
//For the V pattern to be valid, the lowest close MUST be the center bar.
int min=ArrayMinimum(closeArray);
if(min!=centerBar)
return(false);
//Check for higher closes to the right
for(int x=centerBar-2; x>=0; x--)
{
if(closeArray[x]<=closeArray[x+1]) //close is not higher than previous
return(false);
}
//Check for higher closes to the left
for(int x=centerBar+2; x<copied; x++)
{
if(closeArray[x]<=closeArray[x-1]) //close is not higher than later bar
return(false);
}

Print("Is V Pattern");

return true;
}
//+------------------------------------------------------------------+```

Keith Watford:

Of course, a perfect V would be formed with an odd number of bars so that there is an equal number of higher closes to the right and left of the low close.

Thanks, i was able to understand the code and implement it into my EA

Reason: