Loading…

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 · MT4 · v1.0 · Free

About Close All Profitable

Close All Profitable 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 profit to close, Current symbol only and Magic filter. MQL source code included.

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

Settings

SettingDefaultWhat it does
Min profit to close0.0Minimum net profit to close (account currency)
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 closes every magic)
Recheck at closetrueRe-check profit 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: Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria