Loading…

Close Above Profit Threshold

Harvesting is a decision most traders make in bulk. The book is green, something has run further than expected, and you want the winners past a certain size banked without touching the ones still working. Doing that by hand means reading a profit column, doing arithmetic, and clicking - per position, while the numbers keep changing under you. The design question in a script like this is not how to close a position. It is what "profit" means, and the honest answer is that it means two different things. Money is what the account banks. It accounts for volume, for the pip value of the instrument, and for the account currency, so ten units of profit on gold and ten on EURUSD are genuinely the same amount of money. It is the only unit that compares across a mixed book. Pips measure the move the trade was set up to catch. They ignore position size entirely, which is the point when you are judging whether a setup did what it was supposed to do rather than how much it paid. They also ignore carry, and that gap can be large: a position twenty pips up after three weeks of negative swap can be losing money. Neither is correct in general, so both are offered as an enum and the summary states which one was used. Whichever you choose, the journal lists every qualifying position with both numbers side by side, so the difference between them is in front of you before you commit. The pip measurement uses the exit side of the quote - Bid for a long, Ask for a short. Comparing against the entry side is the usual mistake and flatters every open position by the full width of the spread, which on a tight threshold decides whether a trade qualifies at all. The spread is already paid for in the number this script tests. In money mode, swap and commission are included by default and can be switched off. Including them is the honest figure - it is what actually lands in the balance. Switching them off gives the raw market result, which is what you want if you are comparing against a target set from the chart rather than from the account. The usual correctness work is here too. Tickets are collected before anything is closed, because the order pool re-indexes the instant an order leaves it and a naive loop skips roughly half the book. Temporary errors are retried and permanent ones are not. Slippage is entered in pips and converted to points. A threshold at or below zero is refused, because that is a different script - closing everything not currently losing is a much larger action than harvesting winners. Notes and limitations - Market positions only. Pending orders have no gain to test. - Pip mode ignores swap and commission entirely. That is the definition of the unit, not an oversight, and it is why the journal prints the money figure next to the pip figure for every position. - Pip mode is not comparable across instruments. Twenty pips on a JPY cross, on gold and on an index are three unrelated amounts of money. Use currency mode on a mixed book. - If the terminal has never synchronised a symbol, its point size reads as zero. Such a position measures as zero gain and is skipped rather than closed on a guess. - The floating figures move between the scan and the close. A position that qualified by a fraction may realise slightly less than the threshold, or slightly more. - Nothing can close a position while that symbol's market is shut. Those are reported as failures with the broker's reason. - The script runs once when you run it. It is not a trailing take profit and not a background monitor. Those need an EA. - The MQL5 version judges success by looking for the position again after the request rather than by the return code, because a partially filled close reports success and leaves a remnant. The remnant is retried.

Script · MT4 · v1.0 · Free

About Close Above Profit Threshold

Close Above Profit Threshold is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 9 settings, such as Unit, Threshold and Include costs. MQL source code included.

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

Settings

SettingDefaultWhat it does
UnitGAIN_CURRENCYMeasure the gain in
Threshold10.0Close at or above this gain
Include coststrueMoney mode: count swap and commission
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: Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria