selecting last closed order, from History (doh!)

 

OK, I am sure this must have been discussed before, but I could not find it to save my life.. so, what else do I need to add below, so to select from history the order just closed (ie the latest addition to history)?

(I want to create an object /visual reference on the chart based on data related to this order just closed,so I need select it first...)

for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderMagicNumber()  == Magic.Number             // my magic number
    &&  OrderSymbol()       == chart.symbol             // and my pair.
    &&  ?? Order JUST CLOSED and added to HISTORY???? (order with greatest "OrderCloseTime"?)
    ){

 
Dannoo007:

...but I could not find it to save my life..

You have to compare each OrderCloseTime() one by one, to see which OrderCloseTime() that bigger than the other OrderCloseTime().

I'll write the codes later. :)

 
Dannoo007:

OK, I am sure this must have been discussed before, but I could not find it to save my life.. so, what else do I need to add below, so to select from history the order just closed (ie the latest addition to history)?

(I want to create an object /visual reference on the chart based on data related to this order just closed,so I need select it first...)

I now do mine from OrdersHistoryTotal() rather than 1 less than that. That is not what the documentation suggests but it didn't work without. You would think that either ipos=0 or ipos=OrdersHistoryTotal()-1 would be the last order closed but that doesn't seem to work on a real account either. You will get the most recent one by running through all of them and finding the most recent one and that is not too hard provided you remember to save both the ticket number and the OrderCloseTime as you go!
 
But what will happen if at once 2 order will close at the same second?
OrderCloseTime() returns a datetime, that is only 1 second resolution.
 
erzo:
But what will happen if at once 2 order will close at the same second?
OrderCloseTime() returns a datetime, that is only 1 second resolution.
Yes, that could happen if one was closed by your EA and another was closed by the server on a stop, or both were closed on stops I suppose. Hopefully you can then decide which is which if you have two or more equal close times.
 
I've just realized that it will be only for a chart object visualization. I think, that names of the visual objects should store the ticket numbers of orders, and that ticket is not in the chart yet, just draw onto it.
 
erzo:
I've just realized that it will be only for a chart object visualization. I think, that names of the visual objects should store the ticket numbers of orders, and that ticket is not in the chart yet, just draw onto it.

Congrats, You just answer your own question after posting it on forum. I did that several.

Here's the codes, I had this somewhere, but du no where it now. Correct me if the codes is err.

int Closed_Ticked [1], Closed_Time [1], Index, Temp_Closed_Ticket [1], Temp_Closed_Time [1],
    Hist_Total, Total_Hist_Gap;

Hist_Total = OrdersHistoryTotal ();

for(int iPos = Hist_Total; iPos >= 0; iPos--) // we start not from zero
    {
    // ... your codes ....
    if (OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
        &&  OrderMagicNumber()  == Magic.Number             // my magic number
        &&  OrderSymbol()       == Symbol () // ==>> this is : chart.symbol             // and my pair.
        // &&  ?? Order JUST CLOSED and added to HISTORY???? (order with greatest "OrderCloseTime"?)
        )
        { 
        // ...my codes ....
        if (OrderCloseTime () >= ArrayMaximum(Closed_time, WHOLE_ARRAY, 0))// do not use ArrayMinimum ()
           {
            // Whoaaa ... there's order that closed at the same time ...
            if (OrderCloseTime () == ArrayMaximum(Closed_time, WHOLE_ARRAY, 0)) 
               {
               // raise the index
               Index ++;
               // ... change the size om temp array ...
               ArrayResize (Temp_Closed_Time,   Index + 1);
               ArrayResize (Temp_Closed_Ticket, Index + 1);
               // ... copy it into temp array ...
               ArrayCopy   (Temp_Closed_Time,   Closed_Time,   0, 0, WHOLE_ARRAY);
               ArrayCopy   (Temp_Closed_Ticket, Closed_Ticket, 0, 0, WHOLE_ARRAY);
               // ... change the size of main array
               ArrayResize (Closed_Time,   Index + 1);
               ArrayResize (Closed_Ticket, Index + 1);
               // ... copy it back from temp array
               ArrayCopy   (Closed_Time,   Temp_Closed_Time,   0, 0, WHOLE_ARRAY);
               ArrayCopy   (Closed_Ticket, Temp_Closed_Ticket, 0, 0, WHOLE_ARRAY);
               }
               else 
               {
               // LATE EDIT : My apologize : I had this long time ago, re-wrote again in hurry ...
               if (Index > 0)
                 {
                 Index = 0; // very important, must to do
                 // ... not important : change the size of main array back to 1 index
                 ArrayResize (Closed_Time,   1);
                 ArrayResize (Closed_Ticket, 1);
                 // ... if you're not sure that the above ArrayResize run correctly, then reset the value ..
                 ArrayInitialize (Closed_Time,   0);
                 ArrayInitialize (Closed_Ticket, 0);
                 }
               }
               Closed_Time   [Index] = OrderCloseTime ();
               Closed_Ticked [Index] = OrderTicket ();
            }

         // Wait ... suddenly there new closed order in the middle of the loop, how dare ...
         if (OrdersHistoryTotal () > Hist_Total) 
            {
            Total_Hist_Gap = OrdersHistoryTotal () - Hist_Total;
            Hist_Total = OrdersHistoryTotal ();
            pos += Total_Hist_Gap;
            }
        }
    }
    
    for (iPos = 0; iPos <= Index; iPos ++)
      {
      string text = StringConcatenate (text,"Ticket ",Closed_Ticked [iPos],
                    " Closed time ",TimeToStr (Closed_Time [iPos], TIME_DATE|TIME_SECONDS)),"\n";
      }
      
   Comment ("Dude, here is your most recent closed order \n"+text);
 

oh waw Onewithzachy.

I thought this should be a one-liner addition !! anyways, talking about the hidden paths of MQL4... my jaw is on the floor, cool script !!! will look into it and "digest" it, also LMAO ref below : ))))

 Comment ("Dude, here is your most recent closed order \n"+text);
 
Dannoo007:

oh waw Onewithzachy.

I thought this should be a one-liner addition !! anyways, talking about the hidden paths of MQL4... my jaw is on the floor, cool script !!! will look into it and "digest" it, also LMAO ref below : ))))

Whoa. wait, I wrote that in hurry, so please tell me if there's error. I edit and re-edit that after publishing, so re-look at it again.

 
Dannoo007:
to select from history the order just closed (ie the latest addition to history)?
  1. Select by date
    static datetime lastOrderCloseTime;
        for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
        &&  OrderMagicNumber()  == Magic.Number             // my magic number
        &&  OrderSymbol()       == chart.symbol             // and my pair.
        &&  OrderType()         <= OP_SELL//Avoid cr/bal https://www.mql5.com/en/forum/126192
        &&  OrderCloseTime()    >  lastOrderCloseTime
        ){
            lastOrderCloseTime = OrderCloseTime();
            :
    

  2. Order History sort by closing date - MQL4 forum
 
WHRoeder:
  1. Select by date

WHRoeder is correct, by making (look at my script above, not WHRoeder's codes) Closed_Ticked [1], Closed_Time [1], Index, Temp_Closed_Ticket [1], Temp_Closed_Time [1], as global variable, will make the searching much quicker.

Because the "last closed" value is stored, not empty anymore.

Mine was script, not CI or EA, so the array is empty at every execution.

There's one more thing:

erzo 2012.04.06 19:26
I've just realized that it will be only for a chart object visualization. I think, that names of the visual objects should store the ticket numbers of orders, and that ticket is not in the chart yet, just draw onto it.

if the object is moved by accident (you have naughty mouse playing clicking dragging with the insane cursor), you may not have the correct value, of the object .

PS : I edited my codes, you have to look at it. Wrote that in hurry, my apology for that . hope you don't mind :)

Reason: