Displaying a News Calendar at Chart MT4

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:



Please for detail article to :


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/files/Calendar-11-21-2010.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: http://articles.mql4.com/ru/520
Attachments:
 CalendarArticle.mq4 (4.9 Kb)  http://articles.mql4.com/download/3691
 getright_setup.zip (4.7 Mb)     http://articles.mql4.com/download/3692
 Time.mq4 (2.0 Kb)                 http://articles.mql4.com/download/3693
 Time.mqh (750 bytes)                 http://articles.mql4.com/download/3694