Loading…

Move All Stops To Breakeven

Almost every breakeven script sets the stop to the entry price. That is not breakeven. A position closed at its entry price still owes the commission it was charged and whatever swap has accrued since it opened, so a stop parked on the open price locks in a small guaranteed loss and calls it safety. On a scalping account with round-turn commission it is not even a small one. This script computes the number properly. It reads the commission and swap already booked on each position, converts that money into a price distance using the symbol's tick value and tick size, and offsets the stop by it in the direction the trade would have to travel. Because the conversion goes through tick value, it is correct on gold, on indices and on JPY crosses, all of which break any implementation that assumes a pip is worth ten currency units per lot. If your broker charges half the round turn on entry and half on exit, the booked figure only covers the first half, which is what InpCommissionPerLot is for. On the spread, this file deliberately disagrees with most of the ones you will find. A buy is entered at the Ask and its stop is triggered on the Bid. A stop placed at the entry price therefore already yields zero gross - the spread is inside the entry price, and adding it a second time as a "cost" is double counting. The script still offers the spread as an addition, but labels it honestly: it is a slippage cushion. A stop is a market order once it triggers, you get whatever is on the other side, and a spread's worth of headroom is a reasonable way to stop a breakeven exit from turning into a one pip loss. That is a real argument. "The spread is a cost you have not paid yet" is not. The other half of the job is validation, and it is the half that decides whether you can trust the summary. A stop closer to the market than the broker's stop level is rejected outright, and a position already sitting inside the freeze level cannot be modified at all, whatever value you send. Both are checked before anything goes to the server - measured against the correct side of the market, the Bid for a buy and the Ask for a sell, which is where a surprising number of implementations get it backwards and only find out on a volatile symbol. Positions that fail either check are reported as skips with the reason printed per ticket: not far enough past its costs yet, frozen by the broker, or no usable tick value from the server. They are never counted as successes. The final alert breaks the run into moved, already at or beyond breakeven, too close, frozen, unconvertible and refused, so the number you read is the number that happened. By default a stop is never moved backwards. A position whose stop has already been trailed past breakeven is a better position than one at breakeven, and dragging it back would be handing profit away. That is an input, so you can force it if you want uniformity. Notes and limitations - A stop at breakeven is not free. It converts trades that would have dipped and recovered into scratches, and on a strategy with a wide natural stop it can cut the expectancy badly. The script does not have an opinion about whether you should be doing this. - Swap keeps accruing after the stop is set. A position held for another week will drift back below breakeven by the swap it pays. Re-run the script, or add a buffer that covers the holding period you expect. - Brokers with a zero MODE_STOPLEVEL are not necessarily permissive. Several use dynamic, spread-linked minimum distances that the terminal does not publish at all. Those rejections come back from the server and are reported as refusals. - The take profit is never touched. Neither is the open price. This script moves stop losses and nothing else. - Positions with no stop at all are given one. If that is not what you want, use the magic filter to narrow the run. - On a symbol the terminal has never synchronised, tick value can come back as zero. The script refuses to guess and skips the position, rather than silently treating the costs as free and putting the stop in the wrong place. - It does not check whether an EA will immediately move the stop back. If a trailing expert owns the position, it will win.

Script · MT4 · v1.0 · Free

About Move All Stops To Breakeven

Move All Stops To Breakeven 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 Current symbol only, Magic filter and Buffer pips. MQL source code included.

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

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Only this magic (-1 means every magic)
Buffer pips1.0Extra distance beyond breakeven, in pips
Commission per lot0.0Commission still to be charged, per lot, account currency
Use booked coststrueAlso cover the commission and swap already on the ticket
Add spreadtrueAdd the current spread as a slippage cushion
Only improvetrueNever move a stop backwards
Max retries5Attempts per position before giving up
Retry wait ms500Pause between attempts, milliseconds

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: Scalping · Trailing Stop & Break-even · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria