Uses for the MT5 On Trade Transaction Event
An automated strategy does not finish its work when an order request leaves the terminal. The broker may accept, reject, partially execute, or subsequently change the order, and each outcome alters what the algorithm should do next. In MT5, OnTradeTransaction() gives an Expert Advisor a way to respond to individual trade-related changes rather than infer them from the next price tick.
For mt5 developers, the event is particularly useful where execution state and intended exposure diverge. Its three parameters describe the transaction, and, for request-related notifications, the submitted request and server result. Five applications stand out.
Distinguishing Server Acceptance From Actual Execution
A successful order submission does not always mean a position exists. A request may produce an order that waits for execution, while another is rejected or filled immediately. Handling TRADE_TRANSACTION_REQUEST allows an EA to inspect the server result, but subsequent order and deal transactions are needed to establish what happened.
The distinction matters when a strategy places a CFD order during a thin session. Starting a trailing stop merely because the request was accepted can leave the algorithm managing exposure that has not yet been created. Track order tickets and confirmed deals separately, and do not assume transaction notifications arrive in a fixed sequence.
Accounting for Partial Fills Before Sending a Hedge
A large instruction can execute in several deals. TRADE_TRANSACTION_DEAL_ADD can alert the EA to each addition to deal history, where the executed volume, price, and direction can be retrieved. Hedging the requested quantity instead of the filled quantity may create an unintended position in the opposite direction.
Imagine a hypothetical equity-index CFD order to buy eight contracts, each worth $5 per index point. Only three contracts fill at 5,240 before liquidity thins. An EA that immediately sells eight contracts as a hedge, assuming the purchase was complete, could leave net short exposure of five contracts if the remaining buy quantity never executes. A subsequent 40-point rise would produce a $1,000 loss on that net short exposure, excluding charges. Hedging three confirmed contracts instead would initially neutralize the filled exposure. The event handler should reconcile cumulative deals and outstanding orders before sizing any offset.
Detecting Protective-Order Changes Made Outside the Algorithm
Not every transaction originates from the EA. A manual stop adjustment, broker-side operation, or another strategy can change the state on which its risk model relies. Relevant position and order transactions can prompt a fresh read of current server-side information.
An event notification is a reason to verify, not a complete substitute for verification. A position may already have changed again by the time the handler processes it. Compare actual stop levels and position volume with the strategy’s expected state before issuing another modification.
Identifying Cancellations That Leave a Strategy Unhedged
An EA may stage a pending order to offset an existing CFD exposure. If that pending instruction is canceled, expires, or is removed by the server, the original position may remain open without the anticipated hedge. Order deletion and history-related transaction types help the system detect that change.
In mt5 automation, an apparently quiet chart can therefore conceal a significant operational event. Record why the order disappeared when that information is available, inspect remaining exposure, and decide whether replacement is permitted under current margin and execution conditions.
Building an Audit Trail Without Blocking the Event Queue
OnTradeTransaction() can receive bursts of notifications during active execution. Lengthy database writes, network calls, or repeated calculations inside the handler increase the chance that newer transactions displace older ones in its finite queue. A more elaborate logging system can consequently produce a less complete record.
Keep the handler short, capture essential identifiers, and reconcile against order and deal history outside the immediate callback. Before opening a CFD position with an event-driven EA, test partial fills, rejected requests, and manual order changes on a demo account, then verify that its reconstructed exposure matches the broker’s actual positions.

