MQL5 Tutorial

MQL5 TUTORIAL BASICS – 94 SIMPLE SPREADSHEET FILE WRITER


Listen Later

In this video, we are going to write price data into a spreadsheet like this one right out of MQL5. So let’s find out how to do that. To get started, please click on the little icon here, or press F4 on your keyboard. Now you should see the meta editor window and here you want to click on File, New File, Expert Advisor from template, Continue. I will call this file, SimpleSpreadsheetWriter, click on Continue, Continue and Finish. Now you can delete everything above the OnTick function and the two comment lines here. We start by creating static variables for the last high and the last low. A static variable will remember the value inside of this function for as long as our Expert Advisor is running. Afterwards, we use MqlRates to create a price info array. Because MqlRates is a structure that stores the information about prices, volumes, and spread.   Afterwards, we use ArraySetAsSerries to solve the price info array from the current candle downwards. And now we can fill the array by using CopyRates. For the current symbol on the chart and the currently selected period on that chart, we want to start with candle zero and copy the price data for three candles into our price info array. Now let’s check if we have a new candle, and that would be the case if the value for last high is different than the high value for candle one in our price info array. And we also want to check if the last low value is different than the low value for candle one in our price array.   Now we can start to write our file, and to do that we first need to define a name for the file. I will call this file, spreadsheet.csv, CSV is a file extension that is recognized by most spreadsheet calculation programs like Excel or LibreOffice. I think it stands for Comma Separated Values. Now that we have defined the name we want to open the file. That can be done by using FileOpen. And FileOpen will try to open the file and if it doesn’t exist, it will create one for the name of our spreadsheet that we have defined here, and we need to pass a few parameters, FILE_READ and FILE_WRITE will open the file for read and write access. This is the file mode, it is called FILE_CSV, and we are going to use the ANSI mode to write the file.   Now, when you mark that and press F1, you will learn that we can use ANSI mode or Unicode mode. Unicode would be necessary for special characters like German Umlauts. But as we are going to use simple texts and a few numbers , ANSI is okay, we are going to write a CSV file, but you could also create a text file. And if we have been able to open the file, we now need to go to the end of the file, that can be done by using FileSeek. For our file handle that we have created here, we start with the first entry that’s index zero, and we want to seek the end of the file, seek end will do that and will take us to the last character in the file. I want to do that because I want to append the values and not delete the former content of the file. And that can be done by using FILE_WRITE.   For the spreadsheet file handle, we are using commas. I’m going to write a descriptive text followed by the value for the candle one in our price info array. And the first one is the time. This will create a time stamp, followed by the description high for the high price of candle one in our price info array, and afterwards the low for candle one, you could append further values if you are interested. For example, for the open and the close price. After we have written the content, we want to use FileClose to close our spreadsheet until the next candle comes up. And to find out if the candle has changed, we are now going to assign the high price for candle one in our price info array to the variable last high, and the low price for candle one in our price info array to the last low, because the current values for candle one will be the last values the next time this function is called.   Let’s add a comment statement here for the last high and the last low....
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer