BreakEvenTrailingMarket
- Utilities
- Version: 1.0
BreakEven Trailing is a utility for MetaTrader 5 that manages the stop loss of positions you already hold. It moves the stop loss to break-even and trails it behind price, so you do not have to watch the chart to protect an open profit.
The EA does not open positions and does not close positions by market order. It only modifies the stop loss of existing positions. An exit happens when price reaches the stop loss that the EA has placed.
WHAT IT DOES
- Moves the stop loss to the open price, or to the open price plus a chosen offset, once the position reaches a defined profit in pips.
- Trails the stop loss at a fixed distance behind the current price once the position reaches a defined profit in pips.
- Break-even and trailing can each be enabled or disabled, and can be used together.
- Works with both BUY and SELL positions.
- Never moves an existing stop loss to a worse level. The stop loss only moves in the direction that protects more profit.
- Keeps the existing take profit unchanged when it modifies the stop loss.
- Shows the current settings, the account type, and the working state on the chart.
WHICH POSITIONS ARE MANAGED
The EA manages every position on the symbol of the chart it is attached to.
- Magic numbers are not checked. Positions opened manually, by another EA, or by copy trading are all managed.
- Positions on other symbols are never modified.
- Pending orders are never modified.
- Only the stop loss is changed. Volume, take profit and position direction are left as they are.
Because the EA does not filter by magic number, do not run two programs that modify the stop loss of the same symbol at the same time. They would overwrite each other. Attach the EA to one chart per symbol.
HEDGING AND NETTING ACCOUNTS
On a hedging account, each position is managed separately by its ticket. Two positions on the same symbol can have different break-even levels, because each one has its own open price.
On a netting account, the platform keeps one aggregated position per symbol. Several fills are merged into a single position with an average open price, and that average price is the break-even reference. Separate break-even levels for individual fills are not possible on a netting account, and a new fill changes the average open price of the whole position. The EA reads the current aggregated position on every tick, so it always works from the latest average price.
The detected account type is shown on the chart panel as Hedging or Netting.
HOW THE STOP LOSS IS SELECTED
When break-even and trailing are both enabled, both conditions can be met at the same time. The EA does not send two modification requests in that case. On each tick it performs the following steps:
1. Calculates the break-even candidate: open price, plus or minus the offset in the profit direction.
2. Calculates the trailing candidate: current price minus or plus the trailing distance.
3. Rounds each candidate to the tick size of the symbol, always to the side that does not move the stop loss closer to the current price than requested.
4. Checks each candidate separately against the stops level and the freeze level of the symbol.
5. Compares the candidates that are valid, and sends only the one that protects more profit: the higher stop loss for a BUY position, the lower stop loss for a SELL position.
If one candidate cannot be placed because of a broker restriction, the other candidate is used when it is valid. A candidate is never replaced by a worse level in order to force a modification through.
BROKER RESTRICTIONS
Many brokers do not accept a stop loss that is too close to the current price, and some brokers block modification of a position while price is inside a freeze zone. The EA checks the stops level and the freeze level before sending a request, and it also reads the return code from the trade server after sending one.
When a stop loss cannot be placed, the EA waits and retries on the next tick. It does not move the stop loss to a worse level, and it does not disable the function. The reason is shown on the chart panel, so you can see whether the EA is waiting for the market or whether the server refused the request.
Note that some brokers report a stops level of zero and still apply a dynamic restriction on the server side. In that case the final result is decided by the return code of the trade server, which the EA reports on the panel and in the log.
INPUT PARAMETERS
Break-even
- Enable break-even (default: true). Turns the break-even function on or off.
- Profit to trigger break-even, in pips (default: 10.0). The floating profit the position must reach before the stop loss is moved.
- Offset beyond open price, in pips (default: 0.0). Distance from the open price in the profit direction. Use it to cover spread, commission and swap.
Trailing stop
- Enable trailing stop (default: true). Turns the trailing function on or off.
- Profit to start trailing, in pips (default: 15.0). The floating profit the position must reach before trailing starts.
- Distance from price to stop, in pips (default: 10.0). The distance kept between the current price and the stop loss while trailing.
Chart panel
- Show settings and status on chart (default: true).
- Panel font size (default: 12). Values are limited to the range 8 to 24.
Values are validated when the EA starts. If a value cannot work, the EA reports it and does not start. If a combination is allowed but unusual, the EA starts and writes a warning to the log. Two cases produce a warning: an offset larger than the break-even trigger, which means the stop loss can only be placed after price advances further, and a trailing start smaller than the trailing distance, which means the first trailing stop can be worse than the open price.
CHART PANEL
The panel shows the version, the symbol, the account type, the state of both functions with their settings, the number of managed BUY and SELL positions, the number of modifications made on the current tick, and the current status.
Status values include:
- ACTIVE: the EA is monitoring positions.
- WAITING: NO POSITION: there is no position on the current symbol.
- WAITING: NO QUOTE: no valid bid and ask are available yet.
- WAITING: STOP LEVEL: the candidate stop loss is closer to price than the broker allows.
- BLOCKED: FREEZE LEVEL: price is inside the zone where the broker does not allow modification.
- WAITING: MARKET CLOSED, WAITING: PRICE OFF, WAITING: PRICE CHANGED, WAITING: TOO MANY REQUESTS: temporary market or server conditions.
- ALL FUNCTIONS OFF: both break-even and trailing are disabled.
- AUTO TRADING OFF, EA TRADING DISABLED, ACCOUNT TRADE DISABLED, EXPERT TRADE DISABLED, SERVER AUTO TRADING OFF, DISCONNECTED: trading is not permitted in the terminal, for this EA, on this account, or the terminal is not connected.
- ERROR: INVALID STOPS, ERROR: TRADE DISABLED, ERROR: SYMBOL SETTINGS, ERROR: MODIFY FAILED: the request was refused. The return code is written to the log.
Repeated errors on the same ticket are written to the log only on the first occurrence, when the return code changes, and once per minute after that, so the log stays readable. When a position recovers after an error, a recovery line is written.
PIP DEFINITION AND SUPPORTED SYMBOLS
All distances are set in pips, not in points.
- Symbols quoted with 3 or 5 digits: one pip is ten points.
- Symbols quoted with 2 or 4 digits: one pip is one point.
This definition follows standard forex quotation. Metals, indices and crypto symbols are outside the supported scope of version 1.00, because the pip definition for these instruments is not consistent between brokers. On such a symbol the calculation would still run, but the meaning of one pip may not match your expectation, so please verify the resulting distances on a demo account before using the EA there.
WHAT BREAK-EVEN MEANS HERE
Break-even in this EA means the open price recorded by the platform for the position. It does not include commission, swap, slippage or the difference between bid and ask at the moment of the exit. A position closed exactly at the open price is therefore not guaranteed to produce a result of exactly zero.
If you want the stop loss to cover your own trading costs, set the offset beyond the open price to a value that matches those costs on your account.
HOW TO USE
1. Attach the EA to the chart of the symbol you want to manage. Use the timeframe you prefer; the EA works on every tick and does not depend on the timeframe.
2. Enable algorithmic trading in the terminal.
3. Set the trigger and distance values in pips according to your trading style and the typical range of the symbol.
4. Check that the panel shows ACTIVE while a position is open, and WAITING: NO POSITION when there is none.
5. Open one chart per symbol. Do not attach the EA twice to the same symbol.
The EA can be left on the chart permanently. It does nothing while there is no position on the symbol.
REQUIREMENTS
- MetaTrader 5. Tested on terminal build 5836.
- Algorithmic trading enabled in the terminal and for the EA.
- An account that allows modification of the stop loss of open positions.
- No DLL, no WebRequest, no external files and no internet connection are used.
TESTING
Version 1.00 was tested on a demo account of a retail broker.
- Calculation tests: 46 automated checks passed. They cover BUY and SELL, break-even, trailing, both functions together, the tick before the trigger and the exact trigger, one candidate violating the stops level, both candidates violating it, protection of an existing stop loss, the freeze level applied to the stop loss and to the take profit, symbols with 2, 3, 4 and 5 digits, and rounding to the tick size in the correct direction.
- Integration test with the trading API on a hedging account: break-even for BUY and for SELL executed with return code 10009 (request completed), the existing take profit preserved, no failed requests. About 360,000 ticks were processed.
FREQUENTLY ASKED QUESTIONS
Does the EA open or close trades?
No. It only modifies the stop loss of positions that already exist. The position is closed by your broker when price reaches that stop loss.
Does it work with trades from another EA or from a signal?
Yes. Magic numbers are not checked, so any position on the symbol is managed. For that reason, do not use it together with another program that modifies the stop loss of the same symbol.
What happens if the terminal is closed?
The EA cannot work while the terminal is closed. Stop loss levels that were already placed remain on the server and are still valid, because they are server side orders.
Can I use break-even without trailing?
Yes. Each function has its own switch. If both are disabled the EA stays idle and shows ALL FUNCTIONS OFF.
Why is the stop loss not moving even though the profit target is reached?
The most common reason is a broker restriction. Check the panel: WAITING: STOP LEVEL means the requested stop loss is closer to price than the broker allows, and the EA is waiting for a valid distance instead of placing a worse stop loss.
Does it work on a netting account?
Yes. On a netting account there is one aggregated position per symbol, and the average open price is used as the break-even reference.
VERSION
1.00 - First release.
