Position Netting Helper
A hedging account - always true on MQL4, optional on MQL5 - lets a buy and a sell on the same symbol sit as two separate tickets, each paying its own spread history, swap and margin, even though their combined net exposure is smaller than either one alone. This EA periodically finds the oldest matching buy and the oldest matching sell on each symbol (filtered by InpMagicNumber) and merges them with OrderCloseBy (MQL4) or CTrade::PositionCloseBy (MQL5), the function each platform provides for closing one order or position against an opposite one on the same symbol. This does NOT change net exposure. Merging a buy against a sell leaves the account holding exactly the same directional position it already had - the merge only reduces the number of open tickets and the margin tied up holding both sides of an already-offsetting pair. It is housekeeping, not a trading decision. It DOES realise the profit or loss on the smaller side of every pair it merges, immediately, at market, the moment the two are merged. Two tickets a trader intended to manage independently - different stops, different exit plans, different reasons for being open - are merged the same as any other opposing pair as long as their magic numbers match, so InpMagicNumber should be scoped to the strategy this EA is meant to tidy up after, not left at 0 without thinking about what else shares the account. Notes and limitations - Some brokers reject OrderCloseBy/PositionCloseBy outright on certain account types (for example, US-regulated FIFO/no-hedging accounts). This EA detects a non-retryable rejection, logs it once per symbol per pass, and moves on rather than hammering a call the broker will never accept. - Netting only ever happens within one symbol - a buy on EURUSD is never merged against a sell on GBPUSD, since that would change exposure rather than merely tidy it up. - Pairs are merged oldest-first. If volumes differ, the smaller side of the pair is fully closed and the larger side remains open with its volume reduced by the smaller side's amount, keeping the original ticket of the larger side. - This EA never opens a new position and never changes net direction or net lot size on any symbol - only the ticket count and margin usage change. - On MQL5, attaching this EA to a netting or exchange account is harmless but pointless - it logs that plainly once and stays idle rather than scanning for something that cannot exist.
Expert advisor · MT5 · v1.0 · Free
About Position Netting Helper
Position Netting Helper is an expert advisor (trading robot) for MetaTrader 5. It has 4 settings, such as Magic number, Max retries and Retry wait ms. MQL source code included.
Files in the download: NettingHelper.ex5, NettingHelper.mq5, NettingHelper.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Magic number | 20260709 | Magic number to match, 0 = match every magic number (use with care - see notes) |
| Max retries | 5 | Attempts per PositionCloseBy call before giving up on that pair this pass |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Scan interval sec | 30 | Seconds between checks |
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 → Experts.
- 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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Hedging · Buy/Sell Arrows · MT5 Expert Advisor · MQL5 Source Code