Order History Iterator
OrdersHistoryTotal() does not only contain closed trades. It also contains balance and credit operations, which have no real symbol, no meaningful trading type, and no place in a win-rate or profit-factor calculation. Code that loops the history pool without filtering these out gets numbers that are subtly wrong in a way that is easy to miss until a payout or a deposit skews the statistics. This header filters OP_BUY and OP_SELL only, once, in HistoryCollectTickets, and every statistic function downstream works from that clean ticket array instead of the raw pool. The filter also takes an optional magic number, symbol and close-time range, so "how did strategy X do on EURUSD in the last 30 days" is one function call rather than a hand-rolled loop with four conditions in it. The collect-then-compute shape matters for anything asking more than one question of the same result set. Instead of re-scanning the entire history pool once per statistic, the ticket array is built once and every statistic function - win rate, profit factor, average win, average loss - iterates that array instead. Profit factor is the one figure in this file with a genuine edge case: with zero losing trades, gross-loss is zero and the ratio is undefined. Rather than return a divide-by-zero result or an arbitrarily large number, HistoryProfitFactor returns -1.0 as an explicit "undefined" sentinel, and the demo shows exactly how to detect and report it. Notes and limitations - Filtering happens against the exit event's timestamp (OrderCloseTime() on MQL4, the exit deal's DEAL_TIME on MQL5), not the open time. "Trades closed in the last N days" and "trades opened in the last N days" are different questions - this header answers the first, on both platforms. - History size on most terminals is bounded by the account history depth set in the platform, not by this header. Very old trades may already be gone from the pool before this code ever sees them. - HistoryProfitFactor returning -1.0 is deliberate, not a bug: with zero losses the ratio is mathematically undefined, and pretending otherwise (returning 0, or a huge number) would mislead anything that displays or compares it. - Ticket arrays are snapshots. If trading continues while a report is mid-build, results reflect the pool as it was at collection time, not a live view. - On MQL5, a position closed by several partial closes produces one exit deal per partial close, all sharing the same DEAL_POSITION_ID - each is counted as its own entry in tickets[], the same way MQL4 records each partial close as its own history entry. Neither platform's header collapses partial closes back into a single "trade".
Script · MT5 · v1.0 · Free
About Order History Iterator
Order History Iterator is a script for MetaTrader 5. It has 2 settings, such as Magic filter and Recent days. MQL source code included.
Files in the download: HistoryIterator.mq5.mqh, HistoryIterator.txt, HistoryIteratorDemo.ex5, HistoryIteratorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic filter | 990099 | Magic number used for the "no match" filter test |
| Recent days | 30 | Window size for the date-range filter test |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code