How to find the profit of the last trade

 
In my EA, there is need to find the exact profit generated in the last closed trade, which is usually exited by SL or take profit. How can I know if the last order exited with SL or TP and the profit generated from it?

Thanks
 

#include <stdlib.mqh>







int start(){


  static int ClosedTrades;  if(ClosedTrades==HistoryTotal()-1);
      OrderSelect(ClosedTrades,SELECT_BY_POS,MODE_HISTORY){
         if((OrderType()==OP_BUY)||OrderType()==OP_SELL)) {
            if(CompareDoubles(OrderClosePrice(),OrderTakeProfit())){ //Order closed by TP             
                        .
                        .
                        .
               }                                   
            if(CompareDoubles(OrderClosePrice(),OrderStopLoss())){ // Order closed by SL    
                        .
                        .
                        .
               }
        ClosedTrade=HistoryTotal(); }                        
    }
 
Digger:

Great!

Thanks for the quick reply,


#include <stdlib.mqh>







int start(){


  static int ClosedTrades;  if(ClosedTrades==HistoryTotal()-1);
      OrderSelect(ClosedTrades,SELECT_BY_POS,MODE_HISTORY){
         if((OrderType()==OP_BUY)||OrderType()==OP_SELL)) {
            if(CompareDoubles(OrderClosePrice(),OrderTakeProfit())){ //Order closed by TP             
                        .
                        .
                        .
               }                                   
            if(CompareDoubles(OrderClosePrice(),OrderStopLoss())){ // Order closed by SL    
                        .
                        .
                        .
               }
        ClosedTrade=HistoryTotal(); }                        
    }
 
I was under the impression that the numerical order of the MODE_HISTORY collection was not the order in which the trades were closed.
Therefore one has to find the closed trade with the biggest timestamp, can somebody at Metaquotes clarify this?
 
...a tumbleweed rolls by...
 
I worked my way through the above function and it seems to correctly identify the last closed order. It worked fine for me because I have only one order open at a time. But I understand that others who want to use the function for multiple simultaneous orders, this may need to be worked out more thoroughly and delicately.
Reason: