We complete this critical lesson with some simple examples to apply the previous concepts we learned earlier.
Example of opening a read-only text file
In this case, the file must be located on the disk.
If the file does not exist, an error will occur, and we will not get the number or handle of this file.
Note that the FileOpen function requires three variables:
Print("File not found, the last error is ", GetLastError());
- The first variable is the name of the file that we want to open, and we are free to choose the name and extension that we want, and special conditions do not restrict us.
- The second variable is the open file's properties; is it read-only, read and write, or write-only.
We define this with the FILE_READ - FILE_WRIT variables. And merging between them by letter |
We can determine the type of the open file from this same variable, whether it is text or binary.
The text file we know as FILE_CSV and the binary FILE_BIN. The two cases cannot be combined, meaning we must choose only one case.
In our previous example, we defined the file type as text and read-only.
- The third argument is the default separator between the information written on the file, and here is the letter; The semicolon.
Any other letter can be specified, but it is preferable to leave it because it is standard for other programs such as Excel.
An example of how to write text information to a text file
This example shows how to write some of the information we want into a text file with the FileWrite function.
In the previous example, we opened the data.csv text file for writing only. In this case, if the file does not already exist, this function will always create it.
It will delete the old file and create a new file every time it performs this function.
Next, we used the FileWrite function to write an entire line of information, and here is the last bar information.
If we run the previous code and open the data.csv file, we will find in it only one line containing the last candle information separating between
Every piece of information and another letter.
If we wanted to write all the chart information to the text file, we would use a loop for all the chart candles, and the example would become the figure
next one :
Note that dealing with large files as writing should be done in the script and not through an index.
for(int i =0;i<=Bars;i++)
Because it is unreasonable to write the full chart candlestick information into a text file every time the price moves.