In this video we are going to create an expert advisor that is able to write values for the high and the low price of the last candle into a spreadsheet. So let’s find out how to do that with MQL4.

To get started, please click on the little icon here or press F4 on your keyboard. Now you should see the MetaEditor window, and here you want to click on File, New File, Expert Advisor from template, Continue. I will call this file Simple Spreadsheet Writer. Click on Continue, Continue, and Finish. Now you can delete everything above the OnTick function and the two comment lines here.

Static means that a variable will remember the value inside of this function for as long as the program is running. I need two variables, one for the last high price and one for the last low price, because that makes it possible to compare if the value for the last high is equal to the high price for candle one, and if the last low price is not the one that we see for candle one now, we know that we have a new candle, and that’s when we want to write the values into our file.

We need to define a file name. In my case, I used the file name Spreadsheet.CSV. This file extension here is recognized by most spreadsheet solutions like Excel or LibreOffice. I think it stands for Comma Separated Values.

And now that we have the file name, we try to open the file. That can be done by using File Open.

For our spreadsheet, here we want to use File Read and File Write. The mode for the file is File_CSV, and we are going to use File ANSI, so let’s mark that and press F1. And you will see that we can use Unicode or ANSI code. Unicode would be used for special characters, but we are only going to use values and simple text.

I don’t want to replace the current content in the file, so I use FileSeek for our file handle here. I start with the index zero, and I want to go to the end of the file, so I use SeekEnd for the file end. And now we can append the content. To do that we use File Write for our spreadsheet handle, and each value is separated with a comma here. This is text. This is the value for the time stamp. Time is a value in seconds since the 1st of January in 1970. This is for candle one, and I would also like to append the text High and the high price for candle one, followed by the text Low and the low price for candle one. After we have written, we need to close the file. That is done by using File Close for our spreadsheet handle.

And finally, we are going to assign the current value for the high price and the low price of candle one to the variables Last High and Last Low, because the next time we call the function, the current values will be the last ones. Now let’s close the IF loop here. I would like to add a comment statement to output the values on the chart, and that’s about it.

If this was too fast for you, or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic video series, or maybe even the premium course on our website might be interesting for you. For now, please click on the Compile button or press F7 on your keyboard. We don’t have any errors here, and if that is the case, you can click on a little button here or press F4 to go back to MetaTrader.

In MetaTrader you want to click on View, Strategy Tester, or press control and R. Please pick the new file Simple Spreadsheet Writer.ex4. Mark the option for the visual mode and start your test. Here we see different values. So let’s find out if we have created the spreadsheet file.

So please click on File, Open Data Folder. This will show me all the folders below the Terminal folder here. I need to click on Tester, Files, and here is my Spreadsheet.CSV file. Let’s double click it. Here is the preview. I click on Okay, and indeed the spreadsheet file is working. So now you could append other values here. All you would need to do is to change this one line here.

And in this little video you have learned how to create an expert advisor that is able to write values into a spreadsheet, and you have coded it with a few lines of MQL4 code.