Partial Close Chain Resolver
A partial close on MQL4 does not shrink the ticket you are looking at. The terminal closes the original ticket for the requested volume and opens a brand new ticket carrying the remainder, at the same open price and time, and stamps its comment "from #<original ticket>" automatically. A position partially closed three times therefore leaves four separate closed orders in history, each with its own slice of profit, swap and commission, and none of the four represents the trade as a whole. Every statistic computed by reading history order-by-order - win rate, average trade, profit factor, expectancy - is quietly wrong on any account that uses partial closes, because it counts one trade as several and averages across pieces that were never independent decisions. This script reads every closed market order in history, parses each comment for the "from #" marker MT4 itself writes, and links each continuation ticket back to the ticket it continued from. Starting from every ticket that has no parent in local history - genuinely original opens, or the earliest link still visible if history retention has aged an earlier segment out - it walks forward through the chain to the final full close, summing lots, profit, swap and commission across every segment and computing the volume-weighted average exit price. The result is one printed line per real trade: total volume, how many segments it took to close, open and close time, open price, average exit, and the reconciled totals. A chain whose earliest visible ticket still carries a "from #" comment pointing at a ticket not present in local history is reported as incomplete, not folded silently into the numbers as if it were the whole trade - the true open time and lots for that trade are unknowable from local history alone, and pretending otherwise would produce a confidently wrong number rather than an honestly partial one. Notes and limitations - This depends entirely on MT4's own "from #" comment convention. An account history that has been edited, imported from another platform, or where a broker's bridge overwrites order comments will not link correctly - those segments are reported as separate single-segment trades rather than merged incorrectly. - The parent-lookup is a linear search repeated per segment, so cost grows with the square of the number of closed orders. On an account with tens of thousands of closed orders this can take a noticeable moment; it is a one-shot Script, not something run every tick, so this was accepted rather than adding complexity for a case that is rare in practice. - A chain flagged INCOMPLETE means the true original ticket is not visible in local history. The totals shown for that chain start from the earliest visible segment, not the true original open - treat the open time and lot size for that specific trade as unreliable, the rest of the account's chains are unaffected. - Close price is reported as the volume-weighted average across every segment, which is the honest single number for a position that exited in pieces at different prices - it is not any single fill's actual price. - This is a read-only reporting tool. It does not close, modify or open anything.
Script · MT5 · v1.0 · Free
About Partial Close Chain Resolver
Partial Close Chain Resolver is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Symbol filter, Magic filter and Only multi segment. MQL source code included.
Files in the download: PartialCloseChainResolver.ex5, PartialCloseChainResolver.mq5, PartialCloseChainResolver.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Symbol filter | Only chains on this symbol, blank = every symbol | |
| Magic filter | -1 | Only chains with this magic, -1 = every magic |
| Only multi segment | false | Only print chains that were actually partially closed |
| Write csv | false | Also write a CSV report to MQL5\Files\ |
| Csv file name | PartialCloseChains.csv | CSV file name, written under MQL5\Files\ |
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: Volume · Alerts · MT5 Script · MQL5 Source Code