MQL5 How to find lastest history order ticket?
MQL5 How to find lastest history order ticket?
Loading saved threads...
Abbotton · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Abbotton
Original post: https://stackoverflow.com/questions/78447537
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am writing an EA program using MQL5, and now I encounter a problem. I want to get the order number of the last closed order (as shown below, the target order number is
2308697611
). I have tried many methods but cannot solve this problem.
The last attempt was to use the following code:
HistorySelect(0, TimeCurrent());
ulong ticket = HistoryDealGetTicket(HistoryDealsTotal() - 2);
But these two lines of code will only take effect if the positions are closed sequentially. If I close the positions randomly, I will not get the expected results.
Quote
Report
4xPip Official · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: 4xPip Official
Original post: https://stackoverflow.com/a/79261487
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Using the following function, you can get the ticket number of last closed ticket in MQL5.
int lastClosedTicket()
{
HistorySelect(0, TimeCurrent());
uint total_trades = HistoryDealsTotal();
ulong ticket_number = 0;
for(int i =(int) total_trades - 1; i >= 0 ; --i)
{
if((ticket_number = HistoryDealGetTicket(i)) > 0)
{
return HistoryDealGetInteger(ticket_number, DEAL_TICKET)
}
}
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: 4xPip Official Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/a/79261487 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Using the following function, you can get the ticket number of last closed ticket in MQL5. int lastClosedTicket() { HistorySelect(0, TimeCurrent()); uint total_trades = HistoryDealsTotal(); ulong ticket_number = 0; for(int i =(int) total_trades - 1; i >= 0 ; --i) { if((ticket_number = HistoryDealGetTicket(i)) > 0) { return HistoryDealGetInteger(ticket_number, DEAL_TICKET) } } }
Checking account access…