Programming a break-even mechanism in MQL5 involves creating an automated logic that monitors open positions and modifies the stop-loss (SL) to the entry price (or a slight profit buffer) once a trade reaches a predefined profit threshold. This process ensures risk is eliminated, securing your capital while allowing the trade to continue.
What is a Break-even Mechanism?
In algorithmic trading, a break-even (BE) point is a protective technique where the stop-loss of a trade is moved to the entry level (plus an optional commission buffer) once the market has moved in the trade’s favor by a specified number of points. This protects your account balance by ensuring that even if the trade reverses, you do not incur a financial loss.
3 Key Logic Components
- Trigger Points: The distance in points the price must travel in profit before the EA initiates the move.
- Lock Points (Buffer): An optional offset to cover transaction costs (spreads/commissions).
- Validation: The EA must verify the position has not already been moved to break-even to prevent spamming the broker’s server with redundant requests.
MQL5 Implementation Steps
To implement this functionality, you need to filter your active positions and use the CTrade class for modification.
1. Define Input Parameters
Make your EA flexible by defining inputs:
input group "Break-Even Settings"
input bool InpEnableBE = true; // Enable Break-even
input int InpTriggerPoints = 50; // Profit trigger in points
input int InpLockPoints = 5; // Profit buffer (lock) in points
2. The Management Function
This function loops through your open positions and updates the SL:
#include <Trade\Trade.mqh>
CTrade Trade;
void CheckBreakEven() {
for(int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket)) {
// Filter by Symbol and Magic Number
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentSL = PositionGetDouble(POSITION_SL);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
// Logic for BUY trades
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
if(SymbolInfoDouble(_Symbol, SYMBOL_BID) >= openPrice + InpTriggerPoints * point) {
if(currentSL < openPrice + InpLockPoints * point) {
Trade.PositionModify(ticket, openPrice + InpLockPoints * point, PositionGetDouble(POSITION_TP));
}
}
}
}
}
}
Technical Considerations Table
FAQ on Programming Break-even Point in Forex
1. Is setting break-even at exactly the entry price advisable?
It is usually recommended to add a small buffer (Lock Points). If you set it exactly at the entry price, spreads and commissions will still result in a net loss if hit.
2. How do I handle broker rejection?
Always ensure your requested price complies with the SYMBOL_TRADE_STOPS_LEVEL. If the broker rejects a request, log the error code to your trading journal and skip that ticket for the current tick.
3. Should I call this function in OnTick?
For high-frequency strategies, yes. For standard strategies, you can also use the OnTradeTransaction event, which triggers only when a trade activity occurs, saving CPU resources.
4. Can I use a “Virtual” Break-even?
Yes. You can store your target SL level in a variable and only send the actual PositionModify request to the server when the price touches your trigger. This keeps your stop-loss invisible to the broker.
Trade with Headway Broker
Implementing precise risk management requires an execution environment that you can trust. Headway FX trading broker provides the professional infrastructure necessary for running automated Expert Advisors, ensuring that your stop-loss modifications and exit orders are processed with low latency. Partner with a broker that understands the technical demands of automated trading.



