Loading…

Position Aggregator

An account can hold several buy orders and several sell orders on the same symbol at once - a grid EA, a hedge, or simply several strategies sharing one MagicNumber-less account. Answering "what is my real average entry price on the buy side" or "what is this symbol's combined open P/L" then means writing an OrderSelect loop with a running weighted sum, and getting the weighting right (by lots, not by order count) is easy to get wrong under time pressure. AggregatePositions does that scan once and returns both sides together in a single struct: net volume, lot-weighted average open price, and total profit - OrderProfit, OrderSwap and OrderCommission summed, not just the price P/L - for buy and for sell, each with its own order count. Returning one struct rather than the ten separate out-parameters two sides x five fields would need keeps the buy result and the sell result unambiguous at the call site, and it compiles cleanly under #property strict, so there was no reason to reach for out-parameters here. Both sides come from the SAME loop. A version that aggregated buys and sells with two separate function calls would scan the same order pool twice for no reason - on an account with a long order history that is double the work for a number that was available in one pass. PaNetExposure reduces the struct to a single signed number - positive net long, negative net short, zero when the two sides cancel out - useful for a quick hedge or exposure check without reading both fields yourself. Notes and limitations - avgOpenPrice is 0.0 when a side's netVolume is 0.0 - there is nothing to divide by, and 0.0 is the correct "not applicable" value here rather than a fabricated price. Always check netVolume (or orderCount) before reading avgOpenPrice as meaningful. - This is a snapshot, not a subscription. Call it again after anything that might have changed the position/order state - a fill, a close, a modify - rather than caching the struct. - On a hedging account both sides can be non-zero for the same symbol at once, by design - that is exactly the case this header exists to summarise cleanly. This applies to MQL4 (hedging is the only model it supports) and to an MQL5 account running in hedging mode. - Pending orders are intentionally excluded on both platforms. A future revision could add a separate pending-order summary if a submission needs one; this one stays focused on open exposure.

Script · MT5 · v1.0 · Free

About Position Aggregator

Position Aggregator is a script for MetaTrader 5. MQL source code included.

Files in the download: PositionAggregate.mq5.mqh, PositionAggregate.txt, PositionAggregateDemo.ex5, PositionAggregateDemo.mq5.

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

MQL4 & MQL5 scripts · PolyMaria