No, its your code that is incorrect. You should use "<" instead of "<=" ...
for ( int i = 0 ; i < OrdersHistoryTotal() ; i ++ )
Thanks Fernando!
In fact, I didn't notice the obvious thing: indexing starts at zero, so the number of items in OrdersHistoryTotal() is not also the highest index of history total. Everything work good and function finds the closetime value of most recent order :
int FindIndexOfLastClosedPosition() { int index ; datetime LastPosition = 0 ; for ( int i = 0 ; i < OrdersHistoryTotal() ; i ++ ) { if(OrderSelect( i,SELECT_BY_POS,MODE_HISTORY )==false) { Print( "Access to history failed with error (",GetLastError(),"), index of checked position = ", i ); } else { if( OrderCloseTime() > LastPosition ) index = i ; if( OrderCloseTime() <= LastPosition ) index = index ; } } return index ; } datetime GetLastPositionCloseTime() { datetime LastCloseTime ; if(OrderSelect(FindIndexOfLastClosedPosition(),SELECT_BY_POS,MODE_HISTORY)==true) { LastCloseTime = OrderCloseTime(); if ( LastCloseTime > 0 ) Print("Close time for the most recent order ", LastCloseTime); } else Print("OrderSelect failed error code is",GetLastError()); return LastCloseTime ; }
Fixed here in case anyone needs a quick copy / paste in the future.
You are welcome!
Except LastPosition =0 and never changes.
Meaning, by default, the last order in your loop will always be last closed order index, which won't necessarily be the case?
I might look at doing it this way, inside your loop:
if(OrderCloseTime()>LastPosition ) {
index = i ;
LastPosition=OrderCloseTime();
}
Thanks Andrew !
I have never used OrdersHistoryTotal() before, so I was really surprised last week, cause I expected rather last order index = 0.
Maybe I haven't tested that enought, anyway it was also confusing, cause sometimes it seemed as if the numbering was not consecutive.
It is why I rather prefered to leave functions above, than believe each time I get last closed index in the last order.
It is safer this way for me, these funtions work - it is only quick work for my own purposes.
Someday I will look at this issue in more details,
Unfortunately in the coming days I have to let go of the MQL4 topic a bit because of other programming goals
-
Do not assume history has only closed orders.
OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017) -
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020) -
Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
"balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)Broker History FXCM Commission - <TICKET>
Rollover - <TICKET>>R/O - 1,000 EUR/USD @0.52 #<ticket> N/A OANDA Balance update
Financing (Swap: One entry for all open orders.)

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
Looking for a way to find the closing time of the latest position I have created the following functions :
When is selected position with index returned by OrdersHistoryTotal(), folowing error appears :
Access to history failed with error (4051), index of checked position = 2841
I have checked this on my both MT4 accounts - the same situations.
I have no open, pending orders at the time, so I don't understand what going on.
It seems this functions work properly, but I need to be shure will they work in the future.
Is this OrdersHistoryTotal() function property or am I doing something wrong ?