How can i get ticket of the last position?

To add comments, please log in or register
mohsen bahrami
368
mohsen bahrami  

Hi

After i use "PositionClose()"  and my open position get closed, I want to know ticket of the last order and use "GetLastOrderTicket()" Function as  below:

but the ticket returnd is for The last one left.   How can i fix it?


ulong GetLastOrderTicket()
  {
//--- request history for the last 10 days
   if(!GetTradeHistory(10))
     {
      //--- notify on unsuccessful call and return -1
      Print(__FUNCTION__," HistorySelect() returned false");
      return -1;
     }
//--- 
   ulong first_order,last_order,orders=HistoryOrdersTotal();
//--- work with orders if there are any
   if(orders>0)
     {
    //  Print("Orders = ",orders);
      first_order=HistoryOrderGetTicket(0);
   //    PrintFormat("first_order = %d",first_order);
      if(orders>1)
        {
         last_order=HistoryOrderGetTicket((int)orders-1);
         Print("GetLastOrderTicket 1    Num orders:",orders,"    first_order:",first_order,"    last_order:",last_order);
         return last_order;
        }
      return first_order;
     }
//--- no order found, return -1
   return -1;
  }
Alain Verleyen
38857
Alain Verleyen  
mohsen bahrami:

Hi

After i use "PositionClose()"  and my open position get closed, I want to know ticket of last the order and use "GetLastOrderTicket()" Function as  below:

but the ticket returnd is for The last one left.   How can i fix it?


If you want to know something about a closed position, you have to work with "deals" not with "orders".

Please make some researches on the forum, there are plenty of topics about similar things.

mohsen bahrami
368
mohsen bahrami  
Alain Verleyen:

If you want to know something about a closed position, you have to work with "deals" not with "orders".

Please make some researches on the forum, there are plenty of topics about similar things.Hi

Hi

Thanks alot.

mohsen bahrami
368
mohsen bahrami  
Alain Verleyen:

If you want to know something about a closed position, you have to work with "deals" not with "orders".

Please make some researches on the forum, there are plenty of topics about similar things.


But i could'nt solve my problem.
Immediately after PositionClose() , I cant get profit of last closed position.

After using PositionClose() function, i use HistorySelect() and HistoryDealsTotal(). but the last Deal returned does'nt refers to Last Closed position.

Files:
fxsaber
14373
fxsaber  
if(!HistorySelect(from,to + 100))
Alain Verleyen
38857
Alain Verleyen  
mohsen bahrami:


But i could'nt solve my problem.
Immediately after PositionClose() , I cant get profit of last closed position.

After using PositionClose() function, i use HistorySelect() and HistoryDealsTotal(). but the last Deal returned does'nt refers to Last Closed position.

There are plenty of problems with your code. The 2 main in your specific case is :

1° When PositionClose() returns true, that means the order to close has been successfully sent, not that the position is already close (and that MT5 is aware of it). In a script you would need to use Sleep(), in an EA better use OnTrade() or OnTradeTransaction().

2° No need to use HistorySelect() (wrongly as noted by fxsaber), and you can't be sure the last index (deals-1) is the one concerning your position.

You know the position ticket, you should use it :

if(HistorySelectByPosition(lastTicket))
 {
  ...
 }
(strictly speeking, a position ID should be use with HistorySelectByPosition(), but in your case is the same as the position ticket anyway).
mohsen bahrami
368
mohsen bahrami  
Alain Verleyen:

There are plenty of problems with your code. The 2 main in your specific case is :

1° When PositionClose() returns true, that means the order to close has been successfully sent, not that the position is already close (and that MT5 is aware of it). In a script you would need to use Sleep(), in an EA better use OnTrade() or OnTradeTransaction().

2° No need to use HistorySelect() (wrongly as noted by fxsaber), and you can't be sure the last index (deals-1) is the one concerning your position.

You know the position ticket, you should use it :

(strictly speeking, a position ID should be use with HistorySelectByPosition(), but in your case is the same as the position ticket anyway).


Using Sleep() is not OK in Tester , but when I run the program, it works correctly.

Alain Verleyen
38857
Alain Verleyen  
mohsen bahrami:


Using Sleep() is not OK in Tester , but when I run the program, it works correctly.

There is no need to Sleep() in the tester as the result is immediate, no delay implied.
To add comments, please log in or register