Code:
bool skip,lotnotif;
datetime newsTime,calendardate;
int xmlHandle,BoEvent,end,begin,minsTillNews,tmpMins,idxOfNext,dispMinutes[9],newsIdx,next,i,WebUpdateFreq=60;
string xmlFileName,myEvent,mainData[100][7],sData,dispTitle[9],dispCountry[9],dispImpact[9],alertstring,prefix="NEWS_",News[],tmp;
string sUrl="http://www.forexfactory.com/ff_calendar_thisweek.xml";
string sTags[7]=
{
"<title>","<country>","<date><![CDATA[","<time><![CDATA[",
"<impact><![CDATA[","<forecast><![CDATA[","<previous><![CDATA["
};
string eTags[7]=
{
"</title>","</country>","]]></date>","]]></time>",
"]]></impact>","]]></forecast>","]]></previous>"
};
int password_status = -1;
// News Functions
int dpsstart()
{
//if (TimeCurrent() < allowed_until)
{
InitNews(sUrl);
xmlHandle=FileOpen(xmlFileName,FILE_BIN|FILE_READ);
if(xmlHandle>=0)
{
int size=FileSize(xmlHandle);
sData=FileReadString(xmlHandle,size);
FileClose(xmlHandle);
}
ArrayResize(News,0);
Del_MyComment();
newsIdx=0;
tmpMins=10080;
BoEvent= 0;
while(true)
{
BoEvent=StringFind(sData,"<event>",BoEvent);
if(BoEvent==-1) break;
BoEvent+=7;
next=StringFind(sData,"</event>",BoEvent);
if(next == -1) break;
myEvent = StringSubstr(sData, BoEvent, next - BoEvent);
BoEvent = next;
begin= 0;
skip = false;
for(i=0; i < 7; i++)
{
mainData[newsIdx][i]="";
next=StringFind(myEvent,sTags[i],begin);
if(next==-1) continue;
else
{
begin=next+StringLen(sTags[i]);
end=StringFind(myEvent,eTags[i],begin);
if(end>begin && end!=-1)
{mainData[newsIdx][i]=StringSubstr(myEvent,begin,end-begin);}
}
}
if(!IsNewsCurrency(Symbol(),mainData[newsIdx][COUNTRY])){skip=true;}
else if((!MediumImpactNews) && (mainData[newsIdx][IMPACT]=="Medium")){skip=true;}
else if((!LowImpactNews) && (mainData[newsIdx][IMPACT]=="Low")){skip=true;}
else if((StringSubstr(mainData[newsIdx][TITLE],0,4)=="Bank")){skip=true;}
else if(StringSubstr(mainData[newsIdx][TITLE],0,8)!="Daylight"){skip=true;}
else if((mainData[newsIdx][TIME]=="All Day" && mainData[newsIdx][TIME]=="") ||
(mainData[newsIdx][TIME]=="Tentative" && mainData[newsIdx][TIME]=="") ||
(mainData[newsIdx][TIME]=="")){skip=true;}
if(!skip)
{
newsTime=MakeDateTime(mainData[newsIdx][DATE],mainData[newsIdx][TIME]);
minsTillNews=(newsTime-TimeGMT())/60;
if(minsTillNews<0 || MathAbs(tmpMins)>minsTillNews)
{idxOfNext=newsIdx; tmpMins=minsTillNews;}
datetime time=newsTime-TimeGMTOffset();
int ind=ArraySize(News);
ArrayResize(News,ind+1);
News[ind]=TimeToString(time)+"'"+
mainData[newsIdx][COUNTRY]+"'"+
mainData[newsIdx][IMPACT]+"'"+
mainData[newsIdx][Forecast]+"'"+
mainData[newsIdx][Previous]+"'"+
mainData[newsIdx][TITLE];
Add_MyComment();
if((minsTillNews>=0 && minsTillNews<=PauseMinutesBefore))
{
alertstring=mainData[newsIdx][TITLE];
GlobalVariableSet(ipiMagic,false);
ObjectSetString(0,"LableNews",OBJPROP_TEXT,minsTillNews+" minutes until news for "+mainData[newsIdx][COUNTRY]+": "+mainData[newsIdx][TITLE]);
ObjectSetInteger(0,"LableNews",OBJPROP_COLOR,clrRed);
if(PauseMinutesBefore!=0 && minsTillNews>=0 && minsTillNews<=PauseMinutesBefore && alertstring!=tmp)
{
if(AlertOn) { Alert(minsTillNews," minutes until news for "+mainData[newsIdx][COUNTRY],": ",mainData[newsIdx][TITLE]);}
if(NotificationOn) { SendNotification(minsTillNews+" minutes until news for "+mainData[newsIdx][COUNTRY]+": "+mainData[newsIdx][TITLE]);}
tmp=alertstring;
}
break;
}
else
if(minsTillNews<=0 && minsTillNews>=PauseMinutesAfter*-1)
{
GlobalVariableSet(ipiMagic,false);
ObjectSetString(0,"LableNews",OBJPROP_TEXT,minsTillNews+" minutes Since news for "+mainData[newsIdx][COUNTRY]+": "+mainData[newsIdx][TITLE]);
ObjectSetInteger(0,"LableNews",OBJPROP_COLOR,clrRed);
break;
}
else
{
GlobalVariableSet(ipiMagic,true);
ObjectSetString(0,"LableNews",OBJPROP_TEXT,"There Is No Upcoming News For "+PauseMinutesBefore+" Minutes Later");
ObjectSetInteger(0,"LableNews",OBJPROP_COLOR,clrMediumSeaGreen);
}
newsIdx++;
}
}
}
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void InitNews(string newsUrl)
{
if(DoFileDownLoad())
{
DownLoadWebPageToFile(newsUrl);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool DoFileDownLoad()
{
xmlHandle=0;
int size;
datetime time=TimeCurrent();
if(GlobalVariableCheck("Update News File") == false)return(true);
if((time - GlobalVariableGet("Update News File")) > WebUpdateFreq*60)return(true);
xmlFileName=GetXmlFileName();
xmlHandle=FileOpen(xmlFileName,FILE_BIN|FILE_READ);
if(xmlHandle>=0)
{
size=FileSize(xmlHandle);
sData=FileReadString(xmlHandle,size);
FileClose(xmlHandle);
return(false);
}
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string GetXmlFileName()
{
int adjustDays=0;
switch(TimeDayOfWeek(TimeLocal()))
{
case 0:adjustDays=0; break;
case 1:adjustDays=1; break;
case 2:adjustDays=2; break;
case 3:adjustDays=3; break;
case 4:adjustDays=4; break;
case 5:adjustDays=5; break;
case 6:adjustDays=6; break;
}
calendardate=TimeLocal() -(adjustDays *86400);
string fileName=(StringConcatenate(TimeYear(calendardate),"-",
PadString(DoubleToStr(TimeMonth(calendardate),0),"0",2),"-",
PadString(DoubleToStr(TimeDay(calendardate),0),"0",2),"-News",".xml"));
return(fileName);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DownLoadWebPageToFile(string url="http://www.forexfactory.com/ffcal_week_this.xml")
{
int HttpOpen=InternetOpenW(" ",0," "," ",0);
int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1);
int HttpRequest = InternetOpenUrlW(HttpOpen, url, NULL, 0, 0, 0);
int read[1];
uchar Buffer[];
ArrayResize(Buffer,READURL_BUFFER_SIZE+1);
string NEWS="";
xmlFileName=GetXmlFileName();
xmlHandle=FileOpen(xmlFileName,FILE_BIN|FILE_READ|FILE_WRITE);
if(xmlHandle>=0) {FileClose(xmlHandle); FileDelete(xmlFileName);}
xmlHandle=FileOpen(xmlFileName,FILE_BIN|FILE_WRITE);
while(true)
{
bool FL=InternetReadFile(HttpRequest,Buffer,READURL_BUFFER_SIZE,read);
string strThisRead = CharArrayToString(Buffer,0,read[0],CP_UTF8);
if(read[0]> 0)NEWS = NEWS + strThisRead;
else
{
FileWriteString(xmlHandle,NEWS);
FileClose(xmlHandle);
end=StringFind(NEWS,"</weeklyevents>",0);
if(end==-1) {Alert(Symbol()," ",Period(),", Error: File download incomplete!");}
else {GlobalVariableSet("Update News File",TimeCurrent());}
break;
}
}
if(HttpRequest>0) InternetCloseHandle(HttpRequest);
if(HttpConnect>0) InternetCloseHandle(HttpConnect);
if(HttpOpen>0) InternetCloseHandle(HttpOpen);
}
//-----------------------------------------------------------
void dpsLoadWebsite(string inURL="https://www.google.com/finance")
{
string cookie=NULL,headers;
char post[],result[];
int res;
//--- to enable access to the server, you should add URL "https://www.google.com/finance"
//--- in the list of allowed URLs (Main Menu->Tools->Options, tab "Expert Advisors"):
string google_url=inURL;
//--- Reset the last error code
ResetLastError();
//--- Loading a html page from Google Finance
int timeout=5000; //--- Timeout below 1000 (1 sec.) is not enough for slow Internet connection
res=WebRequest("GET",google_url,cookie,NULL,timeout,post,0,result,headers);
//--- Checking errors
if(res==-1)
{
Print("Error in WebRequest. Error code =",GetLastError());
//--- Perhaps the URL is not listed, display a message about the necessity to add the address
MessageBox("Add the address '"+google_url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
}
else
{
//--- Load successfully
PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
//--- Save the data to a file
int filehandle=FileOpen("GoogleFinance.htm",FILE_WRITE|FILE_BIN);
//--- Checking errors
if(filehandle!=INVALID_HANDLE)
{
//--- Save the contents of the result[] array to a file
FileWriteArray(filehandle,result,0,ArraySize(result));
//--- Close the file
FileClose(filehandle);
}
else Print("Error in FileOpen. Error code=",GetLastError());
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string PadString(string toBePadded,string paddingChar,int paddingLength)
{
while(StringLen(toBePadded)<paddingLength)
{
toBePadded=StringConcatenate(paddingChar,toBePadded);
}
return (toBePadded);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsNewsCurrency(string cSymbol,string fSymbol)
{
if(USDNews && fSymbol == "USD"){return(true);}
if(GBPNews && fSymbol == "GBP"){return(true);}
if(EURNews && fSymbol == "EUR"){return(true);}
if(CADNews && fSymbol == "CAD"){return(true);}
if(AUDNews && fSymbol == "AUD"){return(true);}
if(CHFNews && fSymbol == "CHF"){return(true);}
if(JPYNews && fSymbol == "JPY"){return(true);}
if(NZDNews && fSymbol == "NZD"){return(true);}
if(CNYNews && fSymbol == "CNY"){return(true);}
return(false);
}
//+-----------------------------------------------------------------------------------------------+
//| Indicator Subroutine For Date/Time changed by deVries |
//+-----------------------------------------------------------------------------------------------+
datetime MakeDateTime(string strDate,string strTime) //not string now datetime
{
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,4);
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;}
if((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) {nHour24 = 0;}
datetime newsevent=StringToTime(strYear+"."+strMonth+"."+
strDay)+nHour24*3600+(StringToInteger(strMinute)*60);
return(newsevent);
}
//+------------------------------------------------------------------+
//| Create a text label |
//+------------------------------------------------------------------+
bool LabelCreate(const string name="Label", // label name
const int x=0, // X coordinate
const int y=0, // Y coordinate
const string text="Label", // text
const color clr=clrRed) // priority for mouse click
{
ObjectDelete(0,name);
ResetLastError();
if(!ObjectCreate(0,name,OBJ_LABEL,0,0,0))
{
Print(__FUNCTION__,": failed to create text label! Error code = ",GetLastError());
return(false);
}
ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);
ObjectSetString(0,name,OBJPROP_TEXT,text);
ObjectSetString(0,name,OBJPROP_FONT,"Segoe UI");
ObjectSetInteger(0,name,OBJPROP_FONTSIZE,8);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_BACK,false);
ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
ObjectSetInteger(0,name,OBJPROP_SELECTED,false);
ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Add_MyComment()
{
string name=prefix+"Time";
LabelCreate(name,30,35,"Time",clrGray);
//----
name=prefix+"Country";
LabelCreate(name,160,35,"Country",clrGray);
//----
name=prefix+"Impact";
LabelCreate(name,260,35,"Impact",clrGray);
//----
name=prefix+"Forecast";
LabelCreate(name,360,35,"Forecast",clrGray);
//----
name=prefix+"Previous";
LabelCreate(name,460,35,"Previous",clrGray);
//----
name=prefix+"Title";
LabelCreate(name,560,35,"Title",clrGray);
//----
name=prefix+"Separator";
LabelCreate(name,20,35+20,"---------------------------------------------------------------",clrGray);
//----
name=prefix+"Separator2";
LabelCreate(name,272,35+20,"---------------------------------------------------------------",clrGray);
//----
name=prefix+"Separator3";
LabelCreate(name,524,35+20,"---------------------------------------------------------------",clrGray);
int size=ArraySize(News);
string result[];
ushort u_sep;
int d;
color clr;
//----
for(i=0; i<size; i++)
{
u_sep=StringGetCharacter("'",0);
d=StringSplit(News[i],u_sep,result);
if(result[2]=="High"){clr=clrRed;}
else if(result[2]=="Medium"){clr=clrOrange;}
else {clr=clrYellow;}
name=prefix+"Time"+(string)i;
LabelCreate(name,30,35+20*(i+2),result[0],clr);
//----
name=prefix+"Country"+(string)i;
LabelCreate(name,160,35+20*(i+2),result[1],clr);
//----
name=prefix+"Impact"+(string)i;
LabelCreate(name,260,35+20*(i+2),result[2],clr);
//----
name=prefix+"Forecast"+(string)i;
LabelCreate(name,360,35+20*(i+2),result[3],clr);
//----
name=prefix+"Previous"+(string)i;
LabelCreate(name,460,35+20*(i+2),result[4],clr);
//----
name=prefix+"Title"+(string)i;
LabelCreate(name,560,35+20*(i+2),result[5],clr);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Del_MyComment()
{
for(i=ObjectsTotal()-1;i>=0;i--)
{
if(StringFind(ObjectName(i),prefix)>-1)
ObjectDelete(ObjectName(i));
}
}
//+------------------------------------------------------------------+
Regards,
Bookmarks