Overwrite .xml file periodically

 

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.

Here is my code :

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? 

 

anuj71:

Your code should start by checking if the file already exists and deleting it...then everything else.

if(FileIsExist(file_name))
     {
      FileDelete(file_name);
     }
 
Miguel Angel Vico Alba #:

Your code should start by checking if the file already exists and deleting it...then everything else.

Right but even this code will be checked when installing EA to new currency pair chart. I want automatic updating function. So, it will keep updating after X period of time (For example, every 2 hours). So, every two hours it download again from https://www.mql5.com/go?link=http://nfs.faireconomy.media/ff_calendar_thisweek.xml and save it by overwriting existing file
 
anuj71 #:
Right but even this code will be checked when installing EA to new currency pair chart. I want automatic updating function. So, it will keep updating after X period of time (For example, every 2 hours). So, every two hours it download again from https://www.mql5.com/go?link=http://nfs.faireconomy.media/ff_calendar_thisweek.xml and save it by overwriting existing file

Your questions make me think that you don't know how to code and are simply looking for someone to do the work for you.

Sorry, I like to help, but I don't do the work for free.

Be patient, maybe someone will help you with this (because really the solution is simple for anyone with a minimum of knowledge about MQL).

https://www.mql5.com/en/forum/443428

  • Generally, people who cannot code do not get free help on this forum, although it might happen if you are lucky. Be patient.
  • If you show your attempts and describe your problem clearly, you will probably get a response from the community.
  • If you don't want to learn to code, that's not a problem. You can look at the CodeBase section where you will find free and open source code, or at the Market for paid products (also sometimes free). Finally, you also have the option of hiring a programmer in the Freelance section.
Forum rules and recommendations
Forum rules and recommendations
  • 2023.03.12
  • www.mql5.com
Each language has its own forum, and it is therefore forbidden to communicate in any language other than that of the forum in question...
 
Miguel Angel Vico Alba #:

our questions make me think that you don't know how to code and are simply looking for someone to do the work for you.

Sorry, I like to help, but I don't do the work for free.

Your Partially correct, I started coding just 3 weeks ago, i am in learning phase. But i am not looking for someone to do work for free.


I am looking for help, What kind of function i can use which update and overwrite file every 2 hours. I tried like i mentioned above but it not overwriting. Anyways, Thanks for helping :)

 
anuj71 #:

Your Partially correct, I started coding just 3 weeks ago, i am in learning phase. But i am not looking for someone to do work for free.


I am looking for help, What kind of function i can use which update and overwrite file every 2 hours. I tried like i mentioned above but it not overwriting. Anyways, Thanks for helping :)

My advice is to study how the temporal structure works.

Once you understand that you will be able to do anything related to time/dates.

https://book.mql4.com/functions/datetime

Date and Time - Standard Functions - MQL4 Tutorial
Date and Time - Standard Functions - MQL4 Tutorial
  • book.mql4.com
Date and Time - Standard Functions - MQL4 Tutorial
 
Miguel Angel Vico Alba #:
Once you understand that you will be able to do anything related to time/dates.
Checking :)
Reason: