Select the last order BUY or SELL of the history

 
Hello,

I am currently developing an EA, but I have a little problem.
I would select the last trade in the history (closed) Type Buy or Sell and recover its profit and its type.

I made a small image to more clearly explain my goal.


I could do some research, I try to make a loop with OrdersHistoryTotal () but it did not work.
Someone would have already this type of script code or could put me on the right way?
Thanking you in advance for your help
 
jal_fr:
I could do some research, I try to make a loop with OrdersHistoryTotal () but it did not work.
Someone would have already this type of script code or could put me on the right way?
Thanking you in advance for your help

OrderHistoryTotal() will work . . . the last trade has the highest number, so count down . . .

for( TradeCount=OrdersHistoryTotal()-1;TradeCount>=0;TradeCount--)
      {

then use OrderSelect()

OrderSelect(TradeCount,SELECT_BY_POS,MODE_HISTORY);

then you can get its Type and Profit . . .

OrderType()    OrderProfit()  
 

> the last trade has the highest number, so count down . . .

Is that reliable - I think the collection is the content in the terminal window at that moment &.. is in the order of the records at that time

So I always go through this by checking highest dates/times - maybe ticket would work too..?

FWIW

-BB-

 
BarrowBoy:

> the last trade has the highest number, so count down . . .

Is that reliable - I think the collection is the content in the terminal window at that moment &.. is in the order of the records at that time

So I always go through this by checking highest dates/times - maybe ticket would work too..?

FWIW

-BB-

Bugger ! thanks for pointing that out . . . it probably works if the history is sorted by ticket number . . . can't select from the HISTORY pool by ticket number either.

Thanks BB :-)

 
jal_fr:
I would select the last trade in the history (closed) Type Buy or Sell and recover its profit and its type.
https://www.mql5.com/en/forum/135176
 

why not use faithful old linear changing time?

datetime OrderCloseTime() 
 
Thank you for your help, I should be able to get by because of that.
Good trade for you.
Regards.
 
BarrowBoy:

> the last trade has the highest number, so count down . . .

Is that reliable - I think the collection is the content in the terminal window at that moment &.. is in the order of the records at that time

So I always go through this by checking highest dates/times - maybe ticket would work too..?

FWIW

-BB-

Hi BB

I created a little script this morning to test some of this, my own code looks for the last losing trade in the order history and also the number of last consecutive losing trades in the order history so getting this right is relevant to my code too . . . what I have found from my script is that regardless of how I sort the Account History the position i.e. SELECT_BY_POS doesn't change.

Perhaps I have misunderstood what you meant ? anyone else care to comment or clarify ?

 

This was been discussed ad nausium one or two years ago.

By counting down, the first one matching (MN/pair) is currently the latest closed.

But, the documentation does not state that is must be and could change on new versions, so the paranoid among us use the date to make sure.

    static datetime lastClose;  datetime lastClosePrev = lastClose;
    for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
    &&  OrderCloseTime()    > lastClosePrev             // not yet processed,
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL // Avoid cr/bal https://www.mql5.com/en/forum/126192
    ){
        lastClose = OrderCloseTime(); ...
Unless you know your EA will never open several orders, you must process the entire list anyway, so the direction is irrelevant.
 
Great, thanks for the confirmation/explanation. :-)