Loading…

Trade History Analyzer

MetaTrader stores balance operations - deposits, withdrawals, credit adjustments - in the same history pool as closed trades, tagged as OP_BALANCE and OP_CREDIT rather than OP_BUY or OP_SELL. Summing "profit" across the whole pool without filtering those out is the single most common mistake in home-grown trade analyzers, and it is an easy one to miss because the resulting numbers still look plausible - net profit simply comes out too high by however much the account was topped up, and nothing about the output flags that anything went wrong. Every aggregate in this header is built only from entries where OrderType() is OP_BUY or OP_SELL. Pending orders that were later cancelled do not appear in that set either, because a cancelled pending order was never a trade. AnalyzeHistory scans the full closed-trade history with three optional filters - symbol, magic number, and a time window - and returns a TradeStats struct covering win rate, gross profit and loss, net profit, profit factor, per-trade expectancy, average and largest win/loss, the longest streaks of consecutive wins and consecutive losses, and average holding time in minutes. A trade with net profit of exactly 0.0 counts toward neither streak and breaks both, since it is genuinely neither a win nor a loss. Net profit per trade is OrderProfit() + OrderSwap() + OrderCommission(), never OrderProfit() alone - swap and commission are real costs (or, rarely, real income) that a win-rate calculation based on raw profit alone would misstate, particularly for a strategy that holds positions overnight or trades a symbol with a wide commission structure. This is a read-only scan of history that is never modified during the call, so the "iterate backwards when closing" rule from this catalogue's house style does not apply here - that rule exists for pools you are actively deleting from, and a forward read over a stable pool is both correct and simpler to reason about. Self-contained: no dependencies beyond the standard library, no DLLs. Notes and limitations - profitFactor is a large sentinel value (999999.0), not infinity, when there is gross profit and zero gross loss. Treat a value at or near that sentinel as "no losing trades in this filter", not as a precise ratio. Identical on both platforms. - Holding time uses OrderCloseTime() - OrderOpenTime() on MQL4, and the exit deal's DEAL_TIME minus the position's earliest DEAL_ENTRY_IN deal time on MQL5 (see MQL4 VERSUS MQL5 DIFFERENCES). On both platforms, a position that was partially closed and then closed again produces more than one history entry - this header counts each one separately, which is correct for profit but means a single "trade" the user thinks of as one position may appear as more than one row in totalTrades. On MQL5 specifically, a position that was also partially OPENED in more than one fill has its holding time measured from the EARLIEST entry deal for every one of its exit deals - a documented approximation, not a distinct open time per partial fill. - On MQL5, HistoryFindPositionOpenTime returns 0 (excluded from the average) if a position's entry deal has already aged out of the terminal's history depth while its exit deal is still present - an edge case with no MQL4 equivalent, since MQL4's single closed-order record cannot half-expire. - No results on a fresh account or a filter that matches nothing is reported explicitly by the demo (totalTrades = 0) rather than left blank - do not mistake that for an error in the header. Identical on both platforms. - This is a read-only historical scan. It does not touch open positions, and it has no effect on the account. Identical on both platforms.

Script · MT5 · v1.0 · Free

About Trade History Analyzer

Trade History Analyzer is a script for MetaTrader 5. It has 3 settings, such as Symbol filter, Magic filter and Lookback days. MQL source code included.

Files in the download: HistoryAnalyzer.mq5.mqh, HistoryAnalyzer.txt, HistoryAnalyzerDemo.ex5, HistoryAnalyzerDemo.mq5.

Settings

SettingDefaultWhat it does
Symbol filterLeave blank for every symbol, or type one exactly (e.g. EURUSD)
Magic filter-1Magic number filter, -1 = every magic number
Lookback days30How many days back the filtered example covers, 0 = no limit

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria