Loading…

Order Selection Helpers

Almost every EA needs the answer to a handful of the same questions: how many of my own trades are open right now, how much volume do they add up to, what is their combined profit. The naive way to find out is a fresh selection loop at every call site, which multiplies the same few lines of boilerplate across the codebase and multiplies the chance one of the copies gets the filter wrong. This library answers those questions in one call each: CountOrders, SumLots, SumProfit, HasOrder and CollectTickets, all built on the same shared filter so the definition of "matches" cannot drift between them. magic == -1 and symbol == "" both mean "any" - the two filters a caller almost always wants to loosen independently, for example "all my orders on any symbol" versus "any EA's orders on this symbol". SumProfit adds swap and commission to the raw price P/L, not just the price move alone. Leaving those out understates what a position has actually cost, and that is the number that matters for a real equity or risk check - a filter that only reads the price P/L will report a healthier account than the one actually sitting behind it. The internal scan is a plain forward loop over whichever pool(s) a match can come from. This catalogue iterates backward when closing or deleting orders, because a pool re-indexes the instant an order leaves it. Nothing here removes anything - every function is read only - so there is nothing to survive, and forward is the more natural order for an accumulation. CollectTickets is the one function that returns the actual tickets rather than a count or a sum, and it is built as two passes - count matches, size the array once, then fill it - rather than growing the array one element at a time. Notes and limitations - MQL4's MODE_TRADES holds both open positions and pending orders. MQL5 keeps these in two separate pools, both of which this library scans and combines transparently - pass an explicit type if you need one kind and not the other. - These functions read only. If you need to close, delete or modify matching trades, collect the tickets with CollectTickets first, then act on that array on your own terms - never mutate while scanning a live pool in a forward loop. - History (closed and deleted orders) is not covered on either platform. These functions look only at what is currently open or pending. - No caching. Each call re-scans the relevant pool(s), which is the correct behaviour for correctness but means calling several of these back to back on a large book does repeat the scan. Use CollectTickets once and derive the rest yourself if that matters for your use case. - On MQL5, SumProfit's commission recovery makes one HistorySelectByPosition() call per matching open position - inexpensive for a normal basket, but not free; avoid calling SumProfit every tick on a very large position count if commission precision does not matter for that call site.

Script · MT5 · v1.0 · Free

About Order Selection Helpers

Order Selection Helpers is a script for MetaTrader 5. It has 1 setting, such as Magic filter. MQL source code included.

Files in the download: OrderHelpers.mq5.mqh, OrderHelpers.txt, OrderHelpersDemo.ex5, OrderHelpersDemo.mq5.

Settings

SettingDefaultWhat it does
Magic filter-1Magic number to filter by, -1 = any

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: Volume · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria