Close All Profitable
Taking the winners off the table is a normal thing to want to do: before a release, before a weekend, or when a basket has drifted far enough that the profitable half is worth banking. Doing it by hand across a dozen positions is slow and you will mis-click at least once. 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 showing plus 2.10 on the price can be minus 4.60 once the costs are counted, particularly on a carry-negative pair that has been held over a few nights. A script that filters on the raw figure closes that position and reports it as a win, and the account balance disagrees. 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 below for exactly what that figure covers. Two further decisions matter. The threshold is a positive magnitude. Ask for winners of at least 50 and you type 50, not -50, and the script applies MathAbs to whatever you type. A "close the winners" script that could be made to close losers by a stray minus sign is not safe to keep on a hotkey, and the test is written as "greater than zero AND at least the threshold" so a threshold of zero still means every winner rather than everything. Each position is re-checked immediately before its close is sent. Price keeps moving while the script works through the list, and a marginal winner surveyed five seconds ago can be a loser by the time its turn arrives. Closing it then would be the exact mistake the script exists to prevent, so 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 no profit to be in. - The net result is what the terminal reports at that instant. It moves between the survey and the close, which is why InpRecheckAtClose exists. - Skipped is not failed. A skipped position simply stopped qualifying and was left open on purpose; the summary counts the two separately. - Closing the winners out of a hedged book leaves the losing side unhedged and the margin requirement can rise, not fall. Check your free margin before running this on a hedged account. - Swap is credited by the broker 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.
Script · MT5 · v1.0 · Free
About Close All Profitable
Close All Profitable is an expert advisor (trading robot) for MetaTrader 5. It has 9 settings, such as Min profit to close, Current symbol only and Magic filter. MQL source code included.
Files in the download: CloseAllProfitable.ex5, CloseAllProfitable.mq5, CloseAllProfitable.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 |
|---|---|---|
| Min profit to close | 0.0 | Minimum net profit to close (account currency) |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 closes every magic) |
| Add entry commission | true | Include entry commission from the deal history |
| Recheck at close | true | Re-check profit immediately before each close |
| 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 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 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: MT5 Script · MQL5 Source Code