#property copyright "© 2011"
#property link ""
#define vers "1.2.4"
#property indicator_chart_window
#property indicator_buffers 0
extern string _tmp1_ = " --- News ---";
extern string sUrl = "http://www.forexfactory.com/ff_calendar_thisweek.xml";
extern int NewsReload.mi = 60;
extern bool SymbolsFilter.On = true;
extern string SymbolsFilter = "EUR,USD";
extern bool ShowOnlyCurrDay = false;
extern bool IncludeHigh = true;
extern bool IncludeMedium = true;
extern bool IncludeLow = false;
extern bool IgnoreFilter.On = true;
extern string IgnoreFilter = "holiday,speaks,tentative";
extern int NewsSite.GMT.Offset = 0;
extern int Broker.GMT.Offset = 0;
extern int News.HighLightPrior.mi = 10;
extern int News.HighLightAfter.mi = 45;
extern bool Debug = false;
extern string _tmp2_ = " --- Graphic ---";
extern bool NewsLabels.Show = true;
extern int NewsLabels.corner = 1;
extern string NewsLabels.cols = "5,200,40,80,50";
extern int NewsLabels.row0_offset = 30;
extern int NewsLabels.rows_size = 14;
extern string NewsLabels.FontName = "Verdana";
extern int NewsLabels.FontSize = 8;
extern color NewsLabels.RegularRows = Gold;
extern color NewsLabels.HighlightedRows = Tomato;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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
);
#import
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double DataBuff[];
datetime ReloadTime = 0;
int LastDayOfYear = -1;
string OutputFile = "NewsItems.csv";
int DebugLevel = 1;
string NewsArr[][8];
int i_title = 0;
int i_country = 1;
int i_date = 2;
int i_time = 3;
int i_impact = 4;
int i_forecast = 5;
int i_previous = 6;
string prefix = "";
int NewsTotal = 0;
int NewsRes = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void PrintD(string msg)
{
if (Debug) Print(msg);
}
void init()
{
IndicatorBuffers(1);
SetIndexBuffer(0, DataBuff);
SetIndexStyle(0, DRAW_NONE);
prefix = WindowExpertName() + "_";
//Warnings off
if (false)
{
int x;
string s;
OpenLog("");
LogFlush();
GrabWeb("", s);
//GetData("", 0, "", "", x);
Goto("", 0, "", x);
SaveWebPage("", "");
Pad(0);
PrintD("");
}
ClearObj();
return(0);
}
void deinit()
{
ClearObj();
}
void start()
{
if (!IsDllsAllowed())
{
Alert(WindowExpertName() + ": you need to allow DLL imports");
return;
}
//-----
main();
//-----
ArrayInitialize(DataBuff, -1);
int ind_arr[];
GetHighlightedNews(ind_arr);
int size = ArraySize(ind_arr);
if (size > 0)
{
//Comment("\n ", ind_arr[0][i_title]);
for (int i=0; i < size; i++)
{
DataBuff[i] = ind_arr[i];
}
}
}
void main()
{
if (IsTesting())
{
if (NewsTotal == 0)
{
ClearObj();
GetNews();
}
}
else
{
if (LastDayOfYear != DayOfYear())
{
ReloadTime = TimeCurrent();
LastDayOfYear = DayOfYear();
}
if (TimeCurrent() < ReloadTime) return;
ClearObj();
GetNews();
if (NewsTotal > 0)
{
ReloadTime = TimeCurrent() + NewsReload.mi*60;
}
else
{
ReloadTime = TimeCurrent() + 1*60;
return;
}
}
//-----
string obj_name, txt;
datetime tm0 = TimeCurrent() - (Broker.GMT.Offset - NewsSite.GMT.Offset)*60*60;
obj_name = prefix + "TimeAdj";
txt = "NewsSite time: " + TimeToStr(tm0, TIME_DATE|TIME_SECONDS);
DrawNewsLabel(obj_name, 80, NewsLabels.row0_offset, txt, NewsLabels.FontSize, NewsLabels.RegularRows);
DrawNews();
}
void GetNews()
{
string myUrl = sUrl;
if (DebugLevel > 1)
Print("myUrl == ", myUrl);
// THIS CALL WAS DONATED BY PAUL TO HELP FIX THE RESOURCE ERROR
string FileName, sData;
int hFile;
//-----
GrabWeb(myUrl, sData);
// Write the contents of the ForexFactory page to an .xml file
FileName = "" + Day()+ "-"+ Month() +"-"+ Year() +"-"+ "grabweb.xml";
hFile = FileOpen(FileName, FILE_BIN|FILE_READ|FILE_WRITE);
if (hFile < 1)
{
if (DebugLevel > 0)
Print("Can\'t open xml file, the last error is ", GetLastError());
return(false);
}
FileWriteString(hFile, sData, StringLen(sData));
FileClose(hFile);
//--- Look for the end tag to ensure that a complete page was downloaded ---//
int beginning = 1;
beginning = StringFind(sData,"",beginning);
if (beginning <= 0)
{
Alert("GetNewsFF Error - Web page download was not complete!");
return(false);
}
//-----
/*
FileName = "" + Day()+ "-"+ Month() +"-"+ Year() +"-"+ "grabweb.xml";
hFile = FileOpen(FileName, FILE_CSV|FILE_READ);
if (hFile < 1)
{
if (DebugLevel > 0)
Print("Can\'t open xml file, the last error is ", GetLastError());
return(false);
}
sData = "";
string line;
while (!FileIsEnding(hFile))
{
line = FileReadString(hFile);
sData = sData + line;
sData = sData + "\r\n";
//Print(line);
}
FileClose(hFile);
*/
//-------------------------------------------------------------------------------//
// Now the .htm file is written; let's parse it now and write to csv file
hFile = FileOpen(OutputFile, FILE_CSV|FILE_WRITE,",");
if (hFile < 1)
{
if (DebugLevel > 0)
Print("Can\'t open csv file, the last error is ", GetLastError());
return(false);
}
FileWrite(hFile, "Title", "Country", "Date", "Time", "Impact", "Actual", "Forecast", "Previous");
ParseData(sData, hFile);
FileClose(hFile);
return (0);
}
void ParseData(string& sData, int handle)
{
NewsTotal = 0;
ArrayResize(NewsArr, 0);
string a.weeklyevents[];
split_ex(a.weeklyevents, sData, "", "");
if (ArraySize(a.weeklyevents) < 1) return;
string a.event[];
split_ex(a.event, a.weeklyevents[0], "", "");
string a.title[], a.country[], a.date[], a.time[], a.impact[], a.forecast[], a.previous[];
string title, country, date, time, impact, actual, forecast, previous;
string l_title, l_time, l_impact;
string mm_dd_yyyy[];
int mm, dd, yyyy;
int size = ArraySize(a.event);
//Alert(size);
for (int i=0; i < size; i++)
{
split_ex(a.title, a.event[i], "
", "");
split_ex(a.country, a.event[i], "", "");
split_ex(a.date, a.event[i], "", "");
split_ex(a.time, a.event[i], "");
split_ex(a.impact, a.event[i], "", "");
split_ex(a.forecast, a.event[i], "", "");
split_ex(a.previous, a.event[i], "", "");
a.title[0] = Comment2Str(a.title[0], "");
a.country[0] = Comment2Str(a.country[0], "");
a.date[0] = Comment2Str(a.date[0], "");
a.time[0] = Comment2Str(a.time[0], "");
a.impact[0] = Comment2Str(a.impact[0], "");
a.forecast[0] = Comment2Str(a.forecast[0], "");
a.previous[0] = Comment2Str(a.previous[0], "");
title = if_s(ArraySize(a.title) > 0, a.title[0], "");
country = if_s(ArraySize(a.country) > 0, a.country[0], "");
date = if_s(ArraySize(a.date) > 0, a.date[0], "");
time = if_s(ArraySize(a.time) > 0, a.time[0], "");
impact = if_s(ArraySize(a.impact) > 0, a.impact[0], "");
forecast = if_s(ArraySize(a.forecast) > 0, a.forecast[0], "");
previous = if_s(ArraySize(a.previous) > 0, a.previous[0], "");
split(mm_dd_yyyy, date, "-");
if (ArraySize(mm_dd_yyyy) < 3) continue;
mm = StrToInteger(mm_dd_yyyy[0]);
dd = StrToInteger(mm_dd_yyyy[1]);
yyyy = StrToInteger(mm_dd_yyyy[2]);
date = "" + yyyy + "." + if_s(mm<10, "0"+mm, ""+mm) + "." + if_s(dd<10, "0"+dd, ""+dd);
//Alert(date, " ", mm, " ", dd, " ", yyyy);
//Alert(a.event[i]);
NewsTotal++;
//-----
if (mm != Month() || yyyy != Year()) continue;
if (ShowOnlyCurrDay)
{
if (dd != Day()) continue;
}
l_title = ToLower(title);
l_time = ToLower(time);
l_impact = ToLower(impact);
if (SymbolsFilter.On)
{
if (ApplySymbolsFilter(country) < 0) continue;
}
if (!IncludeHigh && StringFind(l_impact, "high") != -1) continue;
if (!IncludeMedium && StringFind(l_impact, "medium") != -1) continue;
if (!IncludeLow && StringFind(l_impact, "low") != -1) continue;
if (IgnoreFilter.On)
{
if (ApplyIgnoreFilter(l_impact) >= 0) continue;
}
//-----
if (StringFind(l_time, ":") != -1) l_time = TimeConvert12To24(l_time);
actual = "";
FileWrite(handle, title, country, date, time, impact, actual, forecast, previous);
//Print(title, ", ", country, ", ", date, ", ", time, ", ", impact, ", ", actual, ", ", forecast, ", ", previous);
int ind = ArrayRange(NewsArr, 0);
ArrayResize(NewsArr, ind+1);
NewsArr[ind][i_title] = title;
NewsArr[ind][i_country] = country;
NewsArr[ind][i_date] = date;
NewsArr[ind][i_time] = l_time;
NewsArr[ind][i_impact] = impact;
NewsArr[ind][i_forecast] = forecast;
NewsArr[ind][i_previous] = previous;
}
}
void DrawNews()
{
//by NewsSite time
datetime tm0 = TimeCurrent() - (Broker.GMT.Offset - NewsSite.GMT.Offset)*60*60;
int ind = ArrayRange(NewsArr, 0);
int j = 0;
int dy = NewsLabels.row0_offset + 3*NewsLabels.rows_size;
string obj_name, txt;
int cols_arr[];
split_i(cols_arr, NewsLabels.cols, ",");
int cols_cnt = ArraySize(cols_arr);
for (int i=0; i < ind; i++)
{
//by NewsSite time
datetime news_tm = MakeTimeNS(NewsArr[i][i_date], NewsArr[i][i_time]);
datetime tm_before = news_tm - News.HighLightPrior.mi*60;
datetime tm_after = news_tm + News.HighLightAfter.mi*60;
int dx = 0;
color FontColor = NewsLabels.RegularRows;
if (tm_before <= tm0 && tm0 <= tm_after)
{
FontColor = NewsLabels.HighlightedRows;
}
obj_name = prefix + "news" + "_r" + (i) + "_col" + (i_title);
dx += if_i(cols_cnt>0, cols_arr[0], 50);
txt = NewsArr[i][i_title];
DrawNewsLabel(obj_name, dx, dy, txt, NewsLabels.FontSize, FontColor);
obj_name = prefix + "news" + "_r" + (i) + "_col" + (i_country);
dx += if_i(cols_cnt>1, cols_arr[1], 50);
txt = NewsArr[i][i_country];
DrawNewsLabel(obj_name, dx, dy, txt, NewsLabels.FontSize, FontColor);
obj_name = prefix + "news" + "_r" + (i) + "_col" + (i_date);
dx += if_i(cols_cnt>2, cols_arr[2], 50);
txt = NewsArr[i][i_date];
DrawNewsLabel(obj_name, dx, dy, txt, NewsLabels.FontSize, FontColor);
obj_name = prefix + "news" + "_r" + (i) + "_col" + (i_time);
dx += if_i(cols_cnt>3, cols_arr[3], 50);
txt = NewsArr[i][i_time];
DrawNewsLabel(obj_name, dx, dy, txt, NewsLabels.FontSize, FontColor);
obj_name = prefix + "news" + "_r" + (i) + "_col" + (i_impact);
dx += if_i(cols_cnt>4, cols_arr[4], 50);
txt = NewsArr[i][i_impact];
DrawNewsLabel(obj_name, dx, dy, txt, NewsLabels.FontSize, FontColor);
dy += NewsLabels.rows_size;
j++;
}
}
void GetHighlightedNews(int& ind_arr[])
{
ArrayResize(ind_arr, 0);
datetime tm0 = TimeCurrent() - (Broker.GMT.Offset - NewsSite.GMT.Offset)*60*60;
int ind = ArrayRange(NewsArr, 0);
for (int i=ind-1; i >= 0; i--)
{
//by NewsSite time
datetime news_tm = MakeTimeNS(NewsArr[i][i_date], NewsArr[i][i_time]);
datetime tm_before = news_tm - News.HighLightPrior.mi*60;
datetime tm_after = news_tm + News.HighLightAfter.mi*60;
if (tm_before <= tm0 && tm0 <= tm_after)
{
int size = ArraySize(ind_arr);
ArrayResize(ind_arr, size+1);
ind_arr[size] = i;
}
}
}
int ApplySymbolsFilter(string key)
{
string f_arr[];
string f_arr_l;
string key_l = ToLower(key);
split(f_arr, SymbolsFilter, ",");
int size = ArraySize(f_arr);
for (int i=0; i < size; i++)
{
f_arr_l = ToLower(f_arr[i]);
if (f_arr_l == key_l) return (i);
}
return (-1);
}
int ApplyIgnoreFilter(string key)
{
string f_arr[];
string f_arr_l;
string key_l = ToLower(key);
split(f_arr, IgnoreFilter, ",");
int size = ArraySize(f_arr);
for (int i=0; i < size; i++)
{
f_arr_l = ToLower(f_arr[i]);
if (f_arr_l == key_l) return (i);
}
return (-1);
}
//from AM/PM to 24
string TimeConvert12To24(string tm)
{
string ftm = StringTrimLeft(tm);
ftm = StringTrimRight(ftm);
ftm = ToLower(ftm);
string TimeItems[];
int hh;
//AM
if (StringFind(ftm, "am") != -1)
{
ftm = StringSubstr(tm, 0, StringLen(ftm) - 2);
ftm = StringTrimRight(ftm);
split(TimeItems, ftm, ":");
if (ArraySize(TimeItems) < 2) return (tm);
hh = StrToInteger(TimeItems[0]);
if (hh == 12)
{
//12 am = 00:00
hh = 0;
}
else
{
//1 am = 1:00
//...
//11 am = 11:00
//no action
}
ftm = hh + ":" + TimeItems[1];
if (hh < 10) ftm = "0" + ftm;
return (ftm);
}
//PM
if (StringFind(ftm, "pm") != -1)
{
ftm = StringSubstr(ftm, 0, StringLen(ftm) - 2);
ftm = StringTrimRight(ftm);
split(TimeItems, ftm, ":");
if (ArraySize(TimeItems) < 2) return (tm);
hh = StrToInteger(TimeItems[0]);
if (hh == 12)
{
//12 pm = 12:00
//no action
}
else
{
hh += 12;
}
if (hh >= 24) hh -= 24;
ftm = hh + ":" + TimeItems[1];
if (hh < 10) ftm = "0" + ftm;
return (ftm);
}
return (tm);
}
/*
string MakeTime(string strTime)
{
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" && nHour24 != 12)
{
nHour24 += 12;
}
string strHourPad = "";
if (nHour24 < 10) strHourPad = "0";
return (StringConcatenate(strHourPad, nHour24, ":", strMinute));
}
*/
datetime MakeTimeNS(string sDate, string sTime)
{
//Print(sDate, " ", sTime);
int dd, mm, yyyy;
int hh, mi;
string yyy_mm_dd[];
string hh_mi[];
split(yyy_mm_dd, sDate, ".");
if (ArraySize(yyy_mm_dd) < 3) return (0);
yyyy = StrToInteger(yyy_mm_dd[0]);
mm = StrToInteger(yyy_mm_dd[1]);
dd = StrToInteger(yyy_mm_dd[2]);
split(hh_mi, sTime, ":");
if (ArraySize(hh_mi) < 2) return (0);
hh = StrToInteger(hh_mi[0]);
mi = StrToInteger(hh_mi[1]);
datetime res = StrToTime("" + yyyy + "." + mm + "." + dd + " " + hh + ":" + mi);
return (res);
}
void split(string& arr[], string str, string sym)
{
ArrayResize(arr, 0);
string item;
int pos, size;
int len = StringLen(str);
for (int i=0; i < len;) {
pos = StringFind(str, sym, i);
if (pos == -1) pos = len;
item = StringSubstr(str, i, pos-i);
item = StringTrimLeft(item);
item = StringTrimRight(item);
size = ArraySize(arr);
ArrayResize(arr, size+1);
arr[size] = item;
i = pos+1;
}
}
void split_i(int& arr[], string str, string sym)
{
ArrayResize(arr, 0);
string item;
int pos, size;
int len = StringLen(str);
for (int i=0; i < len;) {
pos = StringFind(str, sym, i);
if (pos == -1) pos = len;
item = StringSubstr(str, i, pos-i);
item = StringTrimLeft(item);
item = StringTrimRight(item);
size = ArraySize(arr);
ArrayResize(arr, size+1);
arr[size] = StrToInteger(item);
i = pos+1;
}
}
void split_ex(string& arr[], string str, string marker1, string marker2)
{
ArrayResize(arr, 0);
string item;
int pos1, pos2, size;
int len = StringLen(str);
for (int i=0; i < len;)
{
pos1 = StringFind(str, marker1, i);
if (pos1 == -1) break;
pos1 += StringLen(marker1);
pos2 = StringFind(str, marker2, pos1);
if (pos2 == -1) break;
item = "";
if (pos2 > pos1)
{
if (pos2-pos1 >= 4096) item = str + str;
item = StringSubstr(str, pos1, pos2-pos1);
item = StringTrimLeft(item);
item = StringTrimRight(item);
}
size = ArraySize(arr);
ArrayResize(arr, size+1);
arr[size] = item;
i = pos2 + StringLen(marker2);
}
}
string Comment2Str(string comment, string m1, string m2)
{
int pos1 = StringFind(comment, m1);
if (pos1 == -1) return (comment);
int pos2 = StringFind(comment, m2);
if (pos2 == -1) return (comment);
pos1 += StringLen(m1);
string sVal = StringSubstr(comment, pos1, pos2-pos1);
sVal = StringTrimLeft(sVal);
sVal = StringTrimRight(sVal);
return (sVal);
}
string if_s(bool cond, string if_true, string if_false)
{
if (cond) return (if_true);
return (if_false);
}
int if_i(bool cond, int if_true, int if_false)
{
if (cond) return (if_true);
return (if_false);
}
string ToLower(string str)
{
int len = StringLen(str);
for (int j=0; j < len; j++)
{
int ch = StringGetChar(str, j);
if (ch >= 'A' && ch <= 'Z')
{
ch += 'a' - 'A';
str = StringSetChar(str, j, ch);
}
}
return (str);
}
void DrawNewsLabel(string obj_name, int x, int y, string txt, int FontSize, color FontColor)
{
if (!NewsLabels.Show) return;
if (ObjectFind(obj_name) == -1) {
ObjectCreate(obj_name, OBJ_LABEL, 0, 0, 0);
}
ObjectSet(obj_name, OBJPROP_XDISTANCE, x);
ObjectSet(obj_name, OBJPROP_YDISTANCE, y);
ObjectSet(obj_name, OBJPROP_CORNER, NewsLabels.corner);
ObjectSetText(obj_name, txt, FontSize, NewsLabels.FontName, FontColor);
//WindowRedraw();
}
void ClearObj()
{
int total = ObjectsTotal();
for (int i=total-1; i >= 0; i--)
{
string name = ObjectName(i);
if (StringFind(name, prefix) == 0) ObjectDelete(name);
}
}
//+------------------------------------------------------------------+
//| WinInet |
//+------------------------------------------------------------------+
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;
#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 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 |
//+------------------------------------------------------------------+
int nDaysSince1970 = 0;
int LogHandle = -1;
bool bEnableLogging = false; // set this true in EA init()
void OpenLog(string strName)
{
if (!bEnableLogging) return;
if (TimeCurrent()/(60*60*24) > nDaysSince1970 || LogHandle <= 0 )
{
// startup or new day
if (LogHandle > 0) FileClose(LogHandle);
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);
nDaysSince1970 = TimeCurrent()/(60*60*24);
}
FileFlush(LogHandle);
FileSeek(LogHandle,0,SEEK_END);
}
void Log(string msg)
{
if (!bEnableLogging) return;
if (LogHandle <= 0) return;
msg = TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS) + " " + msg;
FileWrite(LogHandle,msg);
}
void LogFlush()
{
FileFlush(LogHandle);
}