RAQ

How To Read and Write to a File in MQL

 

Q: Can I Read and Write a File in MQL?

A:

Yes. MQL provides a suite of functions for reading and writing files.  The functions behave very much like the file IO functions offered in the C programming language.  There are nuances with each of the functions that would be hard for a non-programmer to learn.  Although the functions are provided in VTS (in the Functions Toolbox under Advanced->File), mostly for completeness, I would not expect the average trader to be able to use them.

A few traders have asked, so I created a VTS System name “FileReadWriteTest” that uses the MQL File functions. Actually, I created some new platform functions that use the MQL file funtions.  I had to make many decisions when creating the system, for example: Text or Binary (Text); CSV or not (CSV, which means comma separated values), and when a file is read, it reads the last line only.

If I ever a plug-in for reading and writing files, I would create a user interface to allow the user to decide the format of the file, etc. For now, I chose the most likely useful options.

The purpose of the VTS Systems is to show you how the functions can be used. You can vew the MQL. And to see how and when the files are written on your MT platform.

By the way, MT only allows files to be read and written in the MT platform folder “MQL\Files“.

Additinally, you can add the funcrions you see on the drawings to your Favorites menu and use the same functions in your own drawings.

There are 3 functions in the system. I’ll provide the MQL programming code here in case it helps somebody.

fnSetFileValue adds a string to another string and puts a comma between the two values.

string fnSetFileValue(string line, string value)
{
return( StringConcatenate(line, “,”, value ));
}

fnFileWriteLine writes a line to a file.

int fnFileWriteLine(string filename, string line )
{
int fp = FileOpen( filename, FILE_WRITE|FILE_READ );

if( fp != INVALID_HANDLE )
{
FileSeek( fp, 0, SEEK_END );
FileWriteString( fp, line );
FileClose(fp);
}
return(fp);
}

fnReadFileValue reads a string from a file. You can specify the line and column. If you do not specify, the entire last line of the file is returned.

string fnReadFileValue(string filename, int line=-1, int column=-1)
{
string str = “”;
int count = 1;
string sep = “,”;
ushort u_sep = StringGetCharacter( sep, 0 );

int fp = FileOpen( filename, FILE_READ );
if( fp != INVALID_HANDLE )
{
FileSeek(fp, 0, SEEK_SET );
while( !FileIsEnding(fp))
{
str = FileReadString( fp, 0);
if( (line > 0) && (line == count++) )
break;
}
FileClose( fp );
}

if( column >= 0 )
{
string values[];
StringSplit(str, u_sep, values );

if( column < ArraySize(values) )
str = values[column];
}
return str;
}

Don’t get scared! I just provide the MQL for the few traders that may be interested.

As you can see, the functions all have parameters, so you may be able to use these functions as-is in your VTS system, or even use FileReadWriteTest as-is.

SPECIAL INSTRUCTIONS

You need to manually add the new platform functions to your computer in order to build and run the FileReadWriteTest system.
Just download the PlatformFiles.Zip file seen below, unzip it and add the 3 files to this folder on your PC:

C:\Program Files (x86)\iExpertAdvisor\Visual Trader Studio Connect\elements\platform\MT4\functions

Then download and open the VTS ZIP file (also below) like any other. The system should build a runnable EA. When you run the EA, a file named “mytestfile.txt” is created in your MQL\Files folder. On My PC, the folder is:

C:\Program Files (x86)\FXCM MetaTrader 4\MQL4\Files

Warning: Don’t run the EA for long. The file will get large.

Click to download:  PlatformFiles

Click to download: VtsPackageFileReadWriteTest

For info on opening a ZIP file in VTS  click here

 

 

To get notified about the latest questions and answers, follow us!