", "", "", "", "", "", "", "", "", "" };
int loopcount = 0;
while (true)
{
// loopcount++;
// if (loopcount > 500)
// break;
BoEvent = StringFind(sData, "", BoEvent);
if (BoEvent == -1)
break;
BoEvent += 7;
next = StringFind(sData, "", BoEvent);
myEvent = StringSubstr(sData, BoEvent, next - BoEvent);
BoEvent = next;
begin = 0;
skip = false;
for (i=0; i < 7; i++)
{
mainData[i] = "";
next = StringFind(myEvent, sTags[i], begin);
// Within this event, if tag not found, then it must be missing; skip it
if (next == -1)
continue;
else
{
// We must have found the sTag okay...
begin = next + StringLen(sTags[i]); // Advance past the start tag
end = StringFind(myEvent, eTags[i], begin); // Find start of end tag
if (end > begin && end != -1)
{
// Get data between start and end tag
mainData[i] = StringSubstr(myEvent, begin, end - begin);
}
}
}
// for (i=6; i >= 0; i--)
// Print(sTags[i], " = ", mainData[i]);
if (cntry1 != mainData[1] && cntry2 != mainData[1])
skip = true;
if (!IncludeHigh && mainData[4] == "High")
skip = true;
if (!IncludeMedium && mainData[4] == "Medium")
skip = true;
if (!IncludeLow && mainData[4] == "Low")
skip = true;
if (!IncludeSpeaks && (StringFind(mainData[0], "speaks") != -1 || StringFind(mainData[0], "Speaks") != -1) )
skip = true;
if (mainData[3] == "tentative" || mainData[3] == "Tentative")
skip = true;
// If not skipping this event, then log it into the draw buffers
if (!skip)
{
// If we got this far then we need to calc the minutes until this event
//
// First, convert the announcement time to seconds, GMT
newsTime = StrToTime(MakeDateTime(mainData[2], mainData[3]));
// Now convert to brokerage time
newsTime += (TimeZone * 60 * 60);
// Now calculate the minutes until this announcement (may be negative)
minsTillNews = (newsTime - TimeCurrent()) / 60;
// Keep track of the most recent news announcement.
// Do that by saving each one until we get to the
// first annoucement that isn't int he past; i.e.
// minsTillNews > 0. Then, keep this one instead for
// display, but only once the minutes until the next
// news is SMALLER than the minutes since the last.
// Print("Mins till event: ", minsTillNews);
if (minsTillNews < 0 || MathAbs(dispMinutes) > minsTillNews)
{
dispTitle = mainData[0];
dispCountry = mainData[1];
dispImpact = mainData[4];
dispForecast = mainData[5];
dispPrevious = mainData[6];
dispMinutes = minsTillNews;
}
Log("Weekly calendar for " + pair + "\n\n");
if (DebugLevel > 0)
{
Log("FOREX FACTORY\nTitle: " + mainData[0] +
"\nCountry: " + mainData[1] +
"\nDate: " + mainData[2] +
"\nTime: " + mainData[3] +
"\nImpact: " + mainData[4] +
"\nForecast: " + mainData[5] +
"\nPrevious: " + mainData[6] + "\n\n");
}
// Do alert if user has enabled
if (Alert1MinsBefore != -1 && minsTillNews == Alert1MinsBefore)
Alert(Alert1MinsBefore, " minutes until news for ", pair, ": ", mainData[0]);
if (Alert2MinsBefore != -1 && minsTillNews == Alert2MinsBefore)
Alert(Alert2MinsBefore, " minutes until news for ", pair, ": ", mainData[0]);
// Buffers are set up as so:
// ExtMapBuffer0 contains the time UNTIL each announcement (can be negative)
// e.g. [0] = -372; [1] = 25; [2] = 450; [3] = 1768 (etc.)
// ExtMapBuffer1[0] has the mintutes since the last annoucement.
// ExtMapBuffer1[1] has the mintutes until the next annoucement.
ExtMapBuffer0[newsIdx] = minsTillNews;
newsIdx++;
}
}
// Cycle through the events array and pick out the most recent
// and the next coming event to put into ExtMapBuffer1
int nextMins;
int lastMins = 0;
bool first = true;
ExtMapBuffer1[0] = 0;
ExtMapBuffer1[1] = 0;
string outNews = "Minutes until news events for " + pair + " : ";
for (i=0; i < newsIdx; i++)
{
nextMins = ExtMapBuffer0[i];
outNews = outNews + nextMins + ", ";
if (nextMins > 0 && first)
{
first = false;
ExtMapBuffer1[0] = lastMins;
ExtMapBuffer1[1] = nextMins;
}
lastMins = nextMins;
}
// If we are past all news events, then neither one will have been
// set, so set the past event to the last (negative) minutes
if (ExtMapBuffer1[0] == 0 && ExtMapBuffer1[1] == 0)
ExtMapBuffer1[0] = lastMins;
// For debugging...Print the tines until news events, as a "Comment"
if (DebugLevel > 0)
{
Print(outNews);
Print("LastMins (ExtMapBuffer1[0]) = ", ExtMapBuffer1[0]);
Print("NextMins (ExtMapBuffer1[1]) = ", ExtMapBuffer1[1]);
}
// Now handle all other indicator functions
commentStr = commentStr + dispMinutes + " Mins until " + dispCountry + ": " + dispTitle;
commentStr = commentStr + "\nImpact: " + dispImpact;
commentStr = commentStr + "\nPrevious: " + dispPrevious;
commentStr = commentStr + "\nForecast: " + dispForecast;
Comment(commentStr);
if (logHandle != -1)
FileClose(logHandle);
return (0);
}
string MakeDateTime(string strDate, string strTime)
{
// converts forexfactory time & date into yyyy.mm.dd hh:mm
int n1stDash = StringFind(strDate, "-");
int n2ndDash = StringFind(strDate, "-", n1stDash+1);
string strMonth = StringSubstr(strDate, 0, 2);
string strDay = StringSubstr(strDate, 3, 2);
string strYear = StringSubstr(strDate, 6, 2);
strYear = "20" + strYear;
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(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute));
}
//=================================================================================================
//=================================================================================================
//==================================== GrabWeb Functions ======================================
//=================================================================================================
//=================================================================================================
// Main Webscraping function
// ~~~~~~~~~~~~~~~~~~~~~~~~~
// bool GrabWeb(string strUrl, string& strWebPage)
// returns the text of any webpage. Returns false on timeout or other error
//
// Parsing functions
// ~~~~~~~~~~~~~~~~~
// string GetData(string strWebPage, int nStart, string strLeftTag, string strRightTag, int& nPos)
// obtains the text between two tags found after nStart, and sets nPos to the end of the second tag
//
// void Goto(string strWebPage, int nStart, string strTag, int& nPos)
// Sets nPos to the end of the first tag found after nStart
bool bWinInetDebug = false;
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"
#define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100 // Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy.
#define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000 // Does not add the returned entity to the cache.
#define INTERNET_FLAG_RELOAD 0x80000000 // Forces a download of the requested file, object, or directory listing from the origin server, not from the cache.
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
);
#import
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);
}
}
bool GrabWeb(string strUrl, string& strWebPage)
{
int hInternet;
int iResult;
int lReturn[]={1};
string sBuffer="x";
int bytes;
hInternet = InternetOpenUrlA(hSession(FALSE), strUrl, "0", 0,
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_RELOAD, 0);
if (bWinInetDebug)
Log("hInternet: " + hInternet);
if (hInternet == 0)
return(false);
iResult = InternetReadFile(hInternet, sBuffer, Buffer_LEN, lReturn);
if (bWinInetDebug)
Log("iResult: " + iResult);
if (bWinInetDebug)
Log("lReturn: " + lReturn[0]);
if (bWinInetDebug)
Log("iResult: " + iResult);
if (bWinInetDebug)
Log("sBuffer: " + sBuffer);
if (iResult == 0)
return(false);
bytes = lReturn[0];
strWebPage = 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];
strWebPage = strWebPage + StringSubstr(sBuffer, 0, lReturn[0]);
}
iResult = InternetCloseHandle(hInternet);
if (iResult == 0)
return(false);
if (bWinInetDebug)
{
Log("iResult: " + iResult);
int handle = FileOpen("Grabweb.htm",FILE_BIN|FILE_READ|FILE_WRITE);
FileWriteString(handle,strWebPage,StringLen(strWebPage));
FileClose(handle);
}
return(true);
}
/*
string GetData(string strWebPage, int nStart, string strLeftTag, string strRightTag, int& nPos)
{
nPos = StringFind(strWebPage,strLeftTag,nStart);
if (nPos == -1)
{
if (bWinInetDebug) Log("GetData(WebPage,"+nStart+","+strLeftTag+","+strRightTag+"): not found, returning empty string and -1");
return("");
}
nPos += StringLen(strLeftTag);
int nEnd = StringFind(strWebPage,strRightTag,nPos+1);
string strData = StringTrimLeft(StringTrimRight(StringSubstr(strWebPage,nPos,nEnd-nPos)));
if (bWinInetDebug) Log("GetData(WebPage,"+nStart+","+strLeftTag+","+strRightTag+") returning "+strData+" and "+nPos);
return(strData);
}
void Goto(string strWebPage, int nStart, string strTag, int& nPos)
{
nPos = StringFind(strWebPage,strTag,nStart);
if (nPos == -1)
{
if (bWinInetDebug) Log("Goto("+nStart+","+strTag+") not found. Returning -1");
return;
}
nPos += StringLen(strTag);
if (bWinInetDebug) Log("Goto("+nStart+","+strTag+") returning "+nPos);
}
void SaveWebPage(string strWebPage, string strID)
{
string strFilename = strID+"_"+Year()+Pad(Month())+Pad(Day())+"-"+Pad(Hour())+Pad(Minute())+Pad(Seconds())+".htm";
int handle = FileOpen(strFilename,FILE_BIN|FILE_WRITE);
FileWriteString(handle,strWebPage,StringLen(strWebPage));
FileClose(handle);
}
string Pad(int n)
{
string strPad = "";
if (n <= 9) strPad = "0";
return(strPad+n);
}
*/
//=================================================================================================
//=================================================================================================
//=================================== LogUtils Functions ======================================
//=================================================================================================
//=================================================================================================
void OpenLog(string strName)
{
if (!EnableLogging)
return;
if (logHandle <= 0)
{
string strMonthPad = "";
string strDayPad = "";
if (Month() < 10)
strMonthPad = "0";
if (Day() < 10)
strDayPad = "0";
string strFilename = StringConcatenate(strName, "_", Year(), strMonthPad, Month(), strDayPad, Day(), "_log.txt");
logHandle = FileOpen(strFilename,FILE_CSV|FILE_READ|FILE_WRITE);
Print("logHandle =================================== ", logHandle);
}
if (logHandle > 0)
{
FileFlush(logHandle);
FileSeek(logHandle, 0, SEEK_END);
}
}
void Log(string msg)
{
if (!EnableLogging)
return;
if (logHandle <= 0)
return;
msg = TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS) + " " + msg;
FileWrite(logHandle,msg);
}
//=================================================================================================
//=================================================================================================
//================================= END IMPORTED FUNCTIONS =====================================
//=================================================================================================
//=================================================================================================