Welcome to this blog. In this guide, I will show you an example of how to use the free News Filter library.
Introduction
This library allows you to filter live news according to its importance, but it has certain limitations:
- Only works in real time (not in tester mode).
- It uses OnTimer, so it is not compatible with programs that also use this event.
Given this, if your program already uses OnTimer , I do not recommend using this library , as it may interfere with its operation.
With these clarifications, let's see how to implement it in a simple Expert Advisor.
1. Define Variables and Import the LibraryFirst, we need to define the event importance flags and import four functions needed for their correct use.
// Define flags to be able to choose the importance of events #define FLAG_IMPORTANCE_HIGH 16 #define FLAG_IMPORTANCE_LOW 8 #define FLAG_IMPORTANCE_MODERATE 4 #define FLAG_IMPORTANCE_NONE 2 // Import the external library "Filters News.ex5" #import "Filters_News.ex5" void SetMinutesBeforeAndAfter( int ); //Function to set the minutes before and after for filtering void OnTimerEvent(); //Function that must be executed in the OnTimer event inline bool TradingHastobeSuspended(); //Funcoin that will verify if your strategy can be executed or not int OnNewDay( int flags); //Function that is executed every new day #import
Then, we create the following global variables:
// Input parameter to define minutes before and after an event to consider input int MinutosBeforeAfter = 10; // Variable to store the latest daily candle timestamp datetime new_vela;
Explanation:
- MinutosBeforeAfter: Defines how many minutes before and after an event trading will be suspended.
- new_vela: Stores the opening of the daily candle ( D1 ).
In the OnInit function, we will define how many minutes before and after the event the news will be filtered.
// Initialization function int OnInit() { // Set the number of minutes before and after an event for filtering SetMinutesBeforeAndAfter(MinutosBeforeAfter); return(INIT_SUCCEEDED); }3. Implementation in OnTick
In OnTick , importance flags will be defined to filter the news and check whether it can be traded or not.
// Function executed on every tick void OnTick() { // Check if a new daily candle has formed if(new_vela != iTime(_Symbol,PERIOD_D1,0)) { int flags = FLAG_IMPORTANCE_LOW | FLAG_IMPORTANCE_MODERATE | FLAG_IMPORTANCE_HIGH; // Set event importance OnNewDay(flags); // Process new day events based on the importance level new_vela = iTime(_Symbol,PERIOD_D1,0); // Update the last daily candle timestamp } // Check if trading needs to be suspended if(TradingHastobeSuspended() == false) { // Trading strategy logic goes here } }
Explanation :
- The opening of a new daily candle (D1) is detected.
- Importance flags are defined to filter relevant events.
- It is checked whether trading should be suspended before executing the strategy.
IMPORTANT: Inside the { } block of:
if(TradingHastobeSuspended() == false)
You must write your strategy code.
4.OnDeinit and OnTimer Configuration
Run the OnTimer eventThe OnTimer function will execute OnTimerEvent() every time the timer is triggered.
void OnTimer () { OnTimerEvent(); }
Remove the timer in OnDeinit
When the Expert Advisor is closed or deleted, we need to stop the Timer event to avoid problems.
void OnDeinit ( const int reason) { EventKillTimer (); }
Conclusion
With this implementation, we now have a news filter integrated into our trading system.
Important notes:
- The filter only works for the current symbol.
- Example: If you are on EURUSD, it will only filter EUR and USD news.
- It will not filter events from other peers .