Forexnews.com

Download script
Author: Copyright � 2006, Abhi

This script is designed to automatically fetch, process, and organize global economic news data into a structured format that can be easily read by other software or spreadsheets. It acts as a bridge between an online web calendar and your local trading environment.

Core Functionality

1. Data Retrieval (Web Scraping) The tool begins by connecting to a specific external URL containing a global economic calendar. It simulates a web browser connection to "read" the raw HTML code of that webpage. Once connected, it captures the entire content of the page and saves it into a temporary local file.

2. Data Extraction (Parsing) After saving the raw data, the script searches through the complex code of the webpage to find specific news entries. It looks for key markers in the website's layout—specifically focusing on sections containing actual economic figures, dates, and country names. It systematically pulls out:

  • The specific economic news description.
  • The associated country or currency.
  • The date and time of the event.

3. Data Translation and Normalization One of the script's primary roles is to "translate" geographic locations into standardized currency pairs. For example, if the script identifies a news event from "Canada," it can automatically convert that entry to a specific currency pair (like GBP) based on settings defined in the script. This ensures consistency for traders who prefer to view news through the lens of a specific base currency.

4. File Generation (CSV Export) Finally, the script organizes all the cleaned and translated information into a structured CSV (Comma Separated Values) file. It creates a header row including categories like Description, Currency, DateTime, TimeZone, and other trading metrics. This allows the final data to be saved in a clean, organized table format, making it easy for a user to view or for other automated systems to import.

Summary of Workflow

  • Fetch: Connects to the web to grab raw news code.
  • Save: Stores that raw code into a temporary file on your computer.
  • Parse: Scans the code to find specific news items and dates.
  • Convert: Swaps country names for preferred currency pairs.
  • Export: Writes the final, clean list into a CSV file for easy use.
Miscellaneous
Uses files from the file systemIt writes information to fileIt writes information to file
5 Views
0 Downloads
0 Favorites
Forexnews.com
//+------------------------------------------------------------------+
//|                                                Forexnews.com.mq4 |
//|                                           Copyright © 2006, Abhi |
//|                                http://www.megadelfi.com/experts/ |
//|                            E-mail: grabwebexpert{Q)megadelfi.com |
//|                       fix my e-mail address before mailing me ;) |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Abhi"
#property link      "http://www.megadelfi.com/experts/"

extern string sUrl = "http://www.forexnews.com/globalcalendar/default.asp";
extern string Outputfile = "Calendar.csv";
extern string Timezone = "EST";
extern string ConvertCanadato = "GBP"; // can change this to a full currency pair such as "GBPUSD"
extern string ConvertAustraliato = "GBP";
extern string ConvertE12to = "GBP";
extern string ConvertUKto = "GBP";
extern string ConvertJapanto = "GBP";
extern string ConvertUSto = "GBP";
extern string ConvertEURto = "GBP";
extern string ConvertNZDto = "GBP";

int hSession_IEType;
int hSession_Direct;
int Internet_Open_Type_Preconfig = 0;
int Internet_Open_Type_Direct = 1;
int Internet_Open_Type_Proxy = 3;
int Buffer_LEN = 13;


#import "wininet.dll"
  int InternetOpenA(
    string sAgent,
    int    lAccessType,
    string sProxyName="",
    string sProxyBypass="",
    int lFlags=0
  );

  int InternetOpenUrlA(
    int    hInternetSession,
    string sUrl, 
    string sHeaders="",
    int lHeadersLength=0,
    int lFlags=0,
    int lContext=0 
  );

  int InternetReadFile(
    int hFile,
    string sBuffer,
    int lNumBytesToRead,
    int& lNumberOfBytesRead[]
  );

  int InternetCloseHandle(
    int hInet
  );


int hSession(bool Direct)
   {
    string InternetAgent;
    if (hSession_IEType == 0)
      {
        InternetAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)";
        hSession_IEType = InternetOpenA(InternetAgent, Internet_Open_Type_Preconfig, "0", "0", 0);
        hSession_Direct = InternetOpenA(InternetAgent, Internet_Open_Type_Direct, "0", "0", 0);
      }
    if (Direct) 
    { return(hSession_Direct); }
    else 
    { return(hSession_IEType); }
   }
   
int start()
  {
//   Print("fake start");
  }

int init()
  {
int hInternet;
string iResult;
int lReturn[]={1};
string sBuffer="x";
string sData, csvoutput;
int handle;
int bytes;
int beginning, finalend,end,i;
string aju, text, newsdatetime, country, news;

hInternet = InternetOpenUrlA(hSession(FALSE), sUrl, "0", 0, 67108864, 0);
Print("hInternet: " + hInternet);   
iResult = InternetReadFile(hInternet, sBuffer, Buffer_LEN, lReturn);
Print("iResult: " + iResult);
Print("lReturn: " + lReturn[0]);
//iResult = InternetCloseHandle(hInternet);
Print("iResult: " + iResult);
Print("sBuffer: " +  sBuffer);
Comment("sBuffer: " +  sBuffer);
bytes = lReturn[0];

        sData = StringSubstr(sBuffer, 0, lReturn[0]);
        //if there's more data then keep reading it into the buffer
      while (lReturn[0] != 0)
       {
            iResult = InternetReadFile(hInternet, sBuffer, Buffer_LEN, lReturn);
            if (lReturn[0]==0) break;
            bytes = bytes + lReturn[0];
            sData = sData + StringSubstr(sBuffer, 0, lReturn[0]);
       }

Print("sData: " + sData);
iResult = InternetCloseHandle(hInternet);
Print("iResult: " + iResult);

handle=FileOpen(Day()+ "-"+ Month() +"-"+ Year() +"-"+ "grabweb.htm",FILE_BIN|FILE_READ|FILE_WRITE);
if(handle<1)
    {
     Print("Can\'t open htm file, the last error is ", GetLastError());
     return(false);
    }
   FileWriteString(handle,sData,StringLen(sData));
   FileClose(handle);

   Print("bytes: "+bytes);

// htm file is written, let's parse it now and write to csv file

handle=FileOpen(Outputfile,FILE_CSV|FILE_WRITE,",");
if(handle<1)
    {
     Print("Can\'t open csv file, the last error is ", GetLastError());
     return(false);
    }
    
   FileWrite(handle,"Description","Currency","DateTime","TimeZone","Lots","PercentOfMargin","TrailingStop","SecondsBeforeEvent","StraddleDuration");

beginning = 1;
beginning = StringFind(sData,"<div class=tti>Actual</div></td>",beginning)+32;
finalend = StringFind(sData,"</table>",beginning)-835;

while (beginning < finalend)
   {
      for (i=0; i <=0; i++)
         {
            beginning = StringFind(sData,"<td valign=top align=\"right\"",beginning)+28;
            beginning = StringFind(sData,"nowrap>",beginning)+7;
            end = StringFind(sData,"</td>",beginning)-0;
               if (end != beginning)
                  {      
                     text = StringSubstr(sData, beginning, end-beginning);
                  }
               else
                  {
                     text = "";
                  }

                  if (i == 0)
                  {
                     if (text != "")
                     {
                        newsdatetime = text;
                     }
                     else
                     {
                        text = newsdatetime;
                     }
                  }
          }
       

       for (i=1; i <=1; i++)
          {
             beginning = StringFind(sData,"<td valign=top align=\"middle\"",beginning)+29;
             beginning = StringFind(sData,">",beginning)+1;
             end = StringFind(sData,"</td>",beginning)-0;
               if (end != beginning)
                  {      
                     text = StringSubstr(sData, beginning, end-beginning);
                  }
               else
                  {
                     text = "";
                  }

                  if (i == 1)
                  {
                     if (text != "")
                     {
                        country = text;
                     }
                     else
                     {
                        text = country;
                     }
                  }
           }    
                  
                          
       for (i=2; i <=2; i++)
          {
             beginning = StringFind(sData,"<td valign=top align=\"left\"",beginning)+27;
             beginning = StringFind(sData,">",beginning)+1;
             end = StringFind(sData,"</td>",beginning)-0;
               if (end != beginning)
                  {      
                     text = StringSubstr(sData, beginning, end-beginning);
                  }
               else
                  {
                     text = "";
                  }

                  if (i == 2)
                  {
                     if (text != "")
                     {
                        news = text;
                     }
                     else
                     {
                        text = news;
                     }
                  }                             

            } 
              
         if (country == "Canada") country = ConvertCanadato;
			if (country == "Australia") country = ConvertAustraliato;
			if (country == "E-12") country = ConvertE12to;
			if (country == "UK") country = ConvertUKto;
			if (country == "US") country = ConvertUSto;
			if (country == "Japan") country = ConvertJapanto;
			if (country == "EUR") country = ConvertEURto;
			if (country == "NZD") country = ConvertNZDto;
			
         csvoutput = StringConcatenate(news,",",country,",",newsdatetime,",",Timezone);
         Print("newslist: "+ csvoutput);
         FileWrite(handle,news,country,newsdatetime,Timezone);
    }
   FileClose(handle);

return(0);
  }
   //+------------------------------------------------------------------+
--------------020207080700060909010908--

Comments