# Functions in the loop

1434

To simplify the code, I was trying to integrate functions in the loop this way:

```   for(int i=0;i<OrdersTotal();i++){
//----------------------------------------- Grid orders ---!
if(GridSize>0){
Print(Symbol()," OrderSend error ",GetLastError());
return;
}
}
}```

Original functions that worked were:

```double LastBuy(){
double Price=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderOpenPrice()<Price||Price==0){
Price=OrderOpenPrice();
}
}
}
}
return(Price);
}

double Lot=0;
for(int i=0;i<OrdersTotal();i++){
if(OrderLots()>Lot){
Lot=OrderLots();
}
}
}
}
return(Lot);
}```

As the loop works in the same way that's why I tried to integrate, but no success, just take values from the first order.

I hope someone can help, thank you in advance.

19087

David Diez:

To simplify the code, I was trying to integrate functions in the loop this way:

Original functions that worked were:

As the loop works in the same way that's why I tried to integrate, but no success, just take values from the first order.

I hope someone can help, thank you in advance.

Loop the other way from OrdersTotal-1 to 0 -- ,when you find the order you seek break the loop

1434

Lorentzos Roussos:

Loop the other way from OrdersTotal-1 to 0 -- ,when you find the order you seek break the loop

Can you explain better? I don't know much about loops, always used this one.
19087

David Diez:
Can you explain better? I don't know much about loops, always used this one.

Sure .

```for(int i=0;i<OrdersTotal();i++)
{
//this will run through orders from 0 to last
}

for(int i=OrdersTotal()-1;i>=0;i--)
{
//this will run through orders from last to 0
//break;//<--this will stop the loop process
//continue;//<--this will skip all following code in the loop and move to the next loop run (i)
}```
1434

Lorentzos Roussos:

Sure .

That doesn't work.

Loop Works correct in function calls, so why it doesn't in the main sequence?

19087

David Diez:

That doesn't work.

Loop Works correct in function calls, so why it doesn't in the main sequence?

Ow . it was implied these should be used in a function and not on the global scope

1434

Lorentzos Roussos:

Ow . it was implied these should be used in a function and not on the global scope

I'm trying now a loop for buy and another loop for sell:

```   //-------------------------------------------------- Buy loop ---!
//----------------------------------------- Grid orders ---!
if(Ask<=OrderOpenPrice()-(GridSize*10)*Point){ // OpenPrice of the last order.
Print(Symbol()," OrderSend error ",GetLastError());
return;
}
}
}```

But doesn't match the values :/

Open new orders with the same lot above the grid distance till reach that distance, then opens the correct lot.
1434

Now this Works, but doesn't open new grids from the third order:

```   for(int i=BuyOrders()-1;i==0;i--){
//----------------------------------------- Grid orders ---!
Print("Grid distance is ",(GridSize*10)*Point);
Print("New grid at ",OrderOpenPrice()-(GridSize*10)*Point);
Print(Symbol()," OrderSend error ",GetLastError());
return;
}
}
}
```
19087

You have to use OrdersTotal() in the loop header , BuyOrders will lead to a wrong index reference since you are selecting by position
20248

David Diez: Can you explain better? I don't know much about loops, always used this one.
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
2. and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
1434

William Roeder:
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
2. and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
Worths nothing partner.