Download MetaTrader 5

last_closed_order_profit

To add comments, please log in or register
Having troubles understanding the code? Perhaps, you can find an article on that topic!
el3reef
178
el3reef 2016.10.05 13:53 

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
9644
Keith Watford 2016.10.05 18:31  

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.

To add comments, please log in or register