Partial Close To Realise Target Money
"Take half off" and "close 0.3 lots" are both proxies for what a trader actually wants, which is usually a cash number: bank 50, bank 200, whatever the plan calls for at this stage of the trade. Getting from a cash target to a lot size means dividing by how much profit one lot is currently worth, and that only works cleanly when the position is actually in profit - dividing by a loss, or by zero, produces a nonsense volume that the naive version of this script would send anyway. This script reads the ticket's net profit - floating profit, swap and commission together, since all three are what actually lands in the account balance - and divides by the lot size to get a per-lot rate. The required volume is the target amount divided by that rate, rounded up to the broker's lot step so the bank is never short of the target, only ever slightly over it. If the rounded volume would leave a remainder smaller than the broker's minimum lot, the script closes the whole position instead of trying to hold an unholdable sliver, and says so plainly rather than silently rejecting the close. If the position is not in net profit at all, or the target exceeds its entire profit, the script refuses outright with a clear reason instead of computing a volume with no sensible meaning. Notes and limitations - The volume is computed from the current net profit per lot, which changes with every tick. Between the calculation and the order reaching the server the market can move, so the amount actually banked is an estimate, not a guarantee to the cent. - Rounding is always upward to the lot step, so the position may end up a little smaller than the mathematically exact answer would require - never smaller than what leaves the target short. - Swap and commission are assumed to scale linearly with volume, which is how most brokers charge them. A broker with a genuinely flat, non-proportional fee per ticket would make the per-lot rate slightly off for very small partial closes. - A ticket that is a pending order, not a market position, is refused - there is nothing to bank yet. - The script acts on one ticket per run. It does not scan the whole book looking for the best combination across multiple tickets.
Script · MT4 · v1.0 · Free
About Partial Close To Realise Target Money
Partial Close To Realise Target Money 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 Ticket, Target profit money and Slippage pips. MQL source code included.
Files in the download: PartialCloseToMoney.mq4, PartialCloseToMoney.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Ticket | 0 | Ticket to partial-close (0 = most profitable on this chart's symbol) |
| Target profit money | 50.0 | Cash amount to bank, in account currency |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Allow full close | true | Close the whole position when the target needs all of it |
| Max retries | 5 | Attempts before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Volume · Risk & Lot Size · Alerts · MT4 Script · MQL4 Source Code