MQL4 - automated forex trading   /  

Articles

Login | Register | Language: ru cn en
MetaTrader Market: we have 650 Expert Advisors for algorithmic trading!

Articles  Examples  Displaying a News Calendar To post a new article, please log in or register


Automated trading in
and Trade on the Go!
and Trade on the Go!

This article is about
MetaTrader 4
Download MT 4 - 455 Kb

and Trade on the Go!

and Trade on the Go!

Displaying a News Calendar [ ru ]

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: http://articles.mql4.com/ru/520

Attachments:
CalendarArticle.mq4 (4.9 Kb)
getright_setup.zip (4.7 Mb)
Time.mq4 (2.0 Kb)
Time.mqh (750 bytes)
Created: 2007.11.23  Author: Slobodov Gleb
Warning: All rights to these materials are reserved by MetaQuotes Software Corp. Copying or reprinting of these materials in whole or in part is prohibited.
MT4TerminalSync - System for the Synchronization of MetaTrader 4 Terminals
MT4TerminalSync - System for the Synchronization of MetaTrader 4 Terminals

This article is devoted to the topic "Widening possibilities of MQL4 programs by using functions of operating systems and other means of program development". The article describes an example of a program system that implements the task of the synchronization of several terminal copies based on a single source template.

Equivolume Charting Revisited
Equivolume Charting Revisited

The article dwells on the method of constructing charts, at which each bar consists of the equal number of ticks.

29 comments: 1 2 3   To add comments, please, log in or register
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
2014.04.30 21:28 swarajthakur
Man I really wish this was still working :(
2013.10.10 12:43 DomGilberto
-... 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....
2011.04.21 18:39 luckyluke72
Placed files in correct places and got "Time.mqh" - cannot open the program file
2011.04.21 18:17 luckyluke72

how can i set fix GMT time. I mean indicator take time from my local computer but my computer time and sever time is different. so how can i fix GMT time. can any one help me out...

2011.04.04 07:03 dralialadin
hi all, now the csv format on dailyfx has changed, it looks to be http://www.dailyfx.com/files/Calendar-01-02-2011.csv so it there anyone here who has calender plotter for those function to be shared here? thanks.
2011.01.05 10:04 jhp2025
erekit:
Roddo wrote:
Great trading tool 10 out of 10....

I understand this thread is dead now but fingers crossed there still might be someone that maybe able to help...
I would like this indicator to use a separate window, can this be done and also could it be possible to display a box
with the information of the news when you pass the mouse pointer over the news line as some of the description is doubled up..


Thanks
Rod.

I also hope somebody can help on this matter. My mql4 is not that strong at this moment to enhance this part.

Thanks.

From
erekit
http://www.mql4.com/go?http://100k2.blogspot.com/


Hi

I have been using this EA for while and it does what I needed it to do ... But DailyFX has changed the calendar's file name to a date format

They now use http://www.dailyfx.com/files/Calendar-11-21-2010.csv the date changes every week. Can anybody do a work around for this. The day is always the first day of the week ..

Thanks

Rod

2010.11.20 15:39 Roddo
Roddo wrote:
Great trading tool 10 out of 10....

I understand this thread is dead now but fingers crossed there still might be someone that maybe able to help...
I would like this indicator to use a separate window, can this be done and also could it be possible to display a box
with the information of the news when you pass the mouse pointer over the news line as some of the description is doubled up..


Thanks
Rod.

I also hope somebody can help on this matter. My mql4 is not that strong at this moment to enhance this part.

Thanks.

From
erekit
http://100k2.blogspot.com
2010.05.08 08:54 erekit
I have done the modification on the code to allow selection on currency. At the user input, choose the currency news you would like to display on chart

Thanks the idea from 2010.03.12 18:39 korhan.x

You can download the file and use it. I didn't add the importance level selection as I feel it is not important to do so.

Next -> work to change the download tool from GetRight (free trial but need to buy ) to other free download tool


Thanks

From
erekit
http://100k2.blogspot.com
Attached files:
  CalendarArticle.mq4 (5.49 KB)
2010.05.08 08:03 erekit
29 comments: 1 2 3