How to add News Filter in EA MT4/MT5

How to add News Filter in EA MT4/MT5

29 April 2024, 18:29
Leolouiski Gan
0
127

Introduction

This article will explain how to directly integrate news data provided by The News Filter into your EA source code in order to make decisions. Some experience with MQL language is required, but extensive knowledge is not necessary as the methods provided by The News Filter is easy to use. Before proceeding, make sure that you are already familiar on how to use The News Filter and Filter This script as explained by The News Filter Guide.

To directly integrate the news logic inside your source code, you can either use the global variables method or the CSV file method.


Method 1: Global variables

This method allows you to determine whether a particular currency has ongoing news currently through the use of global variables. The news selection criteria and minutes before/after news are set using the Filter This script. You do not need to understand how global variables work in order to use this method within your source code. You can just copy and paste the following function at the bottom of your source code (works for both MT4 & MT5).

bool ThisChartNewsOngoing(string currency){
   string globalName = "#TNF:"+string(ChartID());
   if(!GlobalVariableCheck(globalName)){
      printf("Global variable corresponding to this chart not found.");
      return false;
   } 
   int val = (int)GlobalVariableGet(globalName);
   if(currency=="ANY"){
      if(val!=0) return true;
      else return false;
   }
   int index = -1;
   string currencyArr[9] = {"USD","EUR","GBP","JPY","AUD","CAD","CHF","NZD","CNY"};
   for(int i=0, n=ArraySize(currencyArr); i<n; i++){
      if(currencyArr[i]==currency){index = i; break;}
   }
   if(index==-1){
      printf("Invalid value %s passed in currency parameter.",currency);
      return false;
   }
   return (val&(1<<index))!=0;
}


Usage

ThisChartNewsOngoing(string currency);

Parameters

currency - Any 1 of the following strings:  "ANY","USD","EUR","GBP","JPY","AUD","CAD","CHF","NZD","CNY"

"ANY" - Checks if there is any news currently ongoing (from any currency)

"USD" - Checks if there is USD news currently ongoing

Return Value

Returns true if the inputted currency has a currently ongoing news

Returns false otherwise

Returns false if global variable is not found with warning Print() to terminal


Example

Let's say you have an EA called SuperEA running on EURCAD in which you want to directly integrate a news logic such that it doesn't open new positions 20 mins before and 30 mins after a high impact EUR or CAD news.

  • Paste the ThisChartNewsOngoing function definition at the bottom of SuperEA source code.
  • Add the logic where before it opens new positions, it checks ThisChartNewsOngoing.
if(!ThisChartNewsOngoing("ANY")){
  //code to open positions
} else{
  //code when there is news ongoing
}

  • Attach the modified SuperEA to EURCAD. 
  • Attach Filter This on the same EURCAD chart with the inputs below. Then click "Filter Selected" button on The News Filter GUI chart with "Create global variable" set to true.

Keep in mind that Order Management is DISABLED and EA is set to "Never remove". This is so that The News Filter will not do anything to SuperEA since all the actions can now be handled by the modified SuperEA itself. These Filter This inputs has selected these 4 news:

These 4 selected news are the only news used to calculate the global variable value corresponding to this EURCAD chart where SuperEA is attached. So, ThisChartNewsOngoing function inside SuperEA attached to EURCAD will also return true/false according to only these 4 selected news data.

You can press F3 on keyboard to view a list of all global variables. The global variable for every Filter This chart will always have the format #TNF:ChartID. Do not modify any of the global variables yourself as it can lead to unexpected outcomes.



Method 2: CSV File

If "Create CSV file" is enabled in The News Filter inputs, you can access a CSV file called NewsData from \MQL5\Files\TheNewsFilter in your MT4/5 installation path. This file contains ALL news data for the week from forex factory with the Forecast and Previous values. Below is a screenshot of the NewsData.csv opened in excel.

This data can be easily processed internally within your EA by using FileReadString() and StringSplit(). Method 2 is only preferable if you need more control over the data including Forecast and Previous value. Otherwise, method 1 using global variables is easier and preferred.



Notes:

DEMO version is for testing purposes only. CSV file saved under DEMO version is limited to only the first 20 news data. Global variables created under DEMO version is only available for 2 hours since The News Filter DEMO is attached. To restart the 2 hour DEMO time limit, remove The News Filter DEMO and reattach it to the chart.

There is no such limitation for the full version of The News Filter MT4/MT5. Feel free to leave a comment if you have any inquiries regarding this blog post.

Thank you!

Share it with friends: