Search Last Lost position???

 

Hi everyone,

How can I search for my last position, and how can I check if it was a profitable or lost position?

Any code suggestion?

Regards

Francis

 
cashmaker:

How can I search for my last position, and how can I check if it was a profitable or lost position?

Any code suggestion?

You need to loop on all orders, select each one in turn (using OrderSelect()) and filter them according to your criteria to find the 'last' one (which is in 'scope' of your EA). To find out if it is profitable, you need to select it (unless it has already been selected) and check the return of OrderProfit(). You can find an explanation of the concept of trade/history pools and order selection here -> https://www.mql5.com/en/forum/126165.


Edit: if swap or commission is relevant, then they should be added to the order's profit; they can be retrieved using OrderSwap() and OrderCommission() (after order has been selected).

 

Snap, I've just finish writing the code. Hate to follow Gordon's post. If you want example here it is. I recommend you follow the links and learn what it all means tho. So that you don't have to keep asking.

 

int start(){
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
int Ticket;
for(int i=OrdersHistoryTotal()-1; i>=0; i--){
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){
      if(OrderProfit() - (OrderCommission()+OrderSwap()) < 0)
         {Ticket=OrderTicket(); break;}}}
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
return(Ticket);}
 
ubzen:

Snap, I've just finish writing the code. Hate to follow Gordon's post. If you want example here it is. I recommend you follow the links and learn what it all means tho. So that you don't have to keep asking.

int start(){
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
int Ticket;
for(int i=OrdersTotal()-1; i>=0; i--){
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true){
      if(OrderProfit() - (OrderCommission()+OrderSwap()) < 0)
         {Ticket=OrderTicket(); break;}}}
//+------------------------------------------------++------------------------------------------------+
//+------------------------------------------------++------------------------------------------------+
return(Ticket);}

your code is not correct. as written in documentation OrdersTotal() returns the amount of Market and Pending orders. even if the loop works, you will always iterate only through a little part (depending on current open orders) of your history. you have to use OrdersHistoryTotal() instead.

also a check if the order was actually triggerd and closed should be added.

 

Yeah thats right lol... Use for(int i=OrdersHistoryTotal(); i>=0; i--){ instead :) thanks zzuegg.

Ps: returning ( return(Ticket);} ) for the start function is a bad idea. This is provided for edu purpose Only. I didn't test the code. Wrote that way so that I make sure it compiles.

 

Thank you all of you for your help.

Reason: