Create a simple news filter for XAUUSD trading on MT5 - MetaTrader 5 script
XAUUSD (Gold) is a popular trading instrument in the Forex market due to its high volatility and profit potential. However, major economic news events (such as non-farm payrolls data, Federal Reserve interest rate decisions, or European Central Bank announcements) can cause prices to rise significantly, creating risks for automated trading strategies. To manage these risks, the News Filter in the Expert Advisor can pause trading during high-impact news. In this article, I will show you how to create a simple news filter for XAUUSD trading on MT5 and share a practical code example to help you get started.
Why use news filters for XAUUSD trading?
News events related to the U.S. dollar, British pound, or euro often cause XAUUSD price to fluctuate significantly. For example, an announcement by the Federal Reserve to raise interest rates could cause gold prices to fall rapidly, triggering stops on automated trading systems or unexpected losses. News filters can help:
In the next section, I will provide a simple MQL5 code snippet to implement a news filter for your XAUUSD trading strategy.
//+------------------------------------------------------------------+ //| Simple news filter for XAUUSD trading | //+------------------------------------------------------------------+ #Property Copyright "Vivan Nguyen" #property- related ” https://www.mql5.com/en/users/wazatrader” # Property version "1.00" Enter integer number of minutes before news = 15 ; // Halt trading minutes before news release Enter integer number of minutes after news = 15 ; // Minutes after news release to resume trading // Simulated news time (replaced with real news data source for demonstration purposes) Date Time News Times [] = { D'2025.05.07 14:30:00' }; // Example: News at 14:30 on May 7, 2025 //+------------------------------------------------------------------+ //|Check if trading should be paused due to news| //+------------------------------------------------------------------+ BooleanIsNewsTime () { datetimecurrenttime = timecurrent (); for ( integer i = 0 ; i < array_size (news_times); i++) { datetime newstime = newstime[i]; Date and time start pause = news time - minutes before news release * 60 ; // Pause X minutes before news release Date and time end pause = news time + minutes after news release * 60 ; // Continue X minutes after news release if (current time >= start pause && current time <= end pause) { print ( "News Filter: Trading suspended due to upcoming news" , NewsTime); Return true ; // Pause transaction } } Returns wrong ; // safe transaction } //+----------------------------------------------------------------------------------+ //|Expert initialization function | //+------------------------------------------------------------------+ Integer initialization () { Return ( initialization successful ); } //+------------------------------------------------------------------+ //|Expert check function | //+------------------------------------------------------------------+ Blank check () { if (IsNewsTime()) { Return ; // Skip transactions during news time } //Add your XAUUSD transaction logic print here ( "XAUUSD Transaction Security" ); } //+------------------------------------------------------------------+ //|Expert to initialize function | //+------------------------------------------------------------------+ Blank solution initialization ( constant integer reason) { //Clean up code if needed } //+----------------------------------------------------------------------------------+
Code description
While the code above provides a basic news filter, you may need a more robust solution for XAUUSD trading. This is why I developed XAU OneShot EA MT5 , an intelligent trading system designed specifically for gold trading with advanced risk management features. It includes:
Incorporating a news filter into your XAUUSD trading strategy is a simple and effective way to manage risk during news volatility. The code provided in this article can be used as a starting point for your EA development. For a more comprehensive solution, try XAU OneShot EA MT5 to take your gold trading to the next level. Happy trading!
Attachment download
📎 simple_news_filter_for_xauusd_trading_on_mt5.mq5 (2.49 KB)
Source: MQL5 #59130
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •