Hedge All Open Positions
Sometimes you want the floating result to stop moving without realising it - before a central bank announcement, at the end of a session, or while you work out what to do. Closing gives up the entries. Hedging keeps them and stops the clock. Three decisions make this version different from the obvious one. The first is that it covers NET exposure per symbol, not each position individually. A symbol holding 1.0 lots of buys and 0.6 lots of sells is already 0.6 hedged. Covering each leg separately would send 1.6 lots of new orders to freeze 0.4 lots of real exposure, and charge you spread and margin on every one of them. The script sums the book per symbol first, works out the whole plan before it sends anything, skips symbols that are already balanced, and shows you the real total in the confirmation. Position-by-position covering is still available as an input for people who need the ticket-level pairing. The second is that the cover carries no stop loss and no take profit. This is an invariant in the code, not a default you can change. A cover with a target on it is a bomb: it can hit that target minutes later, close itself, and leave the original position naked at a moment when you have stopped watching precisely because you thought the book was frozen. If your cover needs a target then it is not a cover, it is a trade, and it should be placed as one. The third is the netting refusal, and it is the reason the MQL5 file exists separately rather than as a translation. On an MQL5 netting account there is at most one position per symbol and an opposite order nets against it. Selling one lot against a one lot long does not hedge it, it closes it. A script that ported the MQL4 logic straight across would therefore liquidate the entire book while reporting that it had protected it. This one reads ACCOUNT_MARGIN_MODE, refuses anything that is not ACCOUNT_MARGIN_MODE_RETAIL_HEDGING, names the mode it found, and sends nothing at all. The rest is ordinary care. Cover volumes round UP onto the broker's step grid, never down, because an uncovered fragment left open is the exact failure the script exists to prevent, and the division is nudged by an epsilon first because 0.1 divided by 0.01 is 9.99999999999999998 in binary arithmetic. Slippage is entered in pips and converted to points. Temporary refusals are retried, permanent ones are not. On MQL5 the two retcodes that mean the account model itself forbids the request - hedge prohibited and FIFO close - are called out in plain English rather than left as a number, because those two are the ones people mistake for a bug in the script. Notes and limitations - Hedging is not free and it is not the same as being flat. You still pay the spread to open the cover, you still pay swap on both legs - often negative on both - and the position is frozen at whatever the market was when the cover filled, not at the price you saw when you clicked. - Free margin matters. On brokers that charge full margin on both legs, hedging a large book can use more margin than the original positions did. Check before you run it. - Some brokers block hedging on individual symbols or enforce FIFO even on a hedging account. Those covers are refused by the server; the script reports the retcode with a plain-English explanation and leaves that symbol exposed rather than pretending otherwise. - A net exposure smaller than the broker's minimum volume cannot be covered exactly. The script rounds up, which slightly over-hedges the symbol, and says so in the journal. - It does not track which cover belongs to which original position. Give the covers their own magic number if you need to unwind them selectively. - Nothing is closed, ever. If a cover is refused, the original position is untouched and still exposed. - The MQL5 version does nothing at all on netting accounts. That is the correct behaviour, not a missing feature.
Script · MT4 · v1.0 · Free
About Hedge All Open Positions
Hedge All Open Positions is an expert advisor (trading robot) for MetaTrader 4. It has 7 settings, such as Current symbol only, Net per symbol and New magic. MQL source code included.
Files in the download: HedgeAllPositions.ex4, HedgeAllPositions.mq4, HedgeAllPositions.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 |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Net per symbol | true | Cover net exposure per symbol, not each position |
| New magic | 0 | Magic number written on the covers |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per cover before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before hedging |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 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 · Trading Sessions · MT4 Script · MQL4 Source Code