Reputation: 103
I am builing an EA which fetch news from http://nfs.faireconomy.media/ff_calendar_thisweek.xml
and set the values to variable News_Trade_Status
Logical Conditions (required):
Before X minutes from news ==> string News_Trade_Status = “Paused”;
X minutes after news ==> string News_Trade_Status = “Paused”;
Current Error with Code:
While setting up an EA, it checks logical conditions and based on that, it sets values to News_Trade_Status (Active or Paused)
. However, after the setup, it does not repeat the condition check whenever news comes.
My Code :
//+------------------------------------------------------------------+
//| News Filter |
//| Copyright 2018, RRS |
//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "NewsFilter"
#property link "https://example.com"
#import "urlmon.dll"
int URLDownloadToFileW(int pCaller,string szURL,string szFileName,int dwReserved,int Callback);
#import
//---
#define INAME "FFCPing"+_Symbol
#define TITLE 0
#define COUNTRY 1
#define DATE 2
#define TIME 3
#define IMPACT 4
#define FORECAST 5
#define PREVIOUS 6
input string p01="***NEWS FILTER***"; //******** NEWS FILTER ******
input bool news_High =true; //. Use High News Filter
input bool News_High_Line =true; //. Use High news line (Red Line)
input int minBefore =120; //. Minutes Before High impact News
input int minAfter =120; //. Minutes After High impact News
input bool news_Medium =true; //. Use Medium News Filter
input bool News_Medium_Line =true; //. Use Medium news line (Orange Line)
input int minBefore1 =60; //. Minutes Before Medium impact News
input int minAfter1 =60; //. Minutes After Medium impact News
input bool news_Low =true; //. Use Low News Filter
input bool News_Low_Line =true; //. Use Low news line (Yellow Line)
input int minBefore2 =30; //. Minutes Before Low impact News
input int minAfter2 =30; //. Minutes After Low impact News
//News status
int comment_on = 1;
string News_Trade_Status = "Paused";
string xmlFileName;
string sData;
string Event[200][7];
string eTitle[200][200],eCountry[200][200],eImpact[200][200],eForecast[200][200],ePrevious[200][200];
bool assignVal=true;
int eMinutes[10];
datetime eTime[200][200];
datetime xmlModifed;
int TimeOfDay;
datetime Midnight;
bool IsEvent;
//Oninst
int OnInit()
{
//--- get today time
TimeOfDay=(int)TimeLocal()%86400;
Midnight=TimeLocal()-TimeOfDay;
//--- set xml file name ffcal_week_this (fixed name)
xmlFileName=INAME+"-ffcal_week_this.xml";
//--- checks the existence of the file.
if(!FileIsExist(xmlFileName))
{
xmlDownload();
xmlRead();
}
//--- else just read it
else
xmlRead();
//--- get last modification time
xmlModifed=(datetime)FileGetInteger(xmlFileName,FILE_MODIFY_DATE,false);
//--- check for updates
if(!FileIsExist(xmlFileName))
{
if(xmlModifed<TimeLocal()-(4*3600))
{
Print(INAME+": xml file is out of date");
xmlUpdate();
}
//--- set timer to update old xml file every x hours
EventSetTimer(4*3600);
}
assignVal=true;
return(INIT_SUCCEEDED);
}
int deinit()
{
return (0);
}
//Onstart
int start()
{
string display_text = "News Status:" + News_Trade_Status;
Comment(display_text);
if(!isNewsPause(_Symbol,0)) { News_Trade_Status = "Active"; }
return (0);
}
//Functions
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
assignVal=true;
Print(INAME+": xml file is out of date");
xmlUpdate();
//---
}
//+------------------------------------------------------------------+
//| Creating Text object |
//+------------------------------------------------------------------+
bool TextCreate(const long chart_ID=0, // chart's ID
const string name="Text", // object name
const int sub_window=0, // subwindow index
datetime time=0, // anchor point time
double price=0, // anchor point price
const string text="Text", // the text itself
const color clr=clrRed, // color
const string font="Arial", // font
const int font_size=10, // font size
const double angle=0.0, // text slope
const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_LOWER, // anchor type
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- reset the error value
ResetLastError();
//--- create Text object
if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))
{
Print(__FUNCTION__,
": failed to create \"Text\" object! Error code = ",GetLastError());
return(false);
}
//--- set the text
ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the object by mouse
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| Create the horizontal line |
//+------------------------------------------------------------------+
bool VLineCreate(const long chart_ID=0, // chart's ID
const string name="VLine", // line name
const int sub_window=0, // subwindow index
datetime time=0, // line time
const color clr=clrRed, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- create a vertical line
if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time,0))
{
return(false);
}
//--- set line color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool HLineCreate(const long chart_ID=0, // chart's ID
const string name="HLine", // line name
const int sub_window=0, // subwindow index
double price=0, // line price
const color clr=clrRed, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=false, // in the background
const bool selection=false, // highlight to move
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- reset the error value
ResetLastError();
//--- create a horizontal line
if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
{
Print(__FUNCTION__,
": failed to create a horizontal line! Error code = ",GetLastError());
return(false);
}
//--- set line color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void xmlDownload()
{
Sleep(3000);
//---
ResetLastError();
/*string sUrl="http://nfs.faireconomy.media/ff_calendar_thisweek.xml";
string FilePath=StringConcatenate(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL4\\files\\",xmlFileName);
int FileGet=URLDownloadToFileW(NULL,sUrl,FilePath,0,NULL);
if(FileGet==0)
PrintFormat(INAME+": %s file downloaded successfully!",xmlFileName);
//--- check for errors
else
PrintFormat(INAME+": failed to download %s file, Error code = %d",xmlFileName,GetLastError());*/
string cookie=NULL, headers;
string reqheaders="User-Agent: Mozilla/4.0\r\n";
char post[],result[];
int res;
string url="http://nfs.faireconomy.media/ff_calendar_thisweek.xml";
ResetLastError();
int timeout=5000;
res=WebRequest("GET",url,reqheaders,timeout,post,result,headers);
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 '"+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(xmlFileName,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());
}
//---
}
//+------------------------------------------------------------------+
//| Read the XML file |
//+------------------------------------------------------------------+
void xmlRead()
{
//---
ResetLastError();
sData="";
ulong pos[];
int size_;
Print("Reading file");
int FileHandle=FileOpen(xmlFileName,FILE_READ|FILE_BIN|FILE_ANSI);
if(FileHandle!=INVALID_HANDLE)
{
//--- receive the file size
ulong size=FileSize(FileHandle);
//--- read data from the file
while(!FileIsEnding(FileHandle))
sData=FileReadString(FileHandle,(int)size);
//--- close
FileClose(FileHandle);
}
//--- check for errors
else
PrintFormat(INAME+": failed to open %s file, Error code = %d",xmlFileName,GetLastError());
Print("Done.");
//---
}
//+------------------------------------------------------------------+
//| Check for update XML |
//+------------------------------------------------------------------+
void xmlUpdate()
{
Sleep(3000);
//--- do not download on saturday
if(TimeDayOfWeek(Midnight)==6)
return;
else
{
Print(INAME+": check for updates...");
Print(INAME+": delete old file");
FileDelete(xmlFileName);
xmlDownload();
xmlRead();
xmlModifed=(datetime)FileGetInteger(xmlFileName,FILE_MODIFY_DATE,false);
PrintFormat(INAME+": updated successfully! last modified: %s",(string)xmlModifed);
}
//---
}
//+------------------------------------------------------------------+
//| Converts ff time & date into yyyy.mm.dd hh:mm - by deVries |
//+------------------------------------------------------------------+
string MakeDateTime(string strDate,string strTime)
{
//---
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);
string tempStr[];
StringSplit(strTime,StringGetCharacter(":",0),tempStr);
int nTimeColonPos=StringFind(strTime,":");
string strHour=tempStr[0];
string strMinute=StringSubstr(tempStr[1],0,2);
string strAM_PM=StringSubstr(tempStr[1],2,2);
int nHour24=StringToInteger(strHour);
if((strAM_PM=="pm" || strAM_PM=="PM") && nHour24!=12)
nHour24+=12;
if((strAM_PM=="am" || strAM_PM=="AM") && nHour24==12)
nHour24=0;
string strHourPad="";
if(nHour24<10)
strHourPad="0";
return((strYear+"."+strMonth+"."+strDay+" "+strHourPad+nHour24+":"+strMinute));
//---
}
//+------------------------------------------------------------------+
//| Convert day of the week to text |
//+------------------------------------------------------------------+
string DayToStr(datetime time)
{
int ThisDay=TimeDayOfWeek(time);
string day="";
switch(ThisDay)
{
case 0:
day="Sun";
break;
case 1:
day="Mon";
break;
case 2:
day="Tue";
break;
case 3:
day="Wed";
break;
case 4:
day="Thu";
break;
case 5:
day="Fri";
break;
case 6:
day="Sat";
break;
}
return(day);
}
//+------------------------------------------------------------------+
//| Convert months to text |
//+------------------------------------------------------------------+
string MonthToStr()
{
int ThisMonth=Month();
string month="";
switch(ThisMonth)
{
case 1:
month="Jan";
break;
case 2:
month="Feb";
break;
case 3:
month="Mar";
break;
case 4:
month="Apr";
break;
case 5:
month="May";
break;
case 6:
month="Jun";
break;
case 7:
month="Jul";
break;
case 8:
month="Aug";
break;
case 9:
month="Sep";
break;
case 10:
month="Oct";
break;
case 11:
month="Nov";
break;
case 12:
month="Dec";
break;
}
return(month);
}
string nextNews="...";
int total=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int UpcomingNewsImpact(string symb,int n)
{
nextNews="...";
string MainSymbol=StringSubstr(symb,0,3);
string SecondSymbol=StringSubstr(symb,3,3);
//---
if(assignVal)
{
//--- BY AUTHORS WITH SOME MODIFICATIONS
//--- define the XML Tags, Vars
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 index=0;
int next=-1;
int BoEvent=0,begin=0,end=0;
string myEvent="";
//--- Minutes calculation
datetime EventTime=0;
int EventMinute=0;
//--- split the currencies into the two parts
//--- loop to get the data from xml tags
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;
for(int i=0; i<7; i++)
{
Event[index][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...
//--- Advance past the start tag
begin=next+StringLen(sTags[i]);
end=StringFind(myEvent,eTags[i],begin);
//---Find start of end tag and Get data between start and end tag
if(end>begin && end!=-1)
Event[index][i]=StringSubstr(myEvent,begin,end-begin);
}
}
//--- filters that define whether we want to skip this particular currencies or events
//if(!IsCurrency(Event[index][COUNTRY]))
//continue;
/*if(importance!=0 && Event[index][IMPACT]=="High")
continue;
if(importance!=1 && Event[index][IMPACT]=="Medium")
continue;
if(importance!=2 && Event[index][IMPACT]=="Low")
continue;
if(!IncludeSpeaks && StringFind(Event[index][TITLE],"Speaks")!=-1)
continue;
if(!IncludeHolidays && Event[index][IMPACT]=="Holiday")
continue;*/
if(Event[index][TIME]=="Tentative" ||
Event[index][TIME]=="")
continue;
//--- sometimes they forget to remove the tags :)
if(StringFind(Event[index][TITLE],"<![CDATA[")!=-1)
StringReplace(Event[index][TITLE],"<![CDATA[","");
if(StringFind(Event[index][TITLE],"]]>")!=-1)
StringReplace(Event[index][TITLE],"]]>","");
if(StringFind(Event[index][TITLE],"]]>")!=-1)
StringReplace(Event[index][TITLE],"]]>","");
//---
if(StringFind(Event[index][FORECAST],"<")!=-1)
StringReplace(Event[index][FORECAST],"<","");
if(StringFind(Event[index][PREVIOUS],"<")!=-1)
StringReplace(Event[index][PREVIOUS],"<","");
//--- set some values (dashes) if empty
if(Event[index][FORECAST]=="")
Event[index][FORECAST]="---";
if(Event[index][PREVIOUS]=="")
Event[index][PREVIOUS]="---";
//--- Convert Event time to MT4 time
string evD=MakeDateTime(Event[index][DATE],Event[index][TIME]);
EventTime=datetime(evD);
index++;
}
//--- loop to set arrays/buffers that uses to draw objects and alert
for(int ii=0; ii<index; ii++)
{
if(Event[index][TIME]=="All Day" && Event[ii][IMPACT]=="Holiday" && (MainSymbol==Event[ii][COUNTRY] || SecondSymbol==Event[ii][COUNTRY]))
return 1;
eTitle[i][n] = Event[ii][TITLE];
eCountry[ii][n] = Event[ii][COUNTRY];
eImpact[ii][n] = Event[ii][IMPACT];
eTime[ii][n] = datetime(MakeDateTime(Event[ii][DATE],Event[ii][TIME]))-TimeGMTOffset();
//Print(eTitle[i]);
}
total=index;
}
datetime tn=TimeCurrent();
for(int qi=0; qi<total; qi++)
{
if(MainSymbol!=eCountry[qi][n] && SecondSymbol!=eCountry[qi][n])
continue;
if(news_High && (eImpact[qi][n]=="High"))
{
if(News_High_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrRed,STYLE_SOLID);
}
if(news_Medium && (eImpact[qi][n]=="Medium"))
{
if(News_Medium_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrOrange,STYLE_SOLID);
}
if(news_Low && (eImpact[qi][n]=="Low"))
{
if(News_Low_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrYellow,STYLE_SOLID);
}
if(news_High && (eImpact[qi][n]=="High") && eTime[qi][n]<(tn+minAfter*60) && eTime[qi][n]>(tn-minBefore*60))
{
return 1;
if(News_High_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrRed,STYLE_SOLID);
}
if(news_Medium && (eImpact[qi][n]=="Medium") && eTime[qi][n]<(tn+minAfter1*60) && eTime[qi][n]>(tn-minBefore1*60))
{
return 1;
if(News_Medium_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrOrange,STYLE_SOLID);
}
if(news_Low && (eImpact[i][n]=="Low") && eTime[i][n]<(tn+minAfter2*60) && eTime[qi][n]>(tn-minBefore2*60))
{
return 1;
if(News_Low_Line)
VLineCreate(0,"Tr_VLine"+TimeToString(eTime[qi][n]),0,eTime[qi][n],clrYellow,STYLE_SOLID);
}
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool isNewsPause(string Symb,int n)
{
bool res=false;
res=(UpcomingNewsImpact(Symb,n)==1);
return res;
}
Upvotes: 0
Views: 1247