last_closed_order_profit

el3reef
202
el3reef  

for sharing and improving

 

double last_closed_order_profit(int Magic){
     double orderStopLoss;
     for(int i= OrdersHistoryTotal() ;i >= 0;i--)
      {
        OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
        if (OrderMagicNumber() == Magic) break;
      }
      
   return OrderProfit();
}

 

 Waiting for comments and developing

Keith Watford
Moderator
26718
Keith Watford  

Consecutive selection of orders using the SELECT_BY_POS parameter returns information in the sequence in which it was received from the trading server. Sorting of the resulting list of orders cannot be guaranteed.

From the documentation.

Your code does not necessarily find the last closed order, it finds the order in the highest position in the list that has the magic number.