Well, I was right. Northeastern Texas has hit the three-digit temps quite a few days now. I am wishing for the rainy 70s and 80s of Northern Indiana. I guess it would probably kill an ordinary guy. 😉 Thank the Lord for air conditioners. The Texas lake country is still beautiful.
In Part 55 we wrapped up the discussion of our generic start code. Keep in mind that you can add whatever functions you want to your generic start code. Make it your very own. With this installment I want to keep my promise to discuss reading and writing files using MQL4.
MQL4 can read and write files in binary or CSV modes. As many of you may already know, CSV stands for Comma-Separated Values. CSV formatting is often used to transfer data from one format or application to another. CSV can be read in a text editor (like Windows Notepad) and is commonly used to move data into Excel spreadsheets or database-driven applications. I use the CSV format in MQL4 when doing just that – transferring data to a spreadsheet. I will also use CSV for moving data into MQL4 if I’m using an outside application to control my EA for some reason.
For most other purposes, I use binary files. Primarily because it requires less data formatting prior to saving. I also believe that it is faster and requires less processor/hard drive activity (I don’t know that for sure, so don’t take me to task on it), although that may be trivial unless we’re reading/writing our files on every market tick for 24 pairs or something – something I would definitely attempt to avoid.
In addition to transferring data, another purpose for saving data in files is to be sure the data survives a chart time-frame change (which initializes the EA), EA shutdown or a system crash. That is the purpose we will address here. You can save data as Global Variables (GVs) or in the form of data files. I prefer data files because they are more transparent to the end user. If you want to allow the end user of your EA to control the EA using the GVs, you definitely don’t want to clog the GV list with a bunch of “rescue” variables. So I recommend using a data file for “rescuing” your data.
I write functions called ReadFile() and WriteFile() to handle the creation, writing and reading of the files. You can follow my outline for these functions, but you know they will be specific to your application and the data you need to store. I use a couple of global variables (not GVs) to control and check these functions.
string DataFileName = StringConcatenate(Prefix,Symbol(),"_DataFile.bin"); int DataFileVersion = 1;
The string variable DataFileName will be the name of the file on your computer. It will be written to the %MT4%/Experts/Files folder. You will have to include the file extension. It doesn’t have to be .bin, but I like to try to conform to Windows naming standards.
The integer variable DataFileVersion is a variable I create containing the version of your data file. If you are anything like me, your programs will change frequently and will be distributed on several platforms. Those changes can easily include changes to your data files. Using an old data file with a modified EA can create a real problem, so I change the Data File Version number whenever a change is made to the data file. I then start by writing this number at the beginning of the file. When I read the file, if the version numbers don’t agree, the file will automatically be overwritten with the new file format. That’s just a time-saver I like to use so I don’t have to copy the data files with the EA. Remember what I said about automating things. I was serious about that.
That’s it for today. Next time I’ll give you the code for ReadFile() and WriteFile() and I’ll discuss each function. Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- Trend Analysis in Forex- It depends on how you trade… - August 21, 2017
- Forex Entry Methods- Where and How - August 19, 2017
- Find Opportunity in Forex, a Complete Guide - August 18, 2017
Winner’s Edge Trading, as seen on: