MQL4 - automated forex trading   /  

Forum

How to find the profit of the last trade

Back to topics list To post a new topic, please log in or register

avatar
18
investor_me 2006.10.25 16:27 
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
Interaction between MetaTrader 4 and Matlab via CSV Files

Interaction between MetaTrader 4 and Matlab via CSV Files

Step-by-step instructions of how to organize data arrays exchange between MetaTrader 4 and Matlab via CSV files.


avatar
97
Digger 2006.10.26 02:03 

#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(); }                        
    }

avatar
18
investor_me 2006.10.26 13:38 
Digger wrote:

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(); }                        
    }


avatar
13
Craig66 2006.10.26 22:14 
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?

avatar
81
Craig 2006.10.28 21:32 
...a tumbleweed rolls by...

avatar
18
investor_me 2006.10.28 23:58 
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.


Back to topics list  

To add comments, please log in or register