Loading…

Close All Losing

RISK WARNING: this script realises losses deliberately and there is no undo. The confirmation prompt shows the exact amount before anything happens. Read the number. There are two normal reasons to run it. The first is an event - a central bank release, a holiday weekend, a session close - where the losing positions are precisely the ones you do not want to carry through it. The second is housekeeping on a basket that has spread out, cutting the tail so the margin goes back to the positions that are working. The trap in every naive version of this script is the profit figure. OrderProfit() in MQL4, and POSITION_PROFIT in MQL5, is the raw price result and nothing else. Swap is charged separately and commission is charged separately. A position that looks flat on price is usually a loser once the costs are in, and a position that looks like a small loser on price can be several times worse. Filtering on the raw figure picks the wrong set: it leaves genuine losers open and it can pick up positions that are not really losing at all. This script filters on the net result. On MQL4 that is profit plus swap plus commission, all three of which the platform exposes on an open order. On MQL5 commission is not a position property at all, so swap is always counted and the entry commission is read back out of the deal history - see the platform notes for exactly what that figure covers. Two further decisions matter. The threshold is a positive magnitude. Ask for losses of at least 50 and you type 50, and the script applies MathAbs to whatever you type, so 50 and -50 mean the same thing. Both readings are the one you intended, and a winner can never match no matter what you type. A "cut the losers" script that could be made to close winners by a missing minus sign is not safe to keep on a hotkey. Each position is re-checked immediately before its close is sent. Price keeps moving while the script works through the list, and a position that has recovered into profit is no longer what you asked to cut. It is left open and counted as skipped. You can switch that off if you would rather close exactly the set you agreed to in the prompt. Notes and limitations - Market positions only. Pending orders are not touched - an unfilled order has nothing to lose yet. - This is the script that costs you money when you run it by mistake. Keep InpConfirmFirst on unless you have a specific reason not to. - Cutting the losing side of a hedge leaves the winning side unhedged and fully exposed. On a hedged book that is usually the opposite of what you want. Check what is actually hedging what before running this. - Skipped is not failed. A skipped position recovered into profit and was left open on purpose; the summary counts the two separately. - Swap is credited at rollover, not continuously, so a position can cross the threshold at a specific time of day rather than gradually. - A FIFO account can refuse to close positions out of order. The refusal comes from the server and is reported as-is. - Nothing here can close a position while that market is shut, which is exactly when you are most likely to want to. Those are reported as failures with the reason given. - An EA still running will re-enter. Stop it first.

Script · MT4 · v1.0 · Free

About Close All Losing

Close All Losing is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 8 settings, such as Min loss to close, Current symbol only and Magic filter. MQL source code included.

Files in the download: CloseAllLosing.ex4, CloseAllLosing.mq4, CloseAllLosing.txt.

Settings

SettingDefaultWhat it does
Min loss to close0.0Minimum net loss to close, as a positive number
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 closes every magic)
Recheck at closetrueRe-check the loss immediately before each close
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per order 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 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria