[MQL4 Coding] How to get Most recently Closed OrderClosePrice()?

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
38
Hello,

I am trying to get a last order close price (most recently closed order) based on filter Magic number and Order symbol.

Code :

MQL4:
double LastOrderClosePrice(int magic)
  {
   double LastOrderClosePrice_Price = 0;
   for(int ClosePrice_i = 0; ClosePrice_i < OrdersHistoryTotal(); ClosePrice_i++)
     {
      OrderSelect(ClosePrice_i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != magic)
         continue;
      LastOrderClosePrice_Price = OrderClosePrice();
      //break; => Using break gives wrong value. Without break gives correct value
     }
   return LastOrderClosePrice_Price;
  }


My code is working fine as i want but the only problem is, if my account have 10,000+ Closed Order history, this will loop through all the 10,000 closed order and this will make EA slow. How to resolve this?


And one more confusion. Should i use for loop like this for(int ClosePrice_i = 0; ClosePrice_i < OrdersHistoryTotal(); ClosePrice_i++) (or) for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) ?
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
18,910
1,428
144
Odesa
www.earnforex.com
You can just scrap the cycle and use something like this:
MQL4:
OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY);
It should select the latest closed order.

However, the sorting order for order history isn't guaranteed (that's what the MQL4 help file on OrderSelect() says), there could be some situations when the latest order in the history isn't the latest order closed, then you will be getting a close price for a wrong order.

An error-proof way would be to cycle through all orders, add their tickets and close time to an array and then to sort that array.
 

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
38
The surefire approach is to run a cycle on all orders, filtering them, and putting them into a custom array. Then you will be able to sort the array by time and make sure you are accessing the latest order.
Using array and storing all the values of OrderHistory can take longer time and make EA slower. Can you help me with some sample code?

Now i am trying like this

MQL4:
double update_last_order(int magic)
{
   int      lastHistoryCount    = EMPTY;
   datetime lastOrderCloseTime  = 0;
   double   lastOrderClosePrice = EMPTY;
   int iPos = OrdersHistoryTotal();
   if(iPos == lastHistoryCount)
      return lastOrderClosePrice; // Return the default value if there are no changes.
   lastHistoryCount = iPos;
   while(iPos > 0)
      if(
         OrderSelect(--iPos, SELECT_BY_POS, MODE_HISTORY)
         && OrderCloseTime()   >  lastOrderCloseTime
         && OrderMagicNumber() == magic
         && (OrderType() == OP_BUY || OrderType() == OP_SELL)
         && OrderSymbol() == Symbol()
      )
      {
         lastOrderClosePrice = OrderClosePrice();
         lastOrderCloseTime  = OrderCloseTime();
      }
   return lastOrderClosePrice; // Return the lastOrderClosePrice
}
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,910
1,428
144
Odesa
www.earnforex.com
Using array and storing all the values of OrderHistory can take longer time and make EA slower. Can you help me with some sample code?

Now i am trying like this

MQL4:
double update_last_order(int magic)
{
   int      lastHistoryCount    = EMPTY;
   datetime lastOrderCloseTime  = 0;
   double   lastOrderClosePrice = EMPTY;
   int iPos = OrdersHistoryTotal();
   if(iPos == lastHistoryCount)
      return lastOrderClosePrice; // Return the default value if there are no changes.
   lastHistoryCount = iPos;
   while(iPos > 0)
      if(
         OrderSelect(--iPos, SELECT_BY_POS, MODE_HISTORY)
         && OrderCloseTime()   >  lastOrderCloseTime
         && OrderMagicNumber() == magic
         && (OrderType() == OP_BUY || OrderType() == OP_SELL)
         && OrderSymbol() == Symbol()
      )
      {
         lastOrderClosePrice = OrderClosePrice();
         lastOrderCloseTime  = OrderCloseTime();
      }
   return lastOrderClosePrice; // Return the lastOrderClosePrice
}
If you only need the last order's close price and don't need any of the previous orders' close prices, then you don't really need to use arrays as I suggested. My suggestion was more for the cases when you need to frequently access N last orders' close prices.

Your solution should work well for your case.