How to return last closed position??

 

Hello, i'm newbie in programming MT4 and i want to make my own expert advisor...

But i have some experience in C programming, so i have the logic n algorithm...

Unfortunately, I still strange with MT4 language program even i have the logic...

A lot of question that i want to ask, maybe i ask one per once...

First, how to return last closed posision information??

I mean, I want to get my last closed position is Profit or Loss, and the position in Buy or Sell??

Thanks.. :)

 

Here is a general description on how to use the order pool -> https://www.mql5.com/en/forum/126165. In your case you want to filter all orders by their OrderCloseTime().

 

Okay, i have read that thread and I want to get my last profit from history pool... This is my code :

int total = OrdersHistoryTotal(), profit;                 
for (int i=0; i<OrdersHistoryTotal(); i++) {    
   if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue; 
   //profit = OrderProfit();
}
am I correct??
 
iTsMePaTz:

Okay, i have read that thread and I want to get my last profit from history pool... This is my code :

No. Here's some quick code (I haven't checked it, so use at your own risk):

// return last closed ticket (returns -1 if not found)
int LastClosedTicket()
{
   datetime last_closed = 0;           // close time of last closed order
   int last_ticket = -1;               // ticket number of last closed order  
   
   // loop on all orders in history pool and filter
   for (int i=0; i<OrdersHistoryTotal(); i++) {  
      if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      //if (OrderMagicNumber() != MAGIC) continue;
      
      if (OrderType()<=1) {
         if (OrderCloseTime() > last_closed) {        // here we filter the last closed order
            last_closed = OrderCloseTime();           // save close time and ticket for next iteration
            last_ticket = OrderTicket();
         }
      }
   }
   
   return(last_ticket);
}

// return last closed profit (returns 0 if not found)
double LastClosedProfit()
{
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0) {
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderProfit());
   }
   
   return(0.0);
}

Please study the code. If u don't understand it then obviously you are jumping ahead too fast. I suggest you start here -> https://book.mql4.com//.

 

Thanks man, my EA completely work like what I want...

Your code is the last puzzle to complete my algorithm...

Thank you... ^__^

Reason: