MQL4 - automated forex trading   /  

Articles

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


This article is about
MetaTrader 4
Download MT 4 - 5.5 Mb

Mobile trading!
Buy a license and be mobile in your trading!

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.

15 comments: 1 2   To add comments, please, log in or register

You can also use wget.exe to get the file. There is a windows version of it.


http://users.ugent.be/~bpuype/wget/

2009.12.29 18:03 xanatose

this did not work for me. I can't figure out what I did wrong or if I missed a step. also there was no folder named html under files so I made one

to see if it would work but to know avail that isn't the problem.


2009.12.27 17:36 england33
c0d3 wrote:
This is a great indicator, i modified it to become an EA, and now, it waits for the news to come out then it trades it. When the actual news data is released, the EA compares the actual data either to the forecast data or the previous data, calculates percent change, then enters the trade. Also, the EA trades every country that is supported on Dailyfx.com. Thx, for creating this awesome indicator and this awesome article.
hi c0d3, I'm new to programming MT4 and was wondering if you'd like to share your modified EA?
2009.12.16 18:08 Dertweiller

If you don't want to install GetRight for downloading .csv files then you can use url2disk.exe .

This is a free tool, a standalone .exe, it doesn't needs to be installed, just put in a directory of your wish and call it from mql.

You can download it from here: http://www.blocsoft.com/fwMore.asp?Title=url2disk

Or you can use this Visual Basic Script (but I don't know if winexec can run it or not) : http://www.robvanderwoude.com/vbstech_internet_download.php

Of course the script must be modified (url and destination) in order to be used here.

2009.09.09 22:05 D0n

Does anyone else get this "error"

Fundamental_Trader_DailyFX_MQL4_v0.04: handle 2 does not exist in FileClose .... and how to fix ??

Thanks

2009.08.21 04:14 chingching
I mean the Auto optimization. Where will it be? I love to use the EA and see positive result.
2009.02.20 19:49 awise303

Hello,

Thank you for the good article.

I just want to make a note that kernel32.dll is spelled incorrectly right at the end of the article. The spelling in the article is kernell32.dll. The dll is spelled correctly in the files for downloading.

2008.12.01 02:27 chingching
how if we don't use getright? can we?
2008.11.25 08:17 jhp2025

//+------------------------------------------------------------------+
//| dll.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

#property indicator_chart_window

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);

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

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();
if((StringFind(stTime,"12"))>=0 && (StringFind(stTime,"PM"))>=0){Res=Res-(TimeHour(Tm )*60*60)-(TimeMinute(Tm )*60);}

return (Res);
}

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);
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+

i have a error here pls help ...

'Max' - variable not defined D:\alpari-idc\experts\indicators\dll.mq4 (88, 2)
'LastTimeDownloading' - variable not defined D:\alpari-idc\experts\indicators\dll.mq4 (119, 22)
'LastTimeDownloading' - variable not defined D:\alpari-idc\experts\indicators\dll.mq4 (123, 8)

or can u just add the indicator for me ?? plss cause i m new in mql 4 or can you add a template

2008.10.21 20:28 wowow10

I found an error in the algorithm that determined an integer in seconds since "Jan 1, 1970". When there is a "PM" in news-time(stTime), the function assumes that the time starts from zero (0). So in this fashion, when the news-time is "12:30 PM", it is actually "0:30 PM". So when the algorithm gets to "12:30 PM", it thinks that the news-time(stTime) is 12 hours ahead, which gives errors in displaying events, and trading decisions. In my EA, this issue caused the EA to wait for 12hours before going to the next event, therefore, i wrote code to fix this, if you are interested, the modification is below.

Replace the following:

if (StringFind(stTime,"PM")>=0)
Res+=12*60*60;
Res=Res-GetShiftGMT();

With the following:

if (StringFind(stTime,"PM")>=0)
Res+=12*60*60;
Res=Res-GetShiftGMT();
if((StringFind(stTime,"12"))>=0 && (StringFind(stTime,"PM"))>=0){Res=Res-(TimeHour(Tm )*60*60)-(TimeMinute(Tm )*60);}

2008.10.02 02:44 c0d3
15 comments: 1 2