Download MetaTrader 5

Question about FILE_SHARE_READ / FILE_SHARE_WRITE (FileOpen)

To add comments, please log in or register
Felix
109
Felix  

Hi, I am just programming an EA and I have been reading the documentation I found: https://docs.mql4.com/files/fileopen

As far I know (I am not an experienced programmer), FILE_SHARE_WRITE (or share read) must be used when a file is accessed from multiple programs, right?

My EA automatically exports the OHLC information from the last closed candle to a txt file. Then, another different program reads that txt file. So I think I must use share_read and share_write instead of FILE_READ or FILE_WRITE, right?

Do I need to modify the code of the external program (written in Visual Basic Net) to access the txt file or if I modify the code of the EA using FILE_SHARE_WRITE OR FILE_SHARE_READ is enough? 


The idea is 'OnBarOpen' (in EA) the EA writes the OHLC values to a text file. The VB programs need as soon as possible the information written in that text file to perform several actions. Until this moment, I coded VB program to access the text file each n*5+1 minutes. I am working on M5 time period in MT4, so the txt file gets updated at 5, 10, 15, 20, 25, 30... minutes and the VB program access at 6, 11, 16, 21, 31... minutes to the text file, but I am spending almost a minute. I mean, I think the VB program can access the txt file (only to read the values) sooner than 1 minute after each 5-minute multiples.


What do you recommend to do in this case?

Thanks.

FileOpen - File Functions - MQL4 Reference
FileOpen - File Functions - MQL4 Reference
  • docs.mql4.com
[in]  The name of the file can contain subfolders. If the file is opened for writing, these subfolders will be created if there are no such ones. [in]  value to be used as a separator in txt or csv-file. If the csv-file delimiter is not specified, the default delimiter is ";". If the txt-file delimiter is not specified, then no separator is...
Demos Stogios
326
Demos Stogios  
Felix:

My EA automatically exports the OHLC information from the last closed candle to a txt file. Then, another different program reads that txt file. So I think I must use share_read and share_write instead of FILE_READ or FILE_WRITE, right?

Do I need to modify the code of the external program (written in Visual Basic Net) to access the txt file or if I modify the code of the EA using FILE_SHARE_WRITE OR FILE_SHARE_READ is enough? 


The idea is 'OnBarOpen' (in EA) the EA writes the OHLC values to a text file. The VB programs need as soon as possible the information written in that text file to perform several actions. Until this moment, I coded VB program to access the text file each n*5+1 minutes. I am working on M5 time period in MT4, so the txt file gets updated at 5, 10, 15, 20, 25, 30... minutes and the VB program access at 6, 11, 16, 21, 31... minutes to the text file, but I am spending almost a minute. I mean, I think the VB program can access the txt file (only to read the values) sooner than 1 minute after each 5-minute multiples.


Hello,

Be careful, one should declare both FILE_READ and FILE_SHARE_READ (for WRITE as well),

Concerning the VB stuff, I do not know about VB, but I guess if the file is first opened from Meta Trader there should be no problem,

As for the timing, I am not sure what you mean, are you looking for ways to sync both programs?

Felix
109
Felix  
Demos Stogios:

Hello,

Be careful, one should declare both FILE_READ and FILE_SHARE_READ (for WRITE as well),

Concerning the VB stuff, I do not know about VB, but I guess if the file is first opened from Meta Trader there should be no problem,

As for the timing, I am not sure what you mean, are you looking for ways to sync both programs?

So, do you mean that I need to declare both FILE_SHARE_READ and FILE_SHARE_WRITE? Why? It happens the same if I declare FILE_READ? I mean if I need to declare read and write when using FILE_READ instead of FILE_SHARE_READ. Thanks.

Demos Stogios
326
Demos Stogios  
Felix:

So, do you mean that I need to declare both FILE_SHARE_READ and FILE_SHARE_WRITE? Why? It happens the same if I declare FILE_READ? I mean if I need to declare read and write when using FILE_READ instead of FILE_SHARE_READ. Thanks.

Well, if e.g. you wanted to share read from a file, you can not specify FILE_SHARE_READ alone, but you have to include FILE_READ as well. That means the line should be FILE_READ|FILE_SHARE_READ. Same goes for share write.

Felix
109
Felix  
Demos Stogios:

Well, if e.g. you wanted to share read from a file, you can not specify FILE_SHARE_READ alone, but you have to include FILE_READ as well. That means the line should be FILE_READ|FILE_SHARE_READ. Same goes for share write.

Oh, I better understand right now.

You mean that FILE_SHARE_READ never should be alone. So the correct way is FILE_READ|FILE_SHARE_READ, right?

Demos Stogios
326
Demos Stogios  
Felix:

Oh, I better understand right now.

You mean that FILE_SHARE_READ never should be alone. So the correct way is FILE_READ|FILE_SHARE_READ, right?

yep, this is it. I reckon, the documentation may not be very clear at times, especially if someone is reading in a hurry, but this is MetaQuotes' way :) For the record here are the docs https://www.mql5.com/en/docs/constants/io_constants/fileflags

Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Standard Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
CSV file (all its elements are converted to strings of the appropriate type, Unicode or ANSI, and separated by separator). Flag is used in FileOpen(). 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. Shared...
To add comments, please log in or register