What is the scope of "OrderSelect" function in mql4?

Hi everyone there. I’m working on an EA in MT4 and currently wonder what is the scope of OrderSelect function. Suppose I have code similar to this:


void ProcessOrders() {
	int TotalOrders = OrdersTotal();
	bool OrderClosed = false;

  for(int PositionIdx = TotalOrders - 1; PositionIdx >= 0; PositionIdx --) {              
    if (OrderSelect(PositionIdx,SELECT_BY_POS)==true) {
			OrderClose = CloseOneOrder(OrderTicket());
		} 
}

bool CloseOneOrder(int OrderTickNo) {
	bool OrdClosedOk = false;
	
	// Do I need to select order once again? If I do, does this
	// interrupt the iteration of order selection in function ProcessOrders?

	OrderSelect(OrderTickNo,SELECT_BY_TICKET); //<--
	if(OrderOpenPrice() > 1.36)	
		OrdClosedOk = OrderClose(OrderTickNo,OrderLots(),Bid,2,Red);
	
	return(OrdClosedOk);
}

I know I can structure the code differently, but just want to make sure I understand how it goes. So If I select on order in one function, does it stay selected even after calling another function? If it does and I select one or different order, does this have any influence of the selection in the first function? Much thanks for your help in advance.

If you use access by ticket number, you don’t have to be concerned with OrderSelect(). If you use select by position, the last one accessed is the active order. Very poor design by MetaQuotes and easy to misunderstand.

Thanks for quick reply CodeMeister. So if I understood you correctly OrderSelect() has some kind of “global scope”, meaning that if select an order in one function and afterwards call second function, without reselecting any order, but try to read some properties of order (like OrderOpenTime(), OrderType(),…) I would get properties which belong to the order I selected in first function? So to ilustrate:

void ProcessOrders() {
	int TotalOrders = OrdersTotal();
	bool OrderClosed = false;

  for(int PositionIdx = TotalOrders - 1; PositionIdx >= 0; PositionIdx --) {              
    if (OrderSelect(PositionIdx,SELECT_BY_POS)==true) {
			OrderClose = CloseOneOrder(OrderTicket());
		} 
}

bool CloseOneOrder(int OrderTickNo) {
	bool OrdClosedOk = false;

	if(OrderOpenPrice() > 1.36)	//<-- does this price belong to the order currently selected by ProcessOrders() function?
		OrdClosedOk = OrderClose(OrderTickNo,OrderLots(),Bid,2,Red);
	
	return(OrdClosedOk);
}

However, if I leave the code as in my original post, does the OrderSelect() in the function CloseOneOrder, influence the loop of order reading in ProcessOrders function, I supppose it doesn’t, right?
Thank you very much for your effort and time.

The answer to your first question is yes, it does have global scope.

The answer to your second question is I don’t know. I have never and would never write code like that. Simple code is best.

Thanks again CodeMeister, I appreciate your help :slight_smile: