Hey there, fellow traders! Today, we’re diving into a crucial aspect of MQL4 programming: implementing the onTrade event handler. This handy little feature helps us keep tabs on our active orders, making our trading experience much smoother.
To kick things off, we’re using a CArrayInt to track order tickets. By monitoring changes to this list in the onTimer function, we can trigger specific event handlers whenever certain actions take place. Here’s a quick rundown:
//+------------------------------------------------------------------+ //| Event handler when stop loss is hit | //+------------------------------------------------------------------+ void onStopLoss(ulong ticket); //+------------------------------------------------------------------+ //| Event handler when take profit is hit | //+------------------------------------------------------------------+ void onTakeProfit(ulong ticket); //+------------------------------------------------------------------+ //| Event handler when a new order is opened | //+------------------------------------------------------------------+ void onTradeEntry(ulong ticket); //+------------------------------------------------------------------+ //| Event handler when an order is closed(removed) | //+------------------------------------------------------------------+ void onTradeExit(ulong ticket);
Note: This implementation doesn’t cover all the bells and whistles of the MQL5 onTradeTransaction handler. Think of it as a solid starting point!
Check out the output log for the EA in the screenshot below:

So there you have it! With these event handlers in your toolkit, you’ll be better equipped to manage your trades effectively. Happy trading!
연관 포스트
- Master Your Trades with MoveStopLoss for MetaTrader 4
- How to Access Forex Factory News in MetaTrader 4 Using Web Requests
- Streamline Your Trading with a Fixed Interval Close Order EA for MetaTrader 4
- Unlocking the Power of Probability: A Forex Expert Advisor for MetaTrader 5
- How to Use Check Trade Time Function in MetaTrader 4 for Optimal Trading