Force to write a file MQL4

 

Hello everybody,

I am looking for a solution of my problem. I am trying to write a file in MQL4. There is no problem, but the problem appears when I try to force to do it constantly.

I mean, with an expert advisor I am writing the file with the follow code:

 

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

int handle;
datetime lastupdate;

int init() {
   lastupdate=Time[0];
   return;
}
int deinit(bool handle) {return(0);}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start(){

   string fileName = Symbol()+Period()+".csv";
   
   if(lastupdate != Time[0])  {
      int fileHandle = FileOpen(fileName,FILE_CSV|FILE_SHARE_READ|FILE_SHARE_WRITE);
   
      FileSeek(fileHandle, 0, SEEK_END);
      string data = "";
      
      for (int i = OrdersTotal() - 1; i >= 0; i--){
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            data += OrderOpenTime() + ";";
            data += OrderSymbol() + ";";
            data += OrderType() + ";";
            data += OrderLots() + ";";
            data += "MT4";
         }
      }
      Alert("Added!");
      FileWrite(fileHandle,data);
      FileClose(fileHandle);
      
      lastupdate = Time[0];
   }    
   return(0);
}
//+----------------------------------------------------

 The file is created and everything is going well. What happen if I try to see into the file with another program (like Excel or notepad++)? The file is not writing anymore until I restart the EA into the platform. How can I force to the MT4 to write the file and simultanously also see the results in real time in Notepad++ or Excel?

 

Thank you in advance,

Dream3r 

 
dream3r:

Hello everybody,

I am looking for a solution of my problem. I am trying to write a file in MQL4. There is no problem, but the problem appears when I try to force to do it constantly.

I mean, with an expert advisor I am writing the file with the follow code:

 

 The file is created and everything is going well. What happen if I try to see into the file with another program (like Excel or notepad++)? The file is not writing anymore until I restart the EA into the platform. How can I force to the MT4 to write the file and simultanously also see the results in real time in Notepad++ or Excel?

 

Thank you in advance,

Dream3r 

In general, file can be accessed by one program only for safety (there are few exceptions.). What happen now is that  your two programs (MT4 and Excel) is fighting to access one file. So this is why MT4 can't write it any more.

To tackle this you need very careful handling of file from your Excel VBA side so they won't crash to each other. If you can make both program to access the file in turn, this would be the best solution.

If you can't then if you have to force it EXCEL VBA to release file after finishing the job so MT4 can write.

Regards.

 
dream3r:

Hello everybody,

I am looking for a solution of my problem. I am trying to write a file in MQL4. There is no problem, but the problem appears when I try to force to do it constantly.

I mean, with an expert advisor I am writing the file with the follow code:

 

 The file is created and everything is going well. What happen if I try to see into the file with another program (like Excel or notepad++)? The file is not writing anymore until I restart the EA into the platform. How can I force to the MT4 to write the file and simultanously also see the results in real time in Notepad++ or Excel?

 

Thank you in advance,

Dream3r 

You have to give the right operation mode for the file :

      int fileHandle = FileOpen(fileName,FILE_CSV|FILE_READ|FILE_WRITE|FILE_SHARE_READ);
  • Allow to read/write (append) to your file from mql4/mql5.
  • Allow you to read the same file outside MT4/MT5.
  • If you also need to write from Excel or Notepad, then add FILLE_SHARE_WRITE.
 
angevoyageur:

You have to give the right operation mode for the file :

  • Allow to read/write (append) to your file from mql4/mql5.
  • Allow you to read the same file outside MT4/MT5.
  • If you also need to write from Excel or Notepad, then add FILLE_SHARE_WRITE.
Thank you so much!
Reason: