Reverse All Positions
You were long and you are now convinced you should be short. Doing that by hand across a dozen positions takes long enough that the reason you wanted to do it has usually gone. This script does the whole book in one pass. The ordering matters and it is not the ordering most people pick. The obvious approach is to open the opposite side first, so that you are never flat, and then close the original. That reads better and it is wrong, because its failure mode is double exposure: if the close is then refused - margin, market closed, a frozen ticket, a lost connection - you are holding both sides at full size and paying for both. Closing first fails to flat. Flat is the safe place to fail, and the script says loudly which positions ended up there. The second decision is that a reversal is opened with no stop loss and no take profit. This is an invariant in the code, not an input. A reversal that inherits the original's targets is a position sized and placed for the trade you just abandoned, and it can hit one of those levels within seconds of being opened. When it does, one leg of a book you thought you had reversed quietly pops itself and the rest keeps running. Set your stops deliberately afterwards, on the trade you actually mean to be in. On an MQL5 netting account the whole thing collapses into something simpler and better. An opposite order does not hedge on netting, it nets, so a sell of two lots against a one lot long leaves a one lot short - the reversal, in one deal. No flat window in the middle, one execution instead of two, and one fewer request that can be refused. The script detects the margin mode and uses that path automatically, falling back to close-then-open only when double the volume would break the broker's per-order maximum. Everything else is the usual care. Positions are snapshotted before anything is closed, because a closed order cannot be read back for its symbol and volume. Volumes are re-normalised to the broker's step, minimum and maximum with an epsilon guard, since 0.1 divided by 0.01 is not 10 in binary arithmetic. Slippage is entered in pips and converted to points. Temporary refusals are retried and permanent ones are not. Pending orders are never touched: a pending has no direction to reverse yet, and cancelling it would be a different intention wearing the same name. Notes and limitations - Reversing is not free. Each position pays the spread again, and commission again, on both legs. Reversing a large book costs real money before the new direction has done anything at all. - The new positions have no protective stops. That is deliberate and it is the single thing most likely to hurt you if you run this and walk away. - A close that succeeds followed by an open that fails leaves that exposure flat. The script counts those separately and warns about them by name in the journal. Read it. - Partial fills are possible on either leg. A partially filled reversal leaves a smaller position than the original; the script reports the send as accepted because the server accepted it, and the Trade tab is the authority on the final size. - The magic filter on an MQL5 netting account is of limited use. A netted position carries the magic of whichever deal last modified it, so filtering by magic there does not mean what it means on a hedging account. - Symbols whose market is closed cannot be reversed and are reported as untouched, which is correct: the original is intact. - Pending orders are ignored entirely, by design.
Script · MT4 · v1.0 · Free
About Reverse All Positions
Reverse All Positions is an expert advisor (trading robot) for MetaTrader 4. It has 7 settings, such as Current symbol only, Magic filter and New magic. MQL source code included.
Files in the download: ReverseAllPositions.ex4, ReverseAllPositions.mq4, ReverseAllPositions.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 |
| Magic filter | -1 | Reverse only this magic (-1 reverses every magic) |
| New magic | 0 | Magic for the reversals (-1 copies the original) |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per order before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before reversing |
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: Volume · MT4 Script · MQL4 Source Code