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)
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
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.
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!
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.
That is not entirely accurate.
but in the strategy tester it's accurate ;-)
//+----------------------------------+ //|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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to retrieve the Ticket number of the Last Closed order.
So I tried this:
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