Author: Copyright � 2006, MetaQuotes Software Corp.
grabweb2

This script is designed to automate the process of retrieving live economic data from the internet and saving it directly onto your computer's storage. By connecting to a specific web address (in this case, a financial calendar), the tool acts as a bridge between online information and your local trading environment.

How the Logic Works

1. Establishing a Connection The script begins by initializing a secure communication channel with the internet. It mimics a standard web browser (like Internet Explorer) to ensure that the website being accessed recognizes the request as a legitimate user visit. It identifies whether to connect using a direct path or via a pre-configured setting to ensure the connection is stable.

2. Fetching the Data Once the connection is active, the script navigates to a specific URL provided in its settings. It "grabs" the raw data from that page. Because web pages can be large, the logic includes a loop system: it reads the information in small chunks and continuously gathers them until the entire page content has been successfully downloaded into its memory.

3. Processing and Saving After the script has fully captured the online data, it performs two final actions:

  • Data Storage: It creates a local file on your computer. The filename is automatically organized using the current date (Day, Month, and Year) to keep your records chronological.
  • Writing to File: It takes all the gathered web information and writes it into that newly created file.

4. Cleanup Finally, the script properly closes the internet connection handle to free up system resources, ensuring that the trading platform remains performant and doesn't leave "hanging" connections.

Summary for the User

Essentially, this tool serves as an automated web scraper. Every time it runs, it reaches out to a financial news source, downloads the latest updates, and saves them into a dated HTML file on your local machine, allowing you to keep a history of economic events without manual downloading.

Miscellaneous
Uses files from the file systemIt writes information to file
15 Views
0 Downloads
0 Favorites
grabweb2
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

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

extern string sUrl = "http://www.forexfactory.com/index.php?page=calendar";

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;
int handle;

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]);
Print("sBuffer: " +  sBuffer);
Comment("sBuffer: " +  sBuffer);

        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;
            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 file, the last error is ", GetLastError());
     return(false);
    }
   FileWriteString(handle,sData,StringLen(sData));
   FileClose(handle);


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

Comments