MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 103 SIMPLE BUY GRID SYSTEM
    In this video, we are going to create an Expert Advisor for a simple buy grid system, it will open buy trades every 50 points. So let’s find out how to do that with MQL5 .To get started, please click on a 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 filem, Expert Advisor from Template, continue. I will call this file simple buy grid system. Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start with an include statement to include the fire Trade.mqh. This is part of MQL5, and now we can create an instance of the class Ctrade that is called trade. We also want to create an array for the price info data that is done by using MQLRates. And we want to declare a global variable for the signal. This is a string variable, but we don’t assign any values here. Inside of the ontick function we declare a static variable. Static variables will hold their values inside of the function for as long as the Expert Advisor is running. It’s a double variable so it can hold floating types. And the name is next buy price. Now let’s calculate the Ask price, that can be done by using Symbolinfodouble for the current symbol on the chart. We use simple underscore ask and with normalize double and underscore digits, we automatically calculate the right number of digits behind the dot. Let’s continue and use ArraySetAsSeries to sort the price array from the current candle downwards. And now we want to use CopyRates for the current symbol on the chart and the currently selected period on that chart to fill the price array with data starting from candle zero for three candles. Let’s actually use an underscore here. So if we have no open positions, that would be true when PositionsTotal delivers a return value of zero, the next buy price is also zero because we have no level. Now let’s check for the entry signal. We do that by calling a function that is called check entry signal here, we need to code that later on and the return value will be assigned to our signal. And one of the entry conditions is that we only want to buy if the Ask price is above or equal to the next buy price. And if that is true, we also want to check if the signal equals buy. In that case, we want to use trade.Buy to open a buy trade for 10 Microlot and now we set the level for the next buy price 50 points above the current Ask price. Finally, we want to create a comment statement to output the Ask price and the next buy price on our chart. That’s it for the Ontick function. But to use this function, we need to create it now. Our function is called check entry signal. I’m using a very simplified version and check if the last candle is bullish. That is true when the close price is above the open price. And in that case we assign the word buy to our signal. In the other case, if the close price is below the open price, that would mean that the candle is bearish. And now we assign the word sell to our signal. Finally, we return the signal to our main module 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 become a Premium Course member. You can find the Premium Course on our website. This actually was the suggestion of a current Premium Course member. And if you are already a premium member and have an idea for a video like this one, just sent me an email. For now, please click on the Compile button or press F7 on your keyboard. I have an additional bracket here, so let’s remove that. Recompile the code. And this time we have no errors and no warnings, and in that case, you can click on a little icon here or press F4 to go back to Metatrader. And in Metatrader, you want to click on View, Strategy tester or press Control and R, please pick the new file. Simple buy grid system.ex5 mark the option for the visual mode here and start your test.
    7 min
  • MQL5 TUTORIAL BASICS – 102 SIMPLE BUY CROSSOVER STOP LOSS
    In this video, we are going to open some test positions, buy positions, and those positions will be closed when the crossover of these two moving averages happens. So let’s find out how to do that with MQL5. 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. I will call this file simple by cross over Stop Loss.   Click on Continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start by including the file Trade.mqh and we want to create an instance of the class Ctrade that is called trade and will provide some simplified trading functions. We first need to calculate the Ask price. That is done by using Symbolinfodouble for the current symbol on the chart, we want to use symbol, underscore, ask and with Normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Let’s also repeat that for the Bit price. And now we create a string variable for the signal for a crossover.   We need two arrays, one for the small and one for the big moving average.   The small moving average definition is created by using the IMA function for the current symbol on the chart and the currently selected period on that chart, we are going to use twenty candles. No shift value in this case. I’m using ModeUnderscoreSMA for simple moving average.   But you also could use ModeUnderscoreEMA for exponential moving average and the calculation should be done based on the close price. The definition for the big moving average is very similar, but in this case we are using 50 candles. Now we use copybuffer to fill our array for the small and for the big moving average.   According to the two definitions that we have created. We do that for buffer zero from Candle zero. That’s the current candle, and we copy the price data for three candles and store them in the arrays for the small and for the big moving average. And whenever the big moving average array for candle one has a higher value than the small moving average array for candle one, we consider that to be a buy signal. In the other case, if the big moving average array for candle one is bigger than the small moving average array for candle one, that would be a sell signal.   The idea is to close all buy positions for the current currency pair. When we have a sell signal and when the return value for PositionsTotal is bigger than zero. That means we have open buy positions and want to close them, but we need to have something to close. So I’m going to open test positions when the signal equals buy and when we have no open positions, that’s when we are going to use trade.Buy to buy 10 Microlot. In our case, we are using Stop Loss value of 200 points and a Take Profit value of one hundred and fifty points.   Please remember, this is just a test position, you wouldn’t do that on a real account and to see what’s going on, I would like to have a comment statement that will output the text. The current signal is now followed by the signal. Well, this function does not exist, so we need to create it now. The name of the function is close all buy positions this pair. We use void because we don’t require a return value. Inside of the position we are going to use a for loop to count down until there are no positions left.   We need to get the ticket number, that is done by using position get ticket. We want to identify the current currency pair. That can be done by position, get symbol. In both cases we are using the current counter for the current position. Now let’s get the position direction. That is done by using position, get integer and the parameter here is position underscore type. And if the position direction equals position type buy, we have a buy position. Now we want to check if the current symbol on the chart equals the position symbol.
    8 min
  • MQL5 TUTORIAL – 101 WHAT HARDWARE FOR ALGORITHMIC TRADING
    In this video, I would like to talk about a question that I get from time to time, and that is the question what kind of hardware can I use to work with Metatrader4 or Metatrader5? So let’s go through all the different solutions that I use. This system here currently is running on a so-called virtual machine, I’m actually using the same virtual machine right now to record this video. This is the new version for the crowd algo trading system that I’ve created on January the 27th.   And I would like to do a live trading experiment in February.   So I have coded this system on this virtual device. The virtual machine is running on a software that is called Virtualbox. This kind of software can be downloaded from the Internet and it will actually help you to run one operating system as a window inside of a host operating system. In my case, I’m using Linux as my main operating system and Windows is running in a virtual device. If you prefer to not use virtual devices, but real ones, you can use a software like this one.   This is VNC Connection Viewer. I can use it to connect to different PCs. I have a lot of those devices. This one is a Windows eight device. It’s running with two gigabytes of Ram. The Metatrader is running smoothly. I can move the machine around and it’s also possible to remote control everything. This, by the way, is the MQL4 version of the crowd algo trading system, and today we have two thousand two hundred seventy three dollars of gross profit.   If you are interested, you can watch the videos that I have made for this MQL4 version. By the way, the Windows four version is running on such a stick device. They call it computer sticks. Those sticks are a little bit bigger than an average USB stick. They don’t need much room and you can use them on screens or TVs because they come with an HDMI port. A device like this one is around 100 dollars. But for the new system, I decided to buy one of these mini pcs here.   This is a little bit bigger than cigarette box. You see the pen here, the whole thing, including the keyboard would fit into a pocket. And this hardware is a little bit better and it’s newer. We have, I think, four gigabyte of RAM and it’s very affordable. You might be able to find it for around 100 dollars online. The really cool version would be to use devices like these mini notebooks. Maybe you remember that kind of device from about 20 years ago when we all used pocket PCs.   These pocket notebooks have been very popular. But back then they only had Windows Mobile or the Symbian operating system. But these two machines here run with Windows 10 and eight gigabyte of RAM. That is more than enough to run Metatrader. And you can do a lot of other cool stuff. For example, mobile programming. If you don’t want to spend that kind of money, you could run Metatrader on an old netbook. This device is about 10 years old.   It’s running with MX Linux. And when you use software like play on Linux and wine, it’s also able to run Metatrader. Used devices like this one would maybe cost between 50 and 100 dollars. And when I started out to use automated systems, I had over half a dozen of these devices on my desk. But today I really prefer to run Linux on a desktop PC, Virtualbox in the background running Microsoft Windows. And when you are not at home, you could use this mobile version of Metatrader to at least see what’s going on with your account.   I think it’s available for Apple devices, too. This one is from the Android store. You can see all the numbers, but it’s not possible to do automated trading on mobile devices right now. And basically, that’s it. So now you know what kind of hardware you need to run Metatrader. That’s all for today. And thank you for listening.
    6 min
  • MQL5 TUTORIAL BASICS 100 – SIMPLE TRIANGLE OBJECT
    In this video, we are going to create a triangle object on the chart, you see the little yellow triangle here. This is actually an object. So let’s find out how to create it with MQL5. To do that, 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 Triangle, click on Continue, continue and finish. Now you can delete everything above the ontick function and let’s also delete the two comment lines here. We start by creating two variables for the highest and for the lowest candle. Afterwards, we want to create two arrays. Those are called high and low. And now we need to use ArraySetAsSeries to sort out arrays from the current downwards, we do that for the high array and of course, we also need to do it for the low array.   Now we want to use  CopyHigh, because that will give us the highest bar prices for the current symbol on the chart and the currently selected period, starting from Candle zero for 30 candles. And it will copy the values into our high array. And you might have guessed it. Now we can do the same with CopyLow to copy the lowest values into our low array. And once we have those values, we can calculate the highest candle by using ArrayMaximum.   That will give us the largest element in our array. In this case, in the high array, starting from Candle zero for 30 candles and for the Iowest candle, we can use ArrayMinimum, that will look into the low array starting from Candle zero for 30 candles. And this will give us the lowest candle. Afterwards, we use MQLRates to create another array for price information. This one also needs to be sorted downward from the current candle downwards. Now we use CopyRates for the current symbol on the chart and the currently selected period on that chart, starting from Candle zero.   This expression here will give us all the available price information for the bars on the chart and the result will be stored in our price information array. Before we want to create a new object we use object delete for the current symbol on the chart to delete any former objects that might have the name triangle. And after we have done that, we can create a new object, that is done by using object create.   For the current symbol on the Chart ID, the object name will be triangle, the object type is OBJ underscore triangle, all in capital letters please. It will be created in our main window at the point in time where we see candle 30 for the close price from candle thirty. That’s our anchor point. And we draw it to the point in time for the Iowest candle to the lowest price of the lowest candle. And the other side will be the point in time for the highest candle and the highest price for the highest candle.   Remember, this is all for the last 30 candles. Now let’s change a few properties. That is done by using ObjectSetInteger. The first value is for the Chart ID. And zero means that we are going to do it on the current chart for the object that is called Triangle. We want to change the property that is called OBJ Prop_Color, and we want to change it to yellow. Finally, we want to repeat that this time we do it for the property object property fill and use yellow to fill the background. So far, that’s 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 this 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 F7 on your keyboard.   I need to remove this additional bracket here. Now, let’s recompile the code, and this time we have no errors, and if this is true, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, you want to click on View Strategy tester or Control and R, please pick the new file.
    7 min
  • MQL5 TUTORIAL – 99 NEW LIVE CROWD ALGO TRADING SYSTEM
    In this video, I would like to talk about a new system that I have released on January the 1st, 2021. It’s an automated algo trading system. I have also published it on the website, MyFxbook. I also need to mention that this is not an MQL5 system for Metatrader5, but it is an MQL4 system made for Metatrader4.   This is my statistics page on MyFxbook, and as you can see, the system started to grow very quickly. But after a few days, the balance curve started to get flat and therefore I asked myself if it would be a good idea to close all open trades, including the open losses, because I have seen this kind of behavior before. Therefore, I have coded a function to show a message box, when I click on the chart in the upper left corner and it says, Would you like to close all positions?   I will not do that right now. But I did it yesterday.   And after I did that, I have a net and a gross profit of one point five one percent for twelve days. So that translates to 15 percent and one hundred and twenty days and as a year has three hundred and sixty five days, we could expect forty five percent profit. But actually I don’t think that we will do that much. But you can see yourself. I have called this system crowd algo trading system because my company name is Crowd Company and if you take that term and google for it, you should be able to already see a lot of results for it.   For example, this is the YouTube channel. This is yesterday’s video. It’s automated content because I have to do one of those videos each day. Here is the link to the Web page. And if you click on that and click on one of the current posts, you will see the same video content. And below that, we have the download for the Metatrader setup file. This is the download for the demo system that will work on Metaquotes, original demo accounts.   And here is the download for the statistics. When I click that and open the page, I see a timestamp for January the 12th. That’s actually the current day. So far we have made four hundred and fifty dollars profit and I expect this system to be a good choice because actually it is a clone from this system. I have traded this one in 2020 with good results. You’ll see this drawdown here in March. That was the time of the first Lockton here in Germany.   And I have tried other systems that have not performed that well. As I’m happy with the results of this one, I took it as the foundation for the new one. This MyFxbook Expert Advisor will upload the current statement every five minutes. Currently we have January the 12th and when you see this video sometime later, you should still be able to get the current statistics when you click on this link, it works without any credentials. Here is the official system page, currently we have made 59 trades, but this will increase in the future.   And if you’re interested to learn what is working and why it is working, you can analyze each and every of these positions here. So for now, that’s it. If you have any questions or suggestions about the system set up, just send me an email. For now, I will let this system do its job. And I would like to say thank you for listening.
    5 min
  • MQL5 TUTORIAL BASICS 98 – HOW TO CLONE YOUR MQL5 SYSTEM
    In this video, I would like to talk about how to clone an existing Metatrader5 installation and create a portable version that also can run from an USB stick. To do that you first need to have a pre configured installation like I have here. This is on a Windows machine. And when you click on file open data folder, you will see that the setup has created a folder inside of the user’s folder. That’s usually something like this one.   And inside you see several files and directories, but not all of what we need.   But it’s possible to create a portable version. Actually, I have done that by using play on Linux and MX Linux. I have just copied my complete folder. This is a brand new Windows 10 machine. This is my USB stick.   I have created a version for Metatrader4 and this one is the one for Metatrader5. I can now copy the whole directory and paste it anywhere here in the file system without any installation, that might take a few seconds. It’s also moving my history files here. So let’s wait until this is finished. OK, now it is finished. I have no search results for any installation, but inside of this directory I can now click on the terminal64.exe file and you should see that Metatrader is started in the background.   This can take some time at the first start up, but here it is. Now I need to connect my account. So let’s click on Continue. This is my existing real cent account 5245388. So let’s type that in. And I also need to type in my password. Click on Finish. And now I’m connected. Here is my Expert Advisor. Let’s click on file, open data folder. Now it has created the user directory here is the experts folder.   It copied everything, including the zip file. Here is my source code, my .ex5 file and folder for the library. My account stats. Let’s try a strategy test. It’s downloading history data. And here’s the Strategy tester. We have the first trade here. So it’s actually running. And in this little video, you have learned how to create a portable version of your existing Metatrader5 installation, including the Expert Advisor settings. You have done it yourself in a few minutes.   And if you have any questions or suggestions about this kind of setup, please let me know.
    4 min
  • MQL5 TUTORIAL BASICS 97 – SIMPLE REVERSE POSITION
    In this video, we are going to create an Expert Advisor that is able to change the course direction for every trade. So let’s find out how to code that with MQL5. To do that, please click on a little button 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 reverse position. Click on continue, continue and finish.   Now you can remove everything above the Ontick function and the two comment lines here. We start with an include statement to include the file Trade.mqh. This one comes with simplified trading functions for MQL5 to use it, we now need to create an instance of the Class Ctrade.   We will call it trade. Let let’s also create some variables here. The first one is a string variable for the direction, the initial value will be buy for this simple example, the next variable is called take profit points. This one has a modifier that is called input. And this input modifier will make it possible to change the variable settings without the need to recompile the code. We can do it from the Expert Advisor settings. Now let’s create another one.   This one is called Stop Loss Points. We will set this one to thirty points. Inside of the ontick function we need to calculate the ask price, that is done by using symbolinfodouble for the current symbol on the chart. We use symbol underscore ask as we want to calculate the ask price and I like to use normalize double and underscore digits to automatically calculate the right number of digits behind the dot.   Now we will repeat the whole thing for the bid price. This is almost the same, except that we are going to use symbol underscore bit in this case. Now let’s check if we have no open position, that can be done by using positions total and if the return value is zero, we have no open position. And we also want to check if the current direction to trade is set to buy and if both conditions are true, we use trade.Buy to buy 10 microlot, for the current symbol.   Let’s actually say that. We want to buy for the current ask price. The next expression here is the stop loss. That will be set to the value that we have defined here, followed by the parameters for the take profit. That’s the value here. And the last variable here could be a comment. We don’t need that. So we sell it to Null. And as we want the next trade to go into the other direction, we will now change the direction to sell.   That’s it for the buying part, the selling part is very similar. First, we need to check if we have any open positions and if we don’t have, we want to check if the direction is set to sell and if both conditions are true, we use trade.Sell to sell 10 microlot for the current symbol on the chart. To sell, we need to use the bid price and of course, when we sell, we need to set the stop loss points to a value above the current price, and the take profit value will be below the current price.   Afterwards, we change the direction to buy.   Now, you might ask yourself, why would anybody do something like this? Well, it’s a good idea to test other components in your system. And actually, if you do this exercise and change the values for take profit and stop loss, you will probably learn something that you didn’t expect. I would like to see the current direction. So I’m going to add a comment statement here that will output the text: “the current direction is”, followed by the direction.   OK, 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 our basic video series or maybe even the Premium Course on our website might be interesting for you. Actually we currently also have the starter course on the website for just one dollar. But for now, please click on the Compile button or press F7 on your keyboard, you shouldn’t get any errors here. And if that is the case,
    7 min
  • MQL5 TUTORIAL BASICS – 96 SIMPLE MOVING TEXT OBJECT
    In this video, we are going to create such a moving text object, it will output the current low price for the current candle. So let’s find out how to do that with MQL5.   To get started, please click on the little icon here or press F4 on your keyboard.   Now you should see the Metaeditor and here you want to click on file, new file, Expert Advisor from Template, continue. I will call this file simple moving text object. Click on Continue, continue and finish.   Now you can delete everything above the ontick function and let’s also remove the two comment lines here, we start by using MQL rates to create an array for the price info. MQLRates is a structure that stores information about the price, the volumes and the spread, and it will also store some time information. Now we are going to use ArraySetAsSeries to sort the array from the current candle downwards. And with CopyRates we can fill the array for the current symbol on the chart and the currently selected period on that chart.   And we want to copy the values for three candles and store them in the price info array. But let’s actually do it this way. Basically, it’s the same. If you mark that and press F1, you will see that you can exchange this function and use underscore period instead, these two values will automatically calculate the right symbol on the chart and the currently selected period on that chart. Now let’s move on and create an object. That is done by using object create for the current symbol on the chart. The object name will be moving text. This is the object type.    If you mark that and press F1, you will see that there are lots of different object types that you can use and we are going to use this one. It is able to output values and text. The last three parameters here are set to zero. Those are for the current subwindow. In case we use window zero, it will be the window where we draw the candles.   And the next parameters are for the date, time and the price. I didn’t provide this information here because we are going to use object_move later on to move our moving text to the point in time and the price that we want to use. But before we do that, we want to change a few properties. In this case, we use ObjectSetInteger for the Chart ID zero and the object that is called moving text. We want to change the object property font size and set it to eight.   Let’s repeat that, this time for the font color. The first two parameters are the same. This time we want to change object property color and set it to CLR red. If you mark that one and press F1, you will see that you can have a lot of different colors for several objects and to shift the form to vertical alignment, we are going to use ObjectSetDouble this time for object property angle and we will set that value to ninety degrees.   And of course we need to set that text value that is done by using object set string for the object property text and the text will be low followed by a colon. And this expression here will give us the low price for the current candle.   Now our object is finished, so let’s move it. And to do that we use object move. The first parameters are the same. The third parameter is for the alignment, or I think they call it the anchor point. We are going to move our object to the point in time where we print candle zero and we want to align it to the low price of candle zero.   That’s the current candle and that’s basically it. If you have no idea what all the code here does or if this was too fast for you, maybe you would like to become a Premium Course member. You can do that on our website. And this was another suggestion from a Premium Course member. So if you are a Premium Course member and have any ideas for a video like this one, please let me know. Let’s click on View Toolbar.   And press the compile button, or you can also use F7, we don’t have any errors, and in that case, we can click on a little button here or press F4 to go back to Metatrader.
    7 min
  • MQL5 TUTORIAL BASICS – 95 SIMPLE LOW GANN FAN OBJECT
    in this video we are going to create this kind of object this is a low gann fan object and we want to find out ho w to create one with mql5.   to do that please click on the little button 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 object simple lowgann fan.   click on continue, continue and finish.   now you can delete everything above the ontick function and the two comment lines here.   the first thing we need to do is to calculate the number of candles on the chart, that can be done by using chart get integer for the chart id 0.   we use chart first visible bar, all in capital letters and the last parameter is for the sub window, subwindow 0 is the one with the candles.   now let’s create a variable for the lowest candle, we also need to create an array.   i have called it low.   with array set as series we are going to sort our low array from the current candle downwards and now we can use a function called copy low that will give us the minimal bar prices for the current symbol on the chart and the currently selected period.   we start with candle 0 for all the candles on the chart and copy the values into our low array.   now we can calculate the lowest candle by using this function that is called array minimum that will give us the lowest element in our array and that’s inside of the low array.   we start with the first candle that’s candle 0 and we will go through all the candles on the chart and store the number for the lowest candle in this variable here.   let’s continue with mql rates, that will create a price information array for our prices but mql rates also will give us the time point for the candle.   let’s use array set as series for this one to also sort it from the current candle downwards and now we can use copyrates for the current symbol on the chart and the currently selected period on that chart.   we start with candle 0 for all the candles on the chart and store the candle values in our price information array.   before we can create a new object we want to use object delete to delete old objects for the current symbol.   i want to delete the objects that are called simple lowgann fan and afterwards we are going to use object create to create a new one.   so let’s go through the parameters here.   the first parameter 0 stands for the current chart.   the second one is the object name.   i have called my object simple lowgann fan.   parameter 3 is for the object type, i’m going to use obg underscore gannfan here.   if you mark that and press f1, you will see that there are lots of different objects.   this is the one that we are going to use.   the next parameter is for the main window, that’s windows 0 and we want to draw our object for the point in time where we have the lowest candle in our price information array.   we want to start with the lowest price of the lowest candle.   the second reference point is the point in time for the current candle 0 and as the last parameter we are going to pass the low price for the current candle in our price information array.   that’s it for the object create part.   let’s set a few properties.   the first property is for the object color.   we are using object set integer for the chart id 0 and the object with the name simple lowgann fan.   we want to change the object property color and set it to red.   now let’s set the trend direction.   the first part is similar but this time we are going to use object property direction and we are going to use gann uptrend.   let’s continue and set the scale.   that is done by using object property scale and my value here is 2.   and finally we want to set the property object property rayright to true because that will give us a kind of object prediction and draw the lines right into the future.   so far so good.   if you have no idea what all the code here does or if this was too f...
    8 min
  • MQL5 TUTORIAL BASICS – 94 SIMPLE SPREADSHEET FILE WRITER
    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....
    9 min