How to read txt or csv files

 

I am trying to read a txt file and am not sure what the best way to do this in mql4 is. (I am not very familiar with mql4)

In mql5 I use the following method to read the txt file and use that data as a string variable called NewsFile within my program: 

#resource "HighImpactNews.txt" as string NewsFile

But in mql4 it seems that .txt files are not allowed as resources. I get the error: "unknown resource type" when compiling. 

Does someone know what the best method of reading a txt file and storing the data within it in a string type variable in mql4 is? 

 
altmql:

I am trying to read a txt file and am not sure what the best way to do this in mql4 is. (I am not very familiar with mql4)

In mql5 I use the following method to read the txt file and use that data as a string variable called NewsFile within my program: 

But in mql4 it seems that .txt files are not allowed as resources. I get the error: "unknown resource type" when compiling. 

Does someone know what the best method of reading a txt file and storing the data within it in a string type variable in mql4 is? 

Using a resource for news is maybe not exactly what you should use.

Resources are added at compile time and will not update ever again, until you recompile.

You should use functions to read the content. FileOpen (), FileRead (), FileClose ().

This will also work in MT4.
 
Dominik Christian Egert #:
Using a resource for news is maybe not exactly what you should use.

Resources are added at compile time and will not update ever again, until you recompile.

You should use functions to read the content. FileOpen (), FileRead (), FileClose ().

This will also work in MT4.

Thanks for your reply. I am using this function only to backtest. I have a news filter that uses webrequest to get future data. I want to include the txt file in order to backtest a strategy that only operates around high impact news releases. And I want the txt file to be included in the compiled ex4 file, without the need of the user to store the txt file on their computer. 

 
altmql #:

Thanks for your reply. I am using this function only to backtest. I have a news filter that uses webrequest to get future data. I want to include the txt file in order to backtest a strategy that only operates around high impact news releases. And I want the txt file to be included in the compiled ex4 file, without the need of the user to store the txt file on their computer. 

then you need to hardcode it into mq4.

Mq4 does not support that type of loading.
 
Dominik Christian Egert #:
then you need to hardcode it into mq4.

Mq4 does not support that type of loading.

Ok, will try to do that. One more question: If I write the entire file with the date and time of high impact news as a string, what will be the best way to write this without having to write it all in one line. The format of the news release data is: 

2022.11.24,15:30

2022.11.25,12:00

2022.11.25,14:00

So the date and time is separated by a ",", and each news event is on its own line.


As far as I know, a string variable has to be defined in a single line of text. However, It will be pretty bothersome if I have to write it all in one line, and separate the events with a delimiter, like ";" or "|". For example, like this: 

string NewsData = "2022.11.24,15:30|2022.11.25,12:00|2022.11.25,14:00";

The txt file with news events is pretty large, so writing all the events in a single line will be very messy. 


If you have any suggestions how to create the string type variable so that I can keep each line in its own row would be much appreciated. 


Thanks again for your help!

 
altmql #:

Ok, will try to do that. One more question: If I write the entire file with the date and time of high impact news as a string, what will be the best way to write this without having to write it all in one line. The format of the news release data is: 

2022.11.24,15:30

2022.11.25,12:00

2022.11.25,14:00

So the date and time is separated by a ",", and each news event is on its own line.


As far as I know, a string variable has to be defined in a single line of text. However, It will be pretty bothersome if I have to write it all in one line, and separate the events with a delimiter, like ";" or "|". For example, like this: 

string NewsData = "2022.11.24,15:30|2022.11.25,12:00|2022.11.25,14:00";

The txt file with news events is pretty large, so writing all the events in a single line will be very messy. 


If you have any suggestions how to create the string type variable so that I can keep each line in its own row would be much appreciated. 


Thanks again for your help!



You have multiple options.

string var = "" 
+ ""
+ "";


string var = "";

var += "";
var += "";
var += "";


Just make sure you also include your line breaks like this:


var+= "Line 1\n";
var+= "Line 2\n";
And don't change the format of your original text file.

For fast editing, use NP++ or some other editor that can do column-insertions.

 
Dominik Christian Egert #:


You have multiple options.


Just make sure you also include your line breaks like this:


And don't change the format of your original text file.

For fast editing, use NP++ or some other editor that can do column-insertions.

Thank you so much!! 

 
altmql #:

Ok, will try to do that. One more question: If I write the entire file with the date and time of high impact news as a string, what will be the best way to write this without having to write it all in one line. The format of the news release data is:

You can use the built-in feature of MetaEditor to insert your file as an array of strings.


 
Dominik Christian Egert #:


You have multiple options.



Or simply (no need for a + ) :

string data = "2022.11.24,15:30..."
              "2022.11.25,12:00..."
              "2022.11.25,14:00...";
 
Alain Verleyen #:

Or simply (no need for a + ) :

Yes, I just gave 2 examples... But that Insert function from ME, I didn't know about. Always learning.
Reason: