Retrieving Last closed Order's Ticket - page 2

 
qjol:
but in the strategy tester it's accurate ;-)


I am using that code on live accounts in an EA and can't get it to give me a wrong answer. If they have a way to make the highest indexed trade not be the most recent one though i'd like to know about it... it could cause some problems....
 
Jimdandy:

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.


I just ran your code

int i =OrdersHistoryTotal()-1;
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
double closingPrice =OrderClosePrice();
Print(closingPrice);

Result

05:48:42 Script aaa AUDUSD,H1: loaded successfully

05:48:42 aaa AUDUSD,H1: initialized

05:48:42 aaa AUDUSD,H1: 102.287

05:48:42 aaa AUDUSD,H1: uninit reason 4

05:48:42 Script aaa AUDUSD,H1: removed

As you can see, it was not the last closed trade

trades

 

As WHRoeder has not posted it yet, I do it Could EA Really Live By Order_History Alone? - MQL4 forum.

 

Hey thanks guys! This is good info indeed! I just ran that code on several live accounts and it works on most of them just fine. However on one broker it was not working properly. After further investigation it looks like that particular broker's indexing of the orders in history is done by each orders OPENING time instead of it's CLOSING time. Glad you guys brought this to may attention.. many thanks... PipPip ...Jimdandy

p.s ..was wondering why you were getting an unitialization reason of 4.. I always get 0 ..

 
Jimdandy:

p.s ..was wondering why you were getting an unitialization reason of 4.. I always get 0 .

I have no idea. I certainly didn't close the chart.

 
Jimdandy:
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.....
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


Thanks once again for your input. The way you defined everything is quite easy for me to understand.

Here I am getting something strange though. I am not understanding how it's counting orders and displaying the Tickets.

Actually my plan is to compare the 'BuyOrder_3/SellOrder_3' of the '3 Simultaneous' orders this EA is opening.

The EA opens a New set of '3 Simultaneous' orders when the Last Opened order i.e. BuyOrder_3/SellOrder_3 gets closed.

Now I am trying that the EA will open '3 Simultaneous' order just once when it gets say Buy signal. Then if all the 3 orders

get closed either by SL/TP and the Buy signal is still 'Valid' it will Open only 'One' order instead of 'Three' orders.

That is why I'm trying to access the 'History' and check if that BuyOrder_3/SellOrder_3 got Closed so that I can set a single odersend function there.

Regards

 
if ( OrderTotal!=OrdersTotal()){
OrderTotal=OrdersTotal();

 color BuyColor =clrBlue;
   color SellColor=clrRed;
//--- request trade history
   HistorySelect(0,TimeCurrent());
//--- create objects
   string   name;
   uint     total=HistoryOrdersTotal();
   ulong    ticket=0;
   double   price;
   double   profit;
   datetime time;
   string   symbol;
   long     type;
   long     entry;
//--- for all deals
   /*for(uint i=0;i<total;i++)
     {*/
      //--- try to get deals ticket
      //if((ticket=HistoryOrderGetTicket(i))>0)
      if(ticket=HistoryOrderGetTicket(HistoryOrdersTotal()-2))
        {
        
         Print(ticket);
         CloseAllPenOrdersLevel(ticket);
       
     }
     
     
     }
Reason: