Loading…

Netting View Emulator

MT4 has no concept of a position at all, only a pool of independent tickets. Strategy logic designed around, or ported from, an MT5 netting account usually wants exactly one number per symbol - "am I net long, how many lots, at what average price" - not a loop over tickets that happens to sum to the same thing every time it is needed. Re-deriving that aggregation by hand at every call site invites the same order-pool bugs (forward iteration during a change, unfiltered magic numbers, pending orders mixed in with market orders) that the rest of this catalogue exists to head off. GetNetPosition() scans the real order pool for one symbol and magic number and returns one SNetPosition: net lots (positive long, negative short, zero flat), the volume-weighted average price of whichever side is net, and the summed profit+swap+commission across every contributing ticket - mirroring what PositionGetDouble() would report on an MT5 netting account for the same book. The aggregation itself (ComputeNetPosition) is a pure function over a caller-supplied leg array, kept separate from the order-pool scan so the arithmetic is exercised with synthetic data in the demo without needing a live hedged book to test against. PlanNettingOrder() goes a step further: given the book's current net lots and a desired netting-style order (direction and lots), it returns the ordered list of real MT4 actions needed to reach that state - closing existing opposing tickets first (largest first, to minimise the number of tickets touched), and only opening a new ticket for whatever remainder could not be absorbed by closing the opposing side. This is exactly the merge-or-reduce decision MT5's netting engine makes internally when a new order arrives on a symbol that already carries exposure, made explicit as a step list the caller can execute (or just log) one action at a time. Notes and limitations - An exactly hedged book (equal opposing volume) reports netLots = 0, the same as MT5 reporting no position - but unlike MT5, the MT4 book in this state is not actually empty, it still carries real tickets with real swap/commission history. GetNetPosition() still returns the correct summed profit for that case; only the average-price figure has no defined side to report from and is returned as 0.0. This is the one case where the emulation cannot be perfectly faithful to MT5, and it is called out explicitly rather than hidden. - PlanNettingOrder() is a planner, not an executor. It does not call OrderClose() or OrderSend() itself - the caller decides whether, and how, to carry out the returned step list, the same separation of planning from execution used elsewhere in this catalogue. - The planner closes the largest opposing tickets first to minimise the number of tickets touched per netting order. It does not attempt to minimise total commission or spread cost across ticket sizes - use CloseByPairOptimiser.mqh (a separate library in this catalogue) if spread-free CloseBy pairing rather than plain closing is the goal. - Pending orders carry no exposure and are excluded from both the aggregation and the planner, matching how an MT5 netting account's position has nothing to do with its separate pending-order pool.

Script · MT5 · v1.0 · Free

About Netting View Emulator

Netting View Emulator is a script for MetaTrader 5. It has 1 setting, such as Magic filter. MQL source code included.

Files in the download: NettingViewEmulator.mq5.mqh, NettingViewEmulator.txt, NettingViewEmulatorDemo.ex5, NettingViewEmulatorDemo.mq5.

Settings

SettingDefaultWhat it does
Magic filter-1Magic number to restrict the live scan to, -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: Hedging · Volume · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria