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 Library
First, we need to define the event importance flags and import four functions needed for their correct use.
#define FLAG_IMPORTANCE_HIGH 16 #define FLAG_IMPORTANCE_LOW 8 #define FLAG_IMPORTANCE_MODERATE 4 #define FLAG_IMPORTANCE_NONE 2 #import "Filters_News.ex5" void SetMinutesBeforeAndAfter( int ); void OnTimerEvent(); inline bool TradingHastobeSuspended(); int OnNewDay( int flags); #import
Then, we create the following global variables:
input int MinutosBeforeAfter = 10; 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 ).
2. Configuration in OnInit
In the OnInit function, we will define how many minutes before and after the event the news will be filtered.
int OnInit() { 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.
void OnTick() { if(new_vela != iTime(_Symbol,PERIOD_D1,0)) { int flags = FLAG_IMPORTANCE_LOW | FLAG_IMPORTANCE_MODERATE | FLAG_IMPORTANCE_HIGH; OnNewDay(flags); new_vela = iTime(_Symbol,PERIOD_D1,0); } if(TradingHastobeSuspended() == false) { } }
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 event
The 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 .