Download MetaTrader 5

Displaying a News Calendar

23 November 2007, 16:08
Slobodov Gleb
29
7 670

Introduction


This article contains the description of writing a simple and convenient indicator displaying in a working area the main economic events from external Internet resources. The indicator's operation looks like this:



Here is a list of requirements for the indicator:
  • The indicator should independently (without a user's help) download a necessary file of the events calendar for the current week.
  • The indicator should display all events (both passed and future) from this file in the form of vertical lines with news headlines.
  • The indicator should trace the events' update on the external resource.
After we have specified the task, we can analyze some technical details.


Technical Part


Let us use the website http://www.dailyfx.com/calendar/ as an external resource. The convenience of this resource is that it enables to download a calendar with .csv extension, so we avoid difficulties of working with html files. Here is a link of news for the current week: http://www.dailyfx.com/calendar/Dailyfx_Global_Economic_Calendar.csv.

Now let us dwell on the process of downloading the file from the Internet. This can be done using a well-known program GetRight. It can be downloaded at: http://www.getright.com/get.html or from the list of attachments to this article.

After you have downloaded the program, set up GetRight for downloading files into a necessary directory. It is the \files\html\ directory in the folder of your trading terminal. To do this, press F8 and change the writing in the field as described below:




Writing the Indicator


Now having answered some questions, we can start writing the indicator.


extern string HtmlAdress = "http://www.dailyfx.com/calendar/Dailyfx_Global_Economic_Calendar.csv";
extern string GetrightAdress = "c:\progra~1\getright\getright.exe";
 
#include <Time.mqh>
#import "kernel32.dll"
int WinExec(string NameEx, int dwFlags);

There are only two external variables: the address of the external resource (actually, you do not have to change this parameter) and the address of the GetRight program (if you have downloaded the program into another directory, I recommend to change the initial value in the indicator, in order not to change the value of the variable constantly at the indicator start). To start the file GetRight.exe we will have to use the function WinExec that can be imported from the Kernel32.dll library. The library Time.mqh contains functions for working with GMT.


void DownloadCalendar()
{
 Print("Downloading "+HtmlAdress+" to experts\files\html\Calendar.csv");
 WinExec(GetrightAdress+" /URL:"+HtmlAdress+" /FILE:Calendar.csv /W /O",0);
}

As you can see, due to the usage of the GetRight program the function of downloading the file from the external resource looks like very simple.The parameter /W denotes that the process will not be returned into the program unless the file downloading is completed. The parameter /O denotes that if there is a file with the same name, it will be overwritten. Remember, if you have changed the settings of GetRight correctly, the calendar will be downloaded into \files\html\. And here are two additional functions:


datetime PerviousMonday(datetime d)
{
 datetime res = d - (TimeDayOfWeek(d)-1)*24*60*60;
 return(res);
}
datetime ToDate(string stDate,string stTime)  
{
 string WeekDay = StringSubstr(stDate,0,3);
 int WeekPlus = 0;
 if (WeekDay=="Mon") WeekPlus=0;
 if (WeekDay=="Tue") WeekPlus=1;
 if (WeekDay=="Wed") WeekPlus=2;
 if (WeekDay=="Thu") WeekPlus=3;
 if (WeekDay=="Fri") WeekPlus=4;
 if (WeekDay=="Sat") WeekPlus=5;
 if (WeekDay=="Sun") WeekPlus=-1;
 datetime Res = PerviousMonday(GetTimeGMT())+WeekPlus*24*60*60;
 datetime Tm = StrToTime(stTime);
 Res=Res+TimeHour(Tm )*60*60+TimeMinute(Tm )*60+TimeSeconds(Tm )
 -TimeHour(Res)*60*60-TimeMinute(Res)*60-TimeSeconds(Res);  
 if (StringFind(stTime,"PM")>=0)  
 Res+=12*60*60;
 Res=Res-GetShiftGMT();
 return (Res);
}

The function PerviousMonday() returns the starting date of the current week. The function ToDate() transfers the date and the time from the calendar format into datatime.


void GrabNews()  
{
 int file = FileOpen("\Html\Calendar.csv",FILE_READ|FILE_CSV,',');
 if (file==-1||FileSize(file)==0)
 return;
 int i=0;
 while (!FileIsEnding(file))
 {
 string stDate="";
 while (!FileIsEnding(file)&&stDate=="")
 stDate = FileReadString(file);
 string stTime = FileReadString(file);
 string stTimeZone = FileReadString(file);
 string stCurrency = FileReadString(file);
 string stDescription = FileReadString(file);
 string stImportance = FileReadString(file);
 string stActual = FileReadString(file);
 string stForecast = FileReadString(file);
 string stPrevious = FileReadString(file);
 datetime Date = ToDate(stDate,stTime);
 color c=Green;
 if (stImportance=="Low") c = Yellow;
 if (stImportance=="Medium") c = Orange;
 if (stImportance=="High") c = Red;
 ObjectCreate("CalendarText"+i, OBJ_TEXT, 0, Date, Close[0]);
 ObjectSet("CalendarText"+i, OBJPROP_COLOR, c);  
 ObjectSetText("CalendarText"+i, stDate + " : "+ stDescription, 8);  
 ObjectSet("CalendarText"+i, OBJPROP_ANGLE, 90);  
 ObjectCreate("CalendarLine"+i, OBJ_VLINE, 0, Date, Close[0]);
 ObjectSet("CalendarLine"+i, OBJPROP_COLOR, c);  
 ObjectSet("CalendarLine"+i, OBJPROP_STYLE, STYLE_DOT);  
 ObjectSet("CalendarLine"+i, OBJPROP_BACK, true);  
 ObjectSetText("CalendarLine"+i, stDescription, 8);  
 i++;
 }
 Max = i;
 if (file!=-1)
 FileClose(file);
}

The main procedure GrabNews() opens the downloaded file \Html\Calendar. csv, reads all event parameters and creates two objects for each news: a vertical line and a text. The event calendar is updated every 15 minute:

int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   if (TimeCurrent()>LastTimeDownloading+15*60)
     {
       DeleteObjects();
       DownloadCalendar();
       LastTimeDownloading = TimeCurrent();
       
       int file=-1;
       while (file==-1)
         file = FileOpen("\Html\Calendar.csv",FILE_READ|FILE_CSV,',');
       FileClose(file);
 
       GrabNews();
     }
//----
   return(0);
  }

Conclusion


The article explained how to display an event calendar from an external resource onto a working area in the form of vertical lines. The indicator was intentionally written without any excessive parameters like filtering news according to their relevance or the correspondence of an event and the symbol of the current window.

P.S. I would like to point at an error in the calendar operation http://www.dailyfx.com/calendar/. Please note that sometimes events in the file .csv from the address http://www.dailyfx.com/calendar/Dailyfx_Global_Economic_Calendar.csv are not identical with the page http://www.dailyfx.com/calendar/. This may happen to news released from 00:00 till 01:00 (GMT). In the file .csv such news are indicated 12 hours later.

Also please note, that the indicator uses external dll (kernell32.dll), so do not forget to enable the corresponding parameter in the indicator settings.

The file CalendarArticle.mq4 should be stored in the folder \experts\indicators. Time.mq4 should be stored in expers\library, Time.mqh - in experts\include.

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1502

Attached files |
getright_setup.zip (4763.33 KB)
Time.mq4 (2.03 KB)
Time.mqh (0.73 KB)
Last comments | Go to discussion (29)
MQL4 Comments
MQL4 Comments | 21 Apr 2011 at 16:17
Placed files in correct places and got "Time.mqh" - cannot open the program file
MQL4 Comments
MQL4 Comments | 21 Apr 2011 at 16:39
-... it is really retarded that one needs a separate program to first download web data in order for indicator to read it.. how much more cumbersome a platform can get...!!?? MT4/5 are designed for brokers and paid by brokers, it's not build for programmers or traders and this becomes painfully obvious when one get's to know MT4/5 more closely....
Dominic Gilbert
Dominic Gilbert | 10 Oct 2013 at 10:43
Man I really wish this was still working :(
Swaraj Thakur
Swaraj Thakur | 30 Apr 2014 at 19:28
I think the thread is dead, but I believe someone implemented it in an EA.. I am foward testing it at the moment.. Will update once I get any significant results.. http://forexnewsexpert.com
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.

How to create an indicator of non-standard charts for MetaTrader Market How to create an indicator of non-standard charts for MetaTrader Market

Through offline charts, programming in MQL4, and reasonable willingness, you can get a variety of chart types: "Point & Figure", "Renko", "Kagi", "Range bars", equivolume charts, etc. In this article, we will show how this can be achieved without using DLL, and therefore such "two-for-one" indicators can be published and purchased from the Market.