GetNewsFF_v02





//=============================================================================
//                                                            GetNewsFF.mq4
//                                            Copyright © 2006, Derk Wehler
//                   Code derieved from original by: Copyright © 2006, Abhi
//
// This "indicator" calls DLLs to fetch the ForexFactory web page in order
// to retreive announcement information for the week.  It writes this web 
// page out to an .htm file, but more importantly, parses the page to extract 
// announcement information and writes this to a .csv file in the folder:
// experts/files.  It does once the first time it is run, and then again 
// once at the top of each hour (in case of updates).
//
// In order for this to work correctly and synchronize with your broker's 
// server time, you must enter the proper value for "TimeZone".  This is 
// entered relative to GMT.  For example, my broker's server time is GMT+1, 
// so I have the default set to 1.  If your broker uses GMT+3, enter 3.
// If your broker uses GMT-2, then enter -2.
//
//=============================================================================
#property copyright "Copyright © 2006, Derk Wehler"
#property link      ""

#property indicator_chart_window

#include <WinInet.mqh>

extern string	sUrl = "http://www.forexfactory.com/index.php?s=&page=calendar&timezoneoffset=g";
extern string 	Outputfile = 		"NewsItems.csv";
extern bool 	IncludeHigh = 		true;
extern bool 	IncludeMedium =		true;
extern bool 	IncludeLow = 		false;
extern bool 	IncludeSpeaks = 	true; 	// news items with "Speaks" in them have very different characteristics
extern bool 	RemoveDuplicates = 	true;	// only one news item per time
extern string 	ConvertUSDto = 		"USD"; 	// can change this to a full currency pair such as "GBPUSD"
extern string	TimeZone = 			"1";	// Set this to the GMT addition, so "1" for GMT+1, "-3" for GMT-3
extern int		DebugLevel =		0;

int handle;
int bytes;
int beginning, finalend,end,i;
string sData, csvoutput;
string lastNewsdate, lastNewstime, lastCountry;
string aju, text, newsdate, newstime, country, news, level, actual, forecast, previous;

static bool FirstRun = true;

int init()
{
	return(0);
}


int deinit()
{
	return(0);
}


int start()
{
	// All this code was originally in the init() function in Abhi's code.
	// I have moved it to the start() function so that it will not run 
	// until the EA is enabled
	if (DebugLevel > 0)
		Comment("Cur Min = ", Minute(), "\n");

	// Run on startup, and Re-run once at the top of each hour
	if (!FirstRun && Minute() != 0)
		return (0);
	FirstRun = false;

	string myUrl = sUrl + TimeZone;
	if (DebugLevel > 1)
		Print("myUrl == ", myUrl);

	
	// THIS CALL WAS DONATED BY PAUL TO HELP FIX THE RESOURCE ERROR
	GrabWeb(myUrl, sData);


	// Write the contents of the ForexFactory page to an .htm file
	handle = FileOpen(Day() + "-" + Month() + "-" + Year() + "-" + "grabweb.htm", FILE_BIN|FILE_READ|FILE_WRITE);
	if (handle < 1)
	{
		if (DebugLevel > 0)
			Print("Can\'t open htm file, the last error is ", GetLastError());
		return(false);
	}
	FileWriteString(handle, sData, StringLen(sData));
	FileClose(handle);



	// THIS BLOCK OF CODE DONATED BY WALLY TO FIX THE RESOURCE ERROR
	//--- Look for the end HTML tag to ensure that a complete page was downloaded ---//
	beginning = 1;
	beginning = StringFind(sData, "</html>", beginning);

	if (beginning <= 0)
	{
		Alert("GetNewsFF Error - Web page download was not complete!");
		return(false);
	}
	//-------------------------------------------------------------------------------//



	if (DebugLevel > 1)
		Print("bytes: "+bytes);

	// Now the .htm file is written; let's parse it now and write to csv file
	handle = FileOpen(Outputfile, FILE_CSV|FILE_WRITE, ",");
	if (handle < 1)
	{
		if (DebugLevel > 0)
			Print("Can\'t open csv file, the last error is ", GetLastError());
		return(false);
	}

	FileWrite(handle, "Description", "Currency", "DateTime", "TimeZone", "Level");

	beginning = 1;
	beginning = StringFind(sData, "<span class=\"smallfont\">Info.</span>", beginning) + 36;
	finalend = StringFind(sData, "</table>", beginning) - 1235;


	while (beginning < finalend)
	{
		for (i=0; i < 8; i++)
		{
			beginning = StringFind(sData, "<span class=\"smallfont\"", beginning)+23;
			beginning = StringFind(sData, ">", beginning)+1;
			end = StringFind(sData, "</span>", beginning)-0;
			if (end != beginning)
			{      
				text = StringSubstr(sData, beginning, end-beginning);
			}
			else
			{
				text = "";
			}

			if (i == 0)
			{
				if (text != "")
				{
					newsdate = text;
				}
				else
				{
					text = newsdate;
				}
			}
         
		// date and time conversion here if needed

			if (i == 1)
			{
				text = StringTrimLeft(text);
				newstime = StringTrimRight(text);
			} 

			if (i == 2)
			{
				country = text;
			}                   
			if (i == 3)
			{
				news = text;
				beginning = StringFind(sData, "src=\"http://www.forexfactory.com/forexforum/images/misc/", beginning) + 59;
				end = StringFind(sData, ".gif", beginning);
				text = StringSubstr(sData, beginning, end-beginning);
				level = text;
			}                   
			if (i == 4)
			{
				actual = text;
			}                   
			if (i == 5)
			{
				forecast = text;
			}                   
			if (i == 6)
			{
				previous = text;
			}                   

         }      
//		csvoutput = newsdate + ", " + newstime + ", " + country + ", " + news + ", " +level + ", " + actual + ", " + forecast + ", " + previous;

		if (!IncludeHigh && level == "high") 
			continue;
		if (!IncludeMedium && level == "med") 
			continue;
		if (!IncludeLow && level == "low") 
			continue;
		if (!IncludeSpeaks && (StringFind(news,"speaks") != -1 || StringFind(news,"Speaks") != -1) ) 
			continue;
		if (newstime == "tentative" || newstime == "Tentative") 
			continue;
		if (RemoveDuplicates && lastNewsdate == newsdate && lastNewstime == newstime && lastCountry == country) 
			continue;
			
		if (country == "USD") 
			country = ConvertUSDto;
			
		int nLevel = 0;
		if (level == "high") 
			nLevel = 3;
		if (level == "med") 
			nLevel = 2;
		if (level == "low") 
			nLevel = 1;

		lastNewsdate = newsdate;
		lastNewstime = newstime;
		lastCountry = country;
		
		string tz = "GMT";
		if (StrToInteger(TimeZone) > 0)
			tz = tz + "+";
		tz = tz + TimeZone;
		
		csvoutput = StringConcatenate(news, ",", country, ",", MakeDateTime(newsdate,newstime), ",", tz, ",", nLevel);
		if (DebugLevel > 1)
			Print("newslist: " + csvoutput);
		FileWrite(handle, news, country, MakeDateTime(newsdate,newstime), tz, nLevel);
	}
	FileClose(handle);

	return (0);
}

   
string MakeDateTime(string strDate, string strTime)
{
	// converts forexfactory time & date into yyyy.mm.dd hh:mm
	int nDateSpacePos = StringFind(strDate," ");
	int nDateSlashPos = StringFind(strDate,"/");

	string strMonth = StringSubstr(strDate,nDateSpacePos+1,nDateSlashPos-nDateSpacePos-1);
	string strDay = StringSubstr(strDate,nDateSlashPos+1);

	int nTimeColonPos	= StringFind(strTime,":");
	string strHour = StringSubstr(strTime,0,nTimeColonPos);
	string strMinute = StringSubstr(strTime,nTimeColonPos+1,2);
	string strAM_PM = StringSubstr(strTime,StringLen(strTime)-2);

	int nHour24 = StrToInteger(strHour);
	if (strAM_PM == "pm" || strAM_PM == "PM" && nHour24 != 12)
	{
		nHour24 += 12;
	}
	string strHourPad = "";
	if (nHour24 < 10) strHourPad = "0";

	return(StringConcatenate(Year(),".",strMonth,".",strDay," ",strHourPad,nHour24,":",strMinute));
}







Sample





Analysis



Market Information Used:



Indicator Curves created:


Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features:

Uses files from the file system
It writes information to file

It issuies visual alerts to the screen