[MQL4 Coding Help] Overwrite .xml file periodically

shanmugapradeep

Active Trader
Dec 18, 2020
112
5
34
38
Hello,

I am fetching news from http://nfs.faireconomy.media/ff_calendar_thisweek.xml and it save the file inside terminal/MQL4\Files as FFCPing(Symbol)-ffcal_week_this.xml (eg, FFCPingGBPUSD-ffcal_week_this.xml). I have added a code to check this file every 2 hours and overwrite it to new version.


MQL4:
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()-(2*3600))
        {
         Print(INAME+": xml file is out of date");
         xmlUpdate();
        }
      //--- set timer to update old xml file every x hours
 
      EventSetTimer(2*3600);
     }
   assignVal=true;
  return(INIT_SUCCEEDED);
}
 
void OnTimer()
  {
//---
   assignVal=true;
   Print(INAME+": xml file is out of date");
   xmlUpdate();
//---
  }
 
 
void xmlDownload()
  {
   Sleep(3000);
//---
   ResetLastError();
 
 
   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());
     }
//---
  }



Error : It not overwriting automatically. I have to delete the file manually and than this code create new .xml file. How to fix this?. How to make this overwrite existing file automatically?
 
Last edited by a moderator:

shanmugapradeep

Active Trader
Dec 18, 2020
112
5
34
38
You are actually trying to update the file only if it doesn't exist, which is an error I think. This line (when you use it the second time) should be fixed:

Should i change like this?


MQL4:
if((!FileIsExist(xmlFileName)) || (FileIsExist(xmlFileName)))
     {
      if(xmlModifed<TimeLocal()-(2*3600))
        {
         Print(INAME+": xml file is out of date");
         xmlUpdate();
        }



Here is my XMLUpdate code.

MQL4:
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);
     }
//---
  }


want Every 2 hours :
  1. Delete old XML file
  2. Create new XML file
  3. Read new XML file
I am getting :
  1. Download XML file when installation of EA on chart
  2. Read XML file
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
18,709
1,386
144
Odesa
www.earnforex.com
Should i change like this?


MQL4:
if((!FileIsExist(xmlFileName)) || (FileIsExist(xmlFileName)))
{
if(xmlModifed<TimeLocal()-(2*3600))
{
Print(INAME+": xml file is out of date");
xmlUpdate();
}
No, you should have just removed the exclamation mark (!) in front of that condition and that's it:
MQL4:
if(FileIsExist(xmlFileName))
If you keep the exclamation mark, it means that the stuff inside the condition's parentheses runs only if the file doesn't exist. Without the exclamation mark, the stuff runs only if the file exists. This what you need when you are updating an existing file.
 

shanmugapradeep

Active Trader
Dec 18, 2020
112
5
34
38
If you keep the exclamation mark, it means that the stuff inside the condition's parentheses runs only if the file doesn't exist. Without the exclamation mark, the stuff runs only if the file exists. This what you need when you are updating an existing file.

Like this?

Code:
if(FileIsExist(xmlFileName))
{
if(xmlModifed
	



And One more questions,

1705499810482.png
is it okay to have else without { } ?
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,709
1,386
144
Odesa
www.earnforex.com
  • 👍
Reactions: shanmugapradeep