Retrieving Last closed Order's Ticket

 

I'm trying to retrieve the Ticket number of the Last Closed order.

So I tried this:

 int start()

{

History_Calling(Symbol(), MagicNumber); 

 BuyOrder=OrderSend(Symbol(), OP_BUY, LotSize,OpenPrice,Slippage,StopLossPrice,TakeProfitPrice, "Buy Order",MagicNumber, 0,Blue);

    if (BuyOrder>0)

{

 Print("Order Opened Successfully, Ticket:",BuyOrder);

} 

return(0);

}

////////////////////////////////////////////////////////////////////////////

void History_Calling(string symbol, int magic)

{ 

 int i,hstTotal=OrdersHistoryTotal();

  for(i=0;i<hstTotal;i++)

    {

     if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;

       {

        if(OrderSymbol()==Symbol())

         {

          if(OrderMagicNumber()==MagicNumber)

           {

            if(OrderType()==OP_BUY)

             {

              if(OrderTicket()==BuyOrder)

               {

                Print("Ticket no of Last Bought Order:",BuyOrder);

               }

              }

             else

              {

               if(OrderType()==OP_SELL)

                { 

                 if(OrderTicket()==SellOrder)

                  {

                   Print("Ticket no of Last Bought Order:",SellOrder);

                  }

                 }

                }//End else   

             }

            }

           }

          } //End for(i=0; i<hsTotal;i++)

         } 

I put this 'History_Calling' function in another full working code and tested in strategy tester but not seeing any printing output in the 'Journal' tab.

Where is the Fault?

Regards

 

Use OrderSelect function to select the most recently closed order. The oldest (furthest in the past) order you have in history will be order number zero 0.

The number of orders in your history is determined by how you have your history selected to show... last week? last month?

"The history list size depends on the current settings of the "Account history" tab of the terminal."

If there are 13 orders in your history they are numbered 0-12 so the last (most recent) order closed is always OrdersHistoryTotal-1

Or in this case 13 - 1 = 12. So order 12 is the most recent order. So to get the closing price of the most recent order closed...

int i =OrdersHistoryTotal()-1;
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
double closingPrice =OrderClosePrice();
It will give you the OrderClosePrice of the order that was just now selected from the History Pool of orders.
Print("The last order was closed at ",closingPrice);
PipPip...Jimdandy
 
void History_Calling(string symbol, int magic)

{ 

 int i,hstTotal=OrdersHistoryTotal();

  for(i=0;i<hstTotal;i++)

    {

...
          if(OrderMagicNumber()==MagicNumber)
 
Jimdandy:

Use OrderSelect function to select the most recently closed order. The oldest (furthest in the past) order you have in history will be order number zero 0.

The number of orders in your history is determined by how you have your history selected to show... last week? last month?

"The history list size depends on the current settings of the "Account history" tab of the terminal."

If there are 13 orders in your history they are numbered 0-12 so the last (most recent) order closed is always OrdersHistoryTotal-1

Or in this case 13 - 1 = 12. So order 12 is the most recent order. So to get the closing price of the most recent order closed...

It will give you the OrderClosePrice of the order that was just now selected from the History Pool of orders. PipPip...Jimdandy


That is not always true

It is best to loop through and find the latest OrderCloseTime

 
Jimdandy:

Use OrderSelect function to select the most recently closed order. The oldest (furthest in the past) order you have in history will be order number zero 0.

The number of orders in your history is determined by how you have your history selected to show... last week? last month?

"The history list size depends on the current settings of the "Account history" tab of the terminal."

If there are 13 orders in your history they are numbered 0-12 so the last (most recent) order closed is always OrdersHistoryTotal-1

Or in this case 13 - 1 = 12. So order 12 is the most recent order. So to get the closing price of the most recent order closed...

It will give you the OrderClosePrice of the order that was just now selected from the History Pool of orders. PipPip...Jimdandy


Thanks Jim for your Detail reply.

Certainly these information will add value to my knowledge base.

 

I'm here to learn. what part of this is not always true. I have been unable to get any other order than the last closed order to show using this method no matter how i arrange them in my accounts history ledger. I always assumed that the numbering of the closed orders on the server was done by OrderCloseTime . Enlighten me please.Thinking

 
angevoyageur:


Thanks. Yes, now the ticket is printing.

Actually I was modifying the same code in the same EA and compiling it but didn't restarted the compiler and terminal.

Hence the modifications wasn't working. After restarting, other printing functions worked too.

But I think it's not working as I wanted. I wanted the 'Ticket' number of the Last Closed orders. I though it'd be different for

different orders but here getting the Same Number!

Files:
hotash-3.mq4  15 kb
 
Jimdandy:

If there are 13 orders in your history they are numbered 0-12 so the last (most recent) order closed is always OrdersHistoryTotal-1

That is not entirely accurate. OrdersHistory is not guaranteed to be by order of date, also the history will contain trades by other EA's, manual trades, deleted pending orders, and other things like balance transfers.

It is advisable to loop through at least the last 10 entries in OrdersHistory pool and Sort by date, and magic number to get the last trade closesd by the EA in question. If you consider a scenario where you have EA's running on several charts at the same time, it would be neccessary to loop through a lot more than 5 or 10 entries.

 
SDC:

That is not entirely accurate.

but in the strategy tester it's accurate ;-)
 
qjol:
but in the strategy tester it's accurate ;-)
yes it is lol
 
If you wanted to write it to get the ticket number of the trade with the highest closing time you might do it like this.....
//+----------------------------------+
//|Script program start function     |
//+----------------------------------+
void OnStart()
{
History_Calling(Symbol(),0); // put in a magic if you use one.
}
void
History_Calling(string symbol,int magic){
int i=0;
int hstTotal=OrdersHistoryTotal();
int counter = 0;
int ticketNumber = 0; 
 for(i=0;i<hstTotal;i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      if(OrderType() < 2)// this weeds out any pending orders.
       if(OrderSymbol()== symbol)
        if(OrderMagicNumber() == magic)
         if(OrderCloseTime() > counter)
           {
            counter=OrderCloseTime();    //each time it finds a greater closing time it
            ticketNumber = OrderTicket();//overwrites the counter and the ticket number.    
           }
    }
     //now it has the ticket number of the highest closing time in seconds since 1970
     //you can get whatever info you want about that particular ticket.
    OrderSelect(ticketNumber,SELECT_BY_TICKET);// when you select by ticket you do not need to specify mode trades or mode history.
    int type = OrderType();// 0 is a buy and 1 is a sell
    string closeTime= TimeToString(OrderCloseTime());
    double closePrice = OrderClosePrice();     
    Print("Ticket number ",ticketNumber," of type ",type," was closed at ",closeTime," at a price of: ",closePrice);
} 
I wrote that kind of quick so there may be some errors in it for you to work out. But i think you get the idea... PipPip..Jimdandy
Reason: