Loading…

Close Buys Only

The usual reason to want this is unwinding a hedge by hand. You are long and short the same instrument, you have decided which leg is worth keeping, and you want the other side gone without touching anything else on the account. Doing it manually across several tickets is slow and one mis-click leaves you flat when you meant to be short. Two things separate this from the obvious loop. The direction test is exact. It matches OP_BUY on MQL4 and POSITION_TYPE_BUY on MQL5, not "anything that is not a sell". OP_BUYLIMIT and OP_BUYSTOP are buy orders in name and pending orders in fact, and a loose test picks them up and cancels resting orders you were relying on. Closing a live position and cancelling an unfilled one are different intentions and this script only does the first. Tickets are collected into a list before anything is closed. The pool re-indexes the moment an entry leaves it, so closing while iterating by index silently skips roughly half the longs - which on a one-sided flatten means you walk away believing you are flat and you are not. Both versions walk the list backwards and build the ticket list first. Beyond that: slippage is entered in pips and converted to points, so the value means the same thing on a 3 or 5 digit broker as on a 4 digit one. Temporary errors are retried and permanent ones are not. The prompt shows the count, the volume and the running result before anything happens. The MQL5 build additionally reads the account margin mode and changes what it tells you, because on a netting account the individual buys no longer exist. That is covered in full below. Notes and limitations - Market positions only. Buy limits and buy stops are pending orders and are not deleted. Use Delete Pending Orders By Type for those. - On a netting account the volume reported is net volume, not the sum of the buys that went into it. There is no way to recover the original legs from an open netted position. - Closing one side of a hedge changes the margin requirement, and on some brokers it rises rather than falls, because a hedged pair can be margined at a discount. Check free margin before running this on a large hedged book. - A FIFO account will refuse to close a long out of order, which is exactly the case this script creates. The refusal comes from the server, is not retryable, and is reported with the FIFO reason named rather than as a bare error number. - Some brokers and some instruments prohibit short positions entirely. On those the whole idea of a one-sided flatten is moot. - An EA still attached will re-enter long. Stop it first. - Nothing here can close a position while that market is shut.

Script · MT4 · v1.0 · Free

About Close Buys Only

Close Buys 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: CloseAllBuys.ex4, CloseAllBuys.mq4, CloseAllBuys.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 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: Hedging · Pending Orders · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria