Loading…

Close By Magic Number

An account running four EAs is four independent books sharing one balance. When one of them starts behaving badly you need its trades out and the other three untouched, and you need that in one action rather than in forty clicks through the Trade tab while the other three keep trading. Most published versions of this script take a single magic number. That is not the situation people actually have. Shutting down two strategies by running a single-magic script twice is not the same as shutting them down together, because the market moves between the runs and the second strategy closes at a worse price than the first. This one takes a list. The list is the interesting part of the file, because parsing it naively is dangerous rather than merely broken. StringToInteger returns zero for anything it cannot read, so a stray letter, a pasted space or a smart quote silently becomes magic number zero - and zero is exactly what manually opened trades carry. A typo in the input box would quietly target every trade you placed by hand. Every token is therefore validated character by character before it is converted, and the script refuses the whole run on the first token it cannot read, naming it in the journal. An empty list is refused too. Combined with the invert option, an empty list would mean "close everything that is not in an empty set" - the entire account. Nobody typing into an empty box meant that. The parsed list is echoed to the journal and repeated in the confirmation prompt. Without that, a mistyped magic and a genuinely quiet account look identical: both close nothing. For a cleanup tool that is the worst possible failure mode, because you walk away believing it worked. Invert selection turns the script into the other half of the same job. List your EAs' magic numbers, tick invert, and it closes everything else - which in practice means the manual trades you opened while testing and forgot about. The usual correctness work is here as well. Tickets are collected before anything is closed, because the order pool re-indexes the instant an order leaves it and a naive loop skips roughly half the book. Temporary errors are retried and permanent ones are not. Slippage is entered in pips and converted to points, so it means the same thing on a 3 or 5 digit broker as on a 4 digit one. Notes and limitations - Market positions only. Pending orders carrying the same magic are left in place. Deleting an unfilled order is a different intention. - Manual trades usually carry magic 0, and 0 is a perfectly valid entry in the list if that is genuinely what you want to close. - The script cannot tell you which magic numbers exist on the account. It acts on what you type. Getting the numbers is a manual step. - On MQL4 the trade context is shared. If the EA you are shutting down is mid-order the script waits for it rather than reporting a failure. - Nothing can close a position while that symbol's market is shut. Those are reported as failures with the broker's reason. - Closing an EA's trades does not stop the EA. Remove it from its chart or turn off automated trading first, or it may simply open them again. - The MQL5 version judges success by looking for the position again after the request rather than by the return code, because a partially filled close reports success and leaves a remnant. The remnant is retried.

Script · MT5 · v1.0 · Free

About Close By Magic Number

Close By Magic Number is an expert advisor (trading robot) for MetaTrader 5. It has 7 settings, such as Magic list, Invert selection and Current symbol only. MQL source code included.

Files in the download: CloseByMagic.ex5, CloseByMagic.mq5, CloseByMagic.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Magic list12345,67890Magic numbers, comma separated
Invert selectionfalseClose everything EXCEPT these magics
Current symbol onlyfalseRestrict to this chart's symbol
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per position before giving up
Retry wait ms500Pause between attempts, milliseconds
Confirm firsttrueAsk for confirmation before closing

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 → Experts.
  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 expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria