Why initial lot sizing is not enough
A risk-based expert advisor can size an order correctly at the signal price and still exceed its intended risk after execution. Partial fills, price drift and a missing protective stop can change the actual exposure. This MT5 strategy architecture separates pre-trade sizing from a second, execution-aware risk reconciliation step. The objective is to detect deviations from the planned loss budget, not to guarantee a maximum possible loss.
From a breakout signal to a risk budget
As an illustrative entry model, use a confirmed close above the highest high of the preceding 20 completed bars. Choose a fixed protective price below the signal low, estimate the stop loss in account currency, round the requested volume down to the permitted lot increment, and skip the entry if even the minimum lot exceeds the budget. The following example focuses on what happens after order submission rather than implementing the breakout itself.
Assume a USD account and standard EURUSD contract size of 100,000 units per lot. The intended buy is 1.0 lot at 1.1000 with stop 1.0980. At USD 10 per pip per lot, the planned loss is USD 200, excluding costs. Suppose 0.6 lot fills at 1.1000 and 0.4 lot fills at 1.1005. The first leg has USD 120 estimated stop loss; the second has USD 100. The weighted entry is 1.1002, and total estimated stop loss is USD 220, ten percent above budget.

Hypothetical EURUSD fills: the original budget is USD 200, but the combined filled position implies USD 220 of loss at the original stop before fees and slippage.
Calculate from the actual position, not the original request
MQL5's OrderCalcProfit estimates financial results in the account currency under current market conditions. Use the actual position volume, average entry and active stop, rather than multiplying the requested volume by a hard-coded pip value. The helper below handles one long position ticket. A zero stop must be treated as missing protection, not as zero risk. A stop above the entry may imply no gross loss relative to entry at that stop price, but does not remove execution risk.
// Long-only helper: estimate current position loss at SL.
// Returns false when position data or protection is invalid.
bool EstimateLongStopRisk(ulong ticket, double &risk)
{
risk = 0.0;
if (!PositionSelectByTicket(ticket))
return false;
if (PositionGetInteger(POSITION_TYPE) != POSITION_TYPE_BUY)
return false;
string sym = PositionGetString(POSITION_SYMBOL);
double volume = PositionGetDouble(POSITION_VOLUME);
double entry = PositionGetDouble(POSITION_PRICE_OPEN);
double stop = PositionGetDouble(POSITION_SL);
if (volume <= 0 || entry <= 0 || stop <= 0)
return false;
double pnl = 0.0;
if (!OrderCalcProfit(ORDER_TYPE_BUY, sym, volume,
entry, stop, pnl))
return false;
risk = MathMax(0.0, -pnl);
return true;
}
// Add inside the EA's event handler.
// The flag must be declared at EA scope.
bool riskDirty = true;
void OnTradeTransaction(const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
riskDirty = true;
}
// OnTick or OnTimer: reconcile strategy-owned positions
// and pending orders, then evaluate riskDirty.
// Clear the flag only after a successful reconciliation.
Reconcile after transaction events
Use OnTradeTransaction as a notification that account state may have changed. Do not assume one request produces one transaction or that events arrive in a convenient order. Keep the handler short and rescan the relevant live positions and working orders in OnTick or OnTimer. On a hedging account, sum estimated nonnegative stop losses for each strategy-owned ticket. On a netting account, use the combined position and avoid mixing independent strategies on the same symbol unless ownership is tracked separately. Include risk reserved for unfilled entry volume before admitting another order.
Handling excess risk
A deterministic policy can block new entries, cancel the unfilled remainder, verify that the active stop exists, and, if necessary, reduce existing exposure subject to broker volume constraints. Cancellation and reduction are requests, not confirmed outcomes: reconcile again after execution. In the example, reducing the net position from 1.0 to 0.9 lot at an unchanged average price would lower estimated remaining stop exposure to USD 198, excluding any loss realized on the reduction. A complete trade budget must also account for realized loss, fees and other pending exposure; do not widen the stop to solve an over-budget condition. This helper deliberately does not implement automatic close or modification requests.
Validation checklist and limitations
Test exact fills, partial fills at different prices, rejected modifications, a missing stop, duplicate or delayed transaction notifications, restart recovery, and concurrent working orders. Compare estimated stop loss with actual realized results after commission and slippage. Standard tester execution may not reproduce broker-specific partial-fill behavior; supplement it with scripted scenario tests and demo execution. The example has not been compiled or backtested here. Currency conversion can change, and stops can execute beyond their specified price. Leveraged trading may result in substantial losses.
For execution-aware risk budgets and order reconciliation, see Custom MT5 strategy Development Services.