Loading…

Close Best Position

Banking the best trade is a common end-of-session habit: take the standout winner off the table, leave the rest of the book to work. Picking it by eye in a crowded Trade tab is exactly how the second-best position gets closed by mistake. Two decisions separate this script from the naive version of itself. First, the ranking number. Raw price profit - OrderProfit() in MQL4, POSITION_PROFIT in MQL5 - ignores swap and commission, which are charged separately. On positions held across a few nights those costs routinely reorder the leaderboard: the trade showing the biggest price gain is not always the trade that pays the most when closed. The MQL4 version ranks on profit plus swap plus commission. The MQL5 version ranks on profit plus swap, because MQL5 keeps commission on the deals rather than the position, and the figure is labelled "before commission" everywhere it appears rather than pretending to a precision it does not have. Second, what happens when the target disappears. The best position can hit its own take profit in the second between the scan and the close. A script that quietly falls back to the runner-up has just closed a position you were never shown and never confirmed. This one reports that the target closed itself and stops. Run it again and the new best is named first, like the old one was. If nothing on the book is in net profit, nothing happens. A "close best" script that would fall back to closing the least bad loser is a trap, not a tool, so the requirement is a strictly positive net result. The confirmation prompt shows the exact ticket, symbol, direction, volume, net result and open time of the position about to be closed. You agree to a specific ticket, not to an idea. Notes and limitations - Market positions only. Pending orders have no profit to rank. - Exactly one position is closed per run. Run it repeatedly to bank winners one at a time, confirming each. - The net result moves between the prompt and the close. The summary reports the figure as of the close; on MQL4 it is read back from the closed order in history, on MQL5 it is the last figure seen before the position vanished. - If two positions carry an identical net result the one found first is kept as the pick; the prompt names it either way. - On a FIFO account the server can refuse to close positions out of order. The refusal is reported as-is.

Script · MT4 · v1.0 · Free

About Close Best Position

Close Best Position is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Current symbol only, Magic filter and Slippage pips. MQL source code included.

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

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 considers every magic)
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts 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