Loading…

Incremental Trade Statistics Engine

A live performance panel that wants an up-to-date win rate or profit factor usually gets it the expensive way: loop OrdersHistoryTotal() from scratch, every tick, and recompute everything. That cost grows with the account's entire trading history and is repeated on every single tick regardless of whether a trade actually closed since the last check. This library flips that around. Feed each newly-closed trade's net profit into RegisterTrade() exactly once, and every statistic - win rate, profit factor, expectancy, average win, average loss, largest win, largest loss, maximum consecutive wins/losses, current streak, net profit, equity peak and maximum drawdown - is a plain running accumulator update, O(1) regardless of how many trades have already been registered. A dashboard built on this only ever does per-tick work proportional to "did a new trade close", never proportional to account history length. Every ratio is guarded against its degenerate case: profit factor returns 0.0 rather than +INF when no losses have been registered yet, win rate and expectancy exclude break-even trades from their divisor rather than silently miscounting them as wins or losses, and drawdown is measured against a running equity peak so it needs no external starting balance. The demo cross-checks every incremental reading against a brute-force O(n) recompute over the identical trade sequence, so the claim that the running accumulators match the reference calculation is verified in the Experts log, not just asserted here. Notes and limitations - RegisterTrade() must be called with each trade's NET profit (profit + swap + commission) exactly once per closed trade - feeding a trade twice, or feeding gross profit while forgetting swap/commission, will silently skew every downstream statistic. - Break-even trades (net profit exactly zero) count toward TotalTrades() and reset the current streak, but are excluded from the win-rate and expectancy denominators - they are neither a win nor a loss. - Drawdown is measured against a synthetic running equity peak built purely from registered trades, starting from zero - it is NOT the account's real balance-based drawdown unless every closed trade on the account has been registered from a genuinely empty starting point. - ProfitFactor() returns 0.0, not +INF, when no losing trade has been registered yet - callers displaying this should treat 0.0 as "not yet meaningful", not as a literal profit factor of zero. - BruteForceRecompute() exists purely for verification; calling it in a live per-trade loop defeats the entire point of this library.

Script · MT4 · v1.0 · Free

About Incremental Trade Statistics Engine

Incremental Trade Statistics Engine is a script for MetaTrader 4. It has 1 setting, such as Trade count. MQL source code included.

Files in the download: IncrementalTradeStats.mqh, IncrementalTradeStats.txt, IncrementalTradeStatsDemo.mq4.

Settings

SettingDefaultWhat it does
Trade count300Total synthetic trades fed through the engine

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: Dashboards & Scanners · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria