Close Below Loss Threshold
READ THIS FIRST. This script is not a stop loss and cannot be used as one. A script only acts in the moment you run it. It cannot protect a position while you sleep, while your connection is down, while the terminal is closed, or across a weekend gap - and those are the four situations that actually empty accounts. A real stop lives on the broker's server and fires whether or not your machine is switched on. What this is for is the cleanup afterwards. You have four trades that went the wrong way, you have decided they are done, and you want them out in one action rather than in sixteen clicks while they keep sliding. It is a last resort, used deliberately, with your eyes open. If you find yourself running it on a schedule, what you actually need is stops on the server or an EA that manages them. With that said, the measurement work in it is the same as in its profit side twin, and it matters just as much. The threshold can be money or pips, chosen from a drop-down, because the two answer different questions. Money is what the account gives up, and it is the only unit comparable across a mixed book. Pips measure how far the trade went wrong, ignoring size and ignoring carry - and that gap can be wide, because a position only ten pips offside can be considerably deeper in the red once three weeks of negative swap are counted. The journal prints both readings for every qualifying position, so you see the discrepancy before you commit rather than afterwards. Both units are entered as positive numbers, and this is deliberate. A loss limit typed as a negative is the classic sign-flip bug in scripts of this kind: the comparison inverts, and instead of closing the four positions that are down fifty, the script closes every position that is not down fifty. The input here never carries a sign at all. Internally the loss is computed as a positive magnitude, so a winning position produces a negative number that can never satisfy a positive threshold, and there is no branch in which the sense can flip. Pip distance is measured to the exit side of the quote - Bid for a long, Ask for a short - so the spread counts against the trade exactly as it will when the close actually happens. Measuring from the entry side, as most published versions do, understates every loss by the width of the spread. A threshold of zero is refused. Zero would close every position that is not currently in profit, which is a far larger and more expensive action than the one the title describes. The rest is the standard correctness work: tickets collected before anything is closed so the re-indexing order pool cannot cause skipped positions, temporary errors retried and permanent ones not, slippage entered in pips and converted to points, and a full record of what was closed and what it cost written to the journal. Notes and limitations - THIS IS NOT A STOP LOSS. It runs when you run it and at no other time. It cannot act overnight, during a disconnection, or over a weekend gap. Put real stops on the server. The script prints this to the journal on every single run. - It is also not a trailing stop, not an equity guard, and not a background monitor. Each of those needs an EA, not a script. - Market positions only. Pending orders have no loss to test. - Pip mode ignores swap and commission entirely. That is the definition of the unit, and it is why the journal prints the money figure beside the pip figure for every position. - Pip mode is not comparable across instruments. Twenty pips on a JPY cross, on gold and on an index are three unrelated amounts of money. Use currency mode on a mixed book. - If the terminal has never synchronised a symbol, its point size reads as zero. Such a position measures as zero loss and is skipped rather than closed on a guess - deliberately the safe direction to fail in. - The floating figures move between the scan and the close, so a position may realise slightly more or slightly less than the threshold. - Nothing can close a position while that symbol's market is shut, and a gapped market is exactly when a loss threshold is breached. Those are reported as failures with the broker's reason, and the summary tells you plainly that the loss is still running. - 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 - a half closed loser being the worst outcome this script could leave behind. The remnant is retried.
Script · MT4 · v1.0 · Free
About Close Below Loss Threshold
Close Below Loss Threshold is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 9 settings, such as Unit, Threshold and Include costs. MQL source code included.
Files in the download: CloseBelowLoss.ex4, CloseBelowLoss.mq4, CloseBelowLoss.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Unit | LOSS_CURRENCY | Measure the loss in |
| Threshold | 50.0 | Close at or beyond this loss (positive) |
| Include costs | true | Money mode: count swap and commission |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 closes every magic) |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts per position before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before closing |
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: Alerts · MT4 Script · MQL4 Source Code