Loading…

Breakeven Engine

"Move to breakeven" is usually implemented as OrderModify(ticket, OrderOpenPrice(), ...), and that is wrong. A stop set exactly at the entry price still loses the spread the moment it is hit, and if the account pays commission it loses that too. Breakeven means the price at which the trade nets to zero after costs, not the price it was opened at. This header computes that price. Spread is covered by a buffer you supply in pips - typically the current spread plus a small margin. Commission is optional: pass a round-turn cost per lot in account currency and it is converted to a price distance through the symbol's tick value, the same number the terminal uses internally, rather than a hardcoded points-per-dollar guess. The commission input is volume-independent by design - a per-lot rate produces the same breakeven distance regardless of how many lots are open, so the caller never has to re-derive it per position size. The second half of the problem is that a computed price is not automatically sendable. Two server-side limits can reject a modify and they fail for different reasons: STOPLEVEL means the stop is too close to the current price for this order type, and FREEZELEVEL means the order is frozen near its own stop or take profit and cannot be touched at all right now, in either direction. Treating both as one generic "modify failed" makes the failure impossible to act on. CheckBreakevenModifiable tells you which one is blocking, so a caller can decide to wait rather than retry into the same rejection every tick. Nothing in this header calls OrderModify. A library that trades on your behalf hides its retry policy and error handling inside itself, and every caller inherits whatever choices it made silently. Returning a price and a status code instead keeps that decision - and the responsibility for slippage and retries - where it belongs, in your own order-management code. Notes and limitations - The function never reads live order data itself - it takes entry price, order type and symbol as plain arguments, so it works equally on a real open position or a hypothetical one you are planning. - Commission adjustment silently becomes 0.0 if the tick value is not yet available for the symbol (MODE_TICKVALUE on MQL4, SYMBOL_TRADE_TICK_VALUE on MQL5). Read it once on startup (or open the Market Watch symbol) so the server has published it before relying on this. - Swap is not covered. A position held overnight accrues swap that this buffer does not account for; add it separately if you carry positions past rollover. - CheckBreakevenModifiable reflects the account's current stop and freeze level at the moment it is called - both can change with volatility on some brokers, so check immediately before sending, not once at trade open.

Script · MT5 · v1.0 · Free

About Breakeven Engine

Breakeven Engine is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders and moves the stop loss behind price (trailing stop / break-even). It has 2 settings, such as Buffer pips and Commission per lot. MQL source code included.

Files in the download: BreakevenEngine.mq5.mqh, BreakevenEngine.txt, BreakevenEngineDemo.ex5, BreakevenEngineDemo.mq5.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Buffer pips2.0Spread buffer added on top of entry, in pips
Commission per lot7.0Round-turn commission per lot, account currency

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Trailing Stop & Break-even · Pending Orders · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria