Loading…

Close Sells Only

Not every change of mind is a change of mind about everything. News breaks against the short side of a hedged book, or a session flips and the sells are the half that no longer makes sense. Closing the lot is the wrong answer, and clicking through the Trade tab one row at a time is slow - which is expensive, because the reason you are doing it is that price is moving. This closes the shorts and nothing else. It exists as its own script rather than as a direction drop-down on a general one because the moment you need it is the moment you should not be reading a dialog carefully. Three things separate it from a ten line loop. A short is bought back at Ask. A long is sold at Bid. Published versions of this script routinely send the wrong side of the quote, and the bug is invisible until the spread is wide enough for the server to reject the price - which is exactly the market condition you are running the script in. The exit price here is chosen from the order's own direction. Tickets are collected before anything is closed. Iterating the order pool by position index while closing is the single most common bug of this kind: the pool re-indexes the moment an order leaves it, so a naive forward loop silently skips roughly half the book and reports success. Failures are retried, but only the ones that deserve it. A requote, an off quote, a busy trade context or a dropped connection are temporary and worth another attempt. An invalid volume or a disabled account is permanent, and retrying it only burns seconds and fills the journal. Slippage is entered in pips and converted to points internally, so the number means the same thing on a 3 or 5 digit broker as on a 4 digit one. At the end you get a count of what closed, what failed, and the total result including swap and commission. Notes and limitations - Market positions only. Pending sell stops and sell limits are left alone. Cancelling an unfilled order is a different intention and a script that quietly did both would be untrustworthy in a hurry. - A short that hits its own stop or target mid-run is treated as already closed and counted as a success, not a failure. - The MQL4 trade context is shared across the terminal. If an EA is mid-order the script waits for it rather than failing, which can add a short delay. - Nothing can close a position while that symbol's market is shut. Those are reported as failures with the broker's reason. - The MQL5 version judges success by looking for the position again after the request, not by the return code, because a partially filled close reports success and leaves a remnant behind. The remnant is retried. - No partial closing. This closes whole positions.

Script · MT4 · v1.0 · Free

About Close Sells Only

Close Sells Only 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: CloseAllSells.ex4, CloseAllSells.mq4, CloseAllSells.txt.

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 closes every magic)
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per position 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: Hedging · Trading Sessions · News Trading · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria