mql5 stops writing data (csv) when i open csv file written in mql5 with excel ,how can mql5 continue write to csv file when i open it with excel or another program ??
By controlling the shared access of the files — Documentation on MQL5: File Functions / FileOpen
open_flags
[in] combination of flags determining the operation mode for the file. The flags are defined as follows:
FILE_READ file is opened for reading
FILE_WRITE file is opened for writing
FILE_BIN binary read-write mode (no conversion from a string and to a string)
FILE_CSV file of csv type (all recorded items are converted to the strings of unicode or ansi type, and are separated by a delimiter)
FILE_TXT a simple text file (the same as csv, but the delimiter is not taken into account)
FILE_ANSI lines of ANSI type (single-byte symbols)
FILE_UNICODE lines of UNICODE type (double-byte characters)
FILE_SHARE_READ shared reading from several programs
FILE_SHARE_WRITE shared writing from several programs
FILE_COMMON location of the file in a shared folder for all client terminals \Terminal\Common\Files
FILE_SHARE_READ
128
Shared access for reading from several programs. Flag is used in FileOpen(), but it does not replace the necessity to indicate FILE_WRITE and/or the FILE_READ flag when opening a file.
FILE_SHARE_WRITE
256
Shared access for writing from several programs. Flag is used in FileOpen(), but it does not replace the necessity to indicate FILE_WRITE and/or the FILE_READ flag when opening a file.
- www.mql5.com
Then you have not set your shared attributes properly or you are not maintaining the file open between updates.
If you close the file during the time that the other application open that file, then the other application becomes the master and takes control of it.
Show your code if you need further guidance.
EDIT: Keep the file open, and only close it when the MQL program terminates, but remember to always flush the data so that the other application can see the updates.
You read the file with Excel. MT5 will continue writing to the file, but Excel is only processing the file as it was.
You are expecting Excel to automatically reread the updated file. It won't.
If as William stated, Excel is unable to see updates, then open the file with VSCode for example, that does track and show updates to file data.
//+------------------------------------------------------------------+ //| csv.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ int index=0; void OnTick() { double equity=AccountInfoDouble(ACCOUNT_EQUITY) string mySpreadsheet = "Spreadsheet2.csv"; int mySpreadsheetHandle = FileOpen(mySpreadsheet,FILE_READ|FILE_SHARE_READ|FILE_WRITE|FILE_ANSI|FILE_CSV,";"); if(mySpreadsheetHandle<1) { Alert("File opening error"); } else { FileSeek(mySpreadsheetHandle,0,SEEK_SET); FileWrite(mySpreadsheetHandle,"index","Value"); FileSeek(mySpreadsheetHandle,0,SEEK_END); FileWrite(mySpreadsheetHandle,index,equity); index += 1; FileFlush(mySpreadsheetHandle); FileClose(mySpreadsheetHandle); } } //+------------------------------------------------------------------+When I open it with notepad or vscode, there is no problem, I guess excel locks the file while opening it, so mql5 cannot open the file, the problem is in excel
- 2023.01.28
- www.mql5.com
If as William stated, Excel is unable to see updates, then open the file with VSCode for example, that does track and show updates to file data.
As I have already stated, DON'T imediately close the file on the MQL side. Keep it open and only close it only when the EA terminates, but keep flushing after updates.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use