MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 122 SIMPLE AVERAGE TRUE RANGE BUY STOP
    In this video we are going to use the average true range indicator to open buy trades and to set a trailing stop value for buy trades. So let’s find out how to do that with MQL 5. To get started please click on a little icon here or Press F four 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 Average True Range Buy Stop. Click on continue, continue and finish. Now you can delete everything above the on tick function and let’s also remove the two comment lines here. We start with an include statement to include the file trade.Mqh.   This will allow us to use the class Ctrade. We are using an instance of Ctrade called Trade here. Let’s define a few global variables. Global variables are defined outside of other functions. They are accessible for each of the functions.   The first one is an int variable for the basic stop point value, we will start with 100 points. The second one will be the calculated stop point value. The third one is for the ask price. Inside of the on tick function we start by creating a string variable for the signal. We don’t assign a value here because that’s what we are going to calculate later on.   Let’s continue and calculate the Ask price. That is done by using symbol info double for the current symbol on the chart. We use symbol underscore Ask, all in capital letters here and with normalize double and underscore digits, we make sure to calculate the right number of digits behind the dot. Now we need to create a price array, that is a double array so it can hold floating type values.   We are going to use the included IATR function to define the average true range parameters here, for the current symbol on the chart and the currently selected period on the chart. We want to calculate values for 14 candles to sort the array from the current candle downwards. We can use the array set as series function for our price array. With copy buffer we can fill our price array with data according to the definition that we have created here, we are going to copy values for buffer zero starting from the current candle zero, for three candles and store the result in our price array.   That makes it possible to calculate the average true range value for the current candle.   We just have to look at the value of candle zero in our price array, and I use normalize double and five again to get five digits behind the dot, like it is shown here. Let’s add another static double variable. Static means that the value for this variable will be inside of the computer memory for as long as our computer program is running. Now we can check if we have a buy signal. That would be the case if the current average true range value is bigger than the old value.   If that is the case, we will set our signal to buy and that’s when we use trade buy to buy ten microlot. We check if the signal equals buy and if positions total is below one, that would mean we have no open trades. And that is when we use trade buy to buy ten micro lot. This is a test position. You wouldn’t do that on a real account.   And to calculate the stop point value, we are now going to call a function that is called Check ATR Buy trailing stop and pass the average true range value that we have calculated as a parameter. To see what’s going on, I would like to have a command output that will show us the signal, the average true range value and the calculated stop point value on our chart. It would be better to read when we do it this way. It makes no difference for the compiler, but it’s better to read for humans.   After all the calculation is done.   We will now assign the average true range value from now as old value for the next time we call the function that’s it with the main function, but we need to create this one. It is a user defined function and we will call it check atrby trailing stop.
    9 min
  • MQL5 TUTORIAL BASICS – 121 SIMPLE RSI SELL STOP LOSS
    In this video we are going to create an expert advisor that is able to open a sell trade when the RSI Oscillator is above 70 and to close the trade when it reaches a value below 30. So let’s find out how to do that with MQL five. To get started, please click on the little button here or Press F four 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 simple RSI Sell Stop. Click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. We start with an include statement to include the file Trade.Mqh. This one comes with MQL 5 and we use it to create an instance of the class C trade that will be called trade, and we are going to use it to open a test position. Inside of the on tick function we need to calculate the ask and the bid price. That can be done by using symbol info double for the current symbol on the chart. We use either symbol ask or symbol bid, and with normalize double and underscore digits, we calculate the number of digits behind the dot. Let’s continue and create an RSI array. We use array set as series to sort our array from the current candle downwards and with the built in function Irsi we can calculate the RSI value for the current symbol on the chart and the currently select period on that chart.   We want to calculate it for 14 candles based on the close price and with copy buffer, we can fill our RSI array for the current buffer zero starting from the current candle zero and store the values for free candles in our array according to the RSI definition that we have created here.   That makes it possible to calculate the current RSI value by looking into candle zero for our RSI array, I used normalize double and two to create an output with two digits because that is what the RSI looks like. And to have a demo position, we want to know if positions total equals zero. And if the RSI value is greater than 70. If both conditions apply, we use Trade sell to open a sell trade for ten microlot. Afterwards we want to check the RSI sell stop. That is a function that doesn’t exist so far, so we need to create it in a few seconds and we will pass the RSI value as a parameter here.   But before we do that, let’s create a chart output by using the comment statement to output the RSI value on the chart. Now let’s continue with the user defined function that is called Check RSI Selll stop. We pass a double parameter for the RSI value here. Now we use a for loop to go through all the open orders. We want to check if the current position symbol equals the position symbol for the current counter value in the for loop.   We use position get integer to get the position ticket and we also use position get integer to get the position type. Now let’s check if the position type equals position underscore type underscore sell. If that is true, we have a sell trade and that’s when we want to check if the RSI is below 30. That is done with this if statement. And if this is true, we use trade position close to close the current position ticket.   Now let’s close the if loop and the function and that’s about it. If this was too fast for you or if you have no idea what all the code is good for, you maybe want to watch one of the other videos first, or maybe you even want to consider to become a premium member. You can find the premium course on our website. For now, please click on the compile button or press F seven. I have an error here. I need to remove the additional bracket.   Now let’s recompile the code and this time it works without any errors. And if that is the case, you can click on a little button here or press F four to go back to Meta Trader and in Meta Trader, please click on View, Strategy Tester or Press Control and R. Now we want to pick the new file. Simple RSI sellstop EX five. Mark the option for the visible mode here and start a...
    7 min
  • MQL5 TUTORIAL BASICS – 120 SIMPLE RSI BUY STOP
    In this video we are going to create an Expert Advisor that is able to use the RSI indicator to open a position when the value is below 30 and to close it when the value is above 70. So let’s find out how to do that with MQL5. To get started, please click on the little icon here or Press F four 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 RSI buy stop. Click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. We start with an include statement to include the FILE TRADE.MQH. This is an included trade library and it makes it possible to use a few simple trade functions. To do that, we create an instance of the class Ctrade that will be called trade. Inside of the on tick function we need to calculate the ask price and the bid price, that can be done by using symbol info double for the current symbol on the chart. We use symbol ask or symbol bid, and with normalize double and underscore digits, we can automatically calculate the right number of digits behind the dot. Let’s continue and create an RSI array. Afterwards we want to sort the RSI array with array set as series, that will sort the array from the current candle downwards and the included function Irsi will make it possible to calculate the current value for the current symbol on the chart and the currently selected period on our chart.   We will do that for 14 candles and the calculation should be done based on the close price. With copy buffer we can now fill our RSI array with data according to the definition that we have created here. We do it for buffer zero. We start with the current candle zero and we want to copy the values for three candles and store them in the RSI array. And that makes it possible to calculate the current RSI value by using normalize double for candle zero in our RSI array. I would like to have two digits behind the dot because that is also how the RSI will show up on our chart, and if we have no open position, that would mean that the return value for positions total equals zero, and if the RSI is below 30, that would mean we have a buy signal.   We use trade buy to open a test position for ten microlot.   Afterwards we want to call a function that is called Check RSI buy stop and pass the current RSI value as a parameter. Let’s add a comment statement to show the RSI value and the calculated value on our chart. This is the end of the main function, but this function doesn’t exist, so we need to create it now. The name of our user defined function will be check RSI buy stop, here we will get the parameter RSI value that we need to calculate if we want to close the position.   Now we use a for loop to go through all the open orders and for each counter value, we want to get the position symbol. That is done by using position get symbol for the current counter value.   Let’s remove this German comment here. We also want to get the position ticket, that is done by using position get integer and as a parameter we use position underscore ticket here. Finally, we want to know the position type. And we can do that by using position get integer and the parameter this time is position type, because that will make it possible to check if the current position type equals position type buy. That would mean that we have a buy trade. And if that is the case, we want to check if the RSI value is bigger than 70.   And if that is true, we use trade position close to close the current position ticket. Finally, please close the if statement and the user defined function here and that’s about it. Well, 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 beginner videos, or maybe even the Premium course on our website might be interesting for you. That’s where you also can find some links to down...
    8 min
  • MQL5 TUTORIAL – SIMPLE VOLUMES FILTER
    In this video we are going to create an Expert Advisor that is going to use the Bollinger Bands as an entry signal and it will also use the Volumes Indicator as a filter. Only if the filter is positive and we have a buy or sell signal we want to open a new position. This is going to be a standalone version and it contains three separate modules. In this video we will talk about the filter function. Now how can we create a filter that is using the Volumes Indicator? To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window, this is the main module, it will include the Bollinger Bands Entry (CheckEntry_Bollinger) and the Check Filter module (CheckFilter_Volumes) for the Volumes Indicator. If you don’t know what the rest of the code does please have a look at the other videos in this channel. The “OnTick” function is called whenever the price changes and here we will use the “CheckEntry” function to get the signal, this is the Check Entry Module (CheckEntry_Bollinger) that will give us the signal for the Bollinger Bands but in this video we are going to talk about how to get the filter value. To do that we need a new file, this one, it is called: “CheckFilter_Volumes” and we are including it here using the “include” statement. Inside of the function we first need to create a variable for the filter that will be called: “filter” and we don’t assign a value here. Afterwards we are going to create an array for the prices that will be called: “myPriceArray” and now we define the filter by using the “iVolumes” function that is included in MQL5, we use “_Symbol” for the first parameter to get the currency pair, that’s for the selected time period on the chart and parameter 3 is “VOLUME_TICK” all in capital letters. We use “ArraySetAsSeries” to sort the prices from the current candle downwards for our price array (myPriceArray) and now we use “CopyBuffer” to fill the array with data. We are going to use the volumes definition (VolumesDefinition) that we have created here to fill the array (myPriceArray) for buffer 0 (zero) from the current candle 0 (zero) for 3 candles and store the values in our price array (myPriceArray). Let’s calculate the value for the current candle that is done by looking into candle 0 (zero) of our price array (myPriceArray) and we are going to store the value in the variable: “CurrentVolumesValue“ and now we do the same for the candle before – that is candle 1– and we will store that value in the variable: “LastVolumesValue“ and in the case that the current volumes value (CurrentVolumesValue) is bigger than the last volumes value (LastVolumesValue), with other words; we have a rising volume like here, that’s when we want to assign the word: “positive” to our filter. In the other case if the current volumes value (CurrentVolumesValue) is below the last volumes value (LastVolumesValue) that would be the case here, the current candle has a volume value of 14 and the volumes value of the candle before was 37 and in that case we want to set the filter value to negative so we assign the word: “negative” to our filter and last but not least we are going to use the return statement to return the filter value to our main module. So that’s done all by calling this function here and when the trading signal equals buy and the filter is positive and “PositionsTotal” equals 0 (zero) – that means we don’t have any open position – that’s when we use “trade.Buy” to buy 10 micro lot. In the other case if the trading signal equal sell and the filter is positive (remember we only want to use increasing volumes because in theory an increasing volume means that the trend is going to continue) and we only want to open a new position when we don’t already have an open position and in that case we use “trade.Sell” to sell 10 micro lot. Here is the final chart output that will output the words: “Trading signal:” followed by the calculated value for the entry signal and ...
    7 min
  • LEARN MQL5 TUTORIAL BASICS – 119 MYFXBOOK EXPERT ADVISOR INSTALLATION
    In this video we are going to import the MyFxBook Expert Advisor so we can publish our statistics on a website. So let’s find out how to do that with MetaTrader 5. This is MyFxBook. It’s a website that provides advanced statistics for automated trading systems. These are a few of my systems and you can see that they have been updated. And this website here, MyFxBook dot com, provides some advanced statistics.   This is a system that I have closed on February 4th and it says it stopped updating and I should download the latest MyFxBook Expert Advisor. Actually, I closed this one manually, but for this video, we are going to click on this link here. This is the MyFxBook EA, here we see the download. This is the download file and when we open it, you will see that it contains some files and a version TXT.   So let’s actually start the installer process by double clicking it. And now it says that some additional applications need to be downloaded. This is the Dot net framework for Windows, so let’s click on download and install. This might take a little while. This framework data is actually the reason why some Linux distributions have problems to update the Expert Advisor for MyFxBook, but on a Windows machine you should have no problems.   The download should be finished in a few seconds. Now we’ll see that the installation is started and afterwards we see the dialogue that the DotNet framework was installed. So let’s close that window. Now we need to restart the installation process. This time we will see a different window.   We can pick one of the installations. Actually, I have downloaded the wrong Expert Advisor installer. This one is for MetaTrader 4. So let’s cancel that. And as often we will have more success when we do a Google search.   The third entry in the Google search is the Metatrader 5 Expert Advisor Connection Wizard. So let’s click on this link and this one says that the EA installer MT5. So it’s for Metatrader 5. You can drag and drop the MetaTrader 5 icon into this window. Here is mine.   So let’s drop it here and it says the access was denied. So let’s click on Browse. You should find it on your hard drive, mine is located on the C drive, programs MetaTrader 5 and now it says that we have an authorization problem. So let’s right click this file and select run as administrator, repeat the browsing process.   Pick the file Terminal ICO. Click on Open and this time the installation worked. It says it was installed successfully. Now we can click on Finish and exit the installer window. If your Metatrader window is open, please right click on Expert Advisors and select refresh.   Afterwards you should see the MyFxBook Expert Advisor entry down here. Now let’s drag it onto the chart. Click on the Dependencies tab here. We need to allow DLL imports. Now please click on the inputs tab, type in your email address and your MyFxBook password and click on OK.   In this case, it says that it has not published anything. Status is 502. That’s because I used the wrong password. If you have that issue, please press F7 and correct your credentials. Afterwards the 502 error is gone.   This icon here should be green. If it isn’t, please click on tools, options, Expert Advisor, allow algorithmic trading and DLL imports. Now please click on okay, I will now close and restart Metatrader. Now let’s repeat the process. I have to retype my credentials.   It says last publish is never and it might take a little while. Let’s try to switch to a different time frame. You can also check your journal for errors, but it says that the Expert Advisor was loaded successfully. So let’s give it a few minutes and now it says it’s uploading the statement. Once again, we have a status error.   It looks like we have a problem with this file. Ms. Core DLL but when you scroll down on the MyFxBook site, you will see that they actually provide their own MyFxBook DLL and the MyFxBook Ex5 file. So let’s download those two files and try the manual wa...
    8 min
  • MQL5 TUTORIAL BASICS – 118 SIMPLE LAST ORDER TYPE
    In this video we are going to create an expert advisor that is able to get the last order type. I hope I can get it done before the building noise around the house starts, 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 File, Expert Advisor from Template. Continue. I will call this file simple last order type.   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. And that includes the class C trade. We’re going to use an instance of Ctrade that is called Trade. Inside of the Ontick function we need to calculate the ask price and the bid price. That can be done by using symbol info double for the current symbol on the chart.   We use either symbol underscore ask or symbol underscore bid. And with normalize double and underscore digits, we’ll make sure to automatically calculate the right number of digits behind the dot. Now we want to open test positions. If we have no positions, that would be the case when positions total is below one. We are going to use Trade Buy and Trade Sell to open test positions on a test demo account. This would make no sense on a real account, but obviously we need to have some positions to find out the position direction of the last one. And to get the position direction we want to call a function that is called Get Last order type and assign the result to a string variable called My last order type.   And we use a comment statement to output the text my last position was and the result of the function that we are going to create now. So let’s start. The name of the new function will be Get Last order type and it will return a string variable. We need to define a few variables that are required to get the proper history result.   Now we use History Select.   We can pass two time stamps from date and to date. We want to start from the beginning until the current time. Now we use a for loop to go through the total number of deals. Afterwards we check if the ticket number for the current history deal ticket of the counter value is above zero. That would mean we have a real ticket, and in that case we would like to get some data.   First we want to calculate the order profit. That is done by using History deal get double for the current ticket number and we use Deal underscore profit. If you mark that and press F1, you will see that we have so called deal properties. And this is the one we use to get the profit. Let’s continue and get the order type. That is done by using History deal get integer for the current ticket number. And this time we use Deal underscore type. Let’s continue with the current symbol. We use History deal, get string for the current ticket number to get the deal symbol, and then we continue and use History deal get integer for the current ticket number to get the deal entry. Let’s check if the current symbol on the chart equals the symbol of the deal.   And of course, if the deal entry is one, that would mean that the order was closed. And if the close type was order type buy, the original position direction was a sell trade. Otherwise, if the close type was order type sell, the original position direction was a buy trade, let’s assign the code profit, the ticket number and the position direction to our string variable called my result. Now we close the loops and finally we return our result to the main module. So far, so good.   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 former starter videos, or maybe even the premium course on our website might be interesting for you. This once again was a suggestion of a premium course member. And if you are already a premium course member and have an idea for a video lik...
    8 min
  • MQL5 TUTORIAL BASICS – 117 SIMPLE MULTIPLE CHART TREND
    In this video we are going to create an expert advisor that is able to identify trends on different charts. So let’s find out how that can be done for 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 File, Expert Advisor from Template, continue. I will call this file simple multi chart trend.   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 want to start by creating an array for the price information, that can be done by using MQL rates. The name of the array will be priceinfo. Afterwards we use Array set as series to sort our price information array from the current candle downwards, and now we use copyrates to file it for the current symbol on the chart and the currently selected period on that chart, starting with the current candle zero and we will store the values for three candles in our price info array.   Now we want to create two variables. One is called chart signal and the other one is called Euro USD signal. We don’t assign any values here. Let’s continue and create two arrays. One is called EMa 20 array.   The other one is EMA Euro US Dollar 20 array. And now we can use the integrated IMA function that comes with MQL5 for the current symbol on the chart and the currently selected period on that chart. To calculate the exponential moving average for 20 candles, we don’t use a shift value here. This parameter is called Mode EMA, that stands for exponential moving average, and the result should be calculated based on the close price. Let’s continue and this time we are doing it in a similar way, but the first parameter is hard coded for the Euro US dollar.   If you click on the symbol in the market watch, you will see a name, that might be a different name depending on your broker and your account type. So please make sure that you pick the right one. Everything else is similar. So now we can use array set as series for both arrays to sort them from the current candle downwards. And now we use copy buffer for both arrays, the one for the current symbol on the chart and the other one for the Euro US dollar to fill them for buffer zero, starting from the current candle zero for three candles.   And now we can check the trend. We want to check if the current candle zero is bigger than candle two for the EMA 20 array. If that is the case, the signal for a current chart is buy. Otherwise, if the value for candle zero is below the one for candle two, that would be a sell signal for the current chart, and this part is basically the same, but for the hard coded Euro US dollar. Finally, we add a comment statement to output the values for the EMA 20 for the current symbol, the EMA 20 for the Euro US dollar, the trend for the current symbol and the trend for the Euro US dollar, and that’s about it.   Well, if this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other videos, or maybe even the premium course on our website might be interesting for you. There you can also find some links for source code downloads like this one and a shop link. For now we are done. Please click on compile or Press F7 on your keyboard. You shouldn’t get any errors, and if that is the case, you can click on the little button here or press F4 to go back to Metatrader. And in MetaTrader, please pick an empty chart.   Go to the Navigator and pick the simple multi chart Trend Expert Advisor and drag it on to the chart. I will allow the modification of signal settings, but it will not actually trade. It will just show us trends and when the next tick comes in, you should see that the current trend for the Canadian dollar against the Swiss franc is a buy trend. Now please click on insert Indicators, trend, select Moving Average. Pick a period of 20 candles for the exponential...
    8 min
  • MQL5 TUTORIAL BASICS – 116 SIMPLE ENVELOPES SELL TRAILING STOP
    In this video we are going to create an expert advisor that is able to adjust a sell trailing stop according to the upper band of the envelopes indicator. So let’s find out how to do that with MQL5. To get started, please click on the little button here or Press F four 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 Envelopes Sell Trailing Stop. Click on continue, continue and finish.   Now you can delete everything above the Ontick function and let’s remove the two comment lines here. We start with an include statement to include the file trade.mqh. This file is part of MQL5 and it will help us to open a test position. To do that, we need to create an instance of the class Ctrade and that will be called trade. We need to define a static double variable called Last Stop envelopes value.   This will hold the last stop loss value for each time we call the function. We also need to calculate the ask price and the bid price. That can be done by using symbol info double for the current symbol on the chart. We either use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot. To test a trailing stop, we need to open a test position.   Therefore, we check if positions total is below one. If that is true, we want to reset the last value for the stop loss and set it to zero and then we use Trade.sell to sell ten micro lot. You wouldn’t do that on a real account, but that’s what demo accounts are good for. Let’s continue and create two arrays for the upper and the lower band array. We use Array set as series for both arrays to sort them from the current candle downwards. And now we can use the included function Ienvelopes for the current symbol on the chart and the currently selected period on that chart to calculate the envelopes indicator for 14 candles. We don’t use a shift value here. The next parameter is mode underscore SMA, that stands for Simple Moving Average. The result should be calculated based on the close price and this last parameter here is for the deviation, that’s the standard value. If you pick an empty chart window and click on insert, indicators, trend, envelopes, you will also see this value as a deviation value.   Here are the 14 candles. Now please click on OK, rightclick into the chart. Select Templates, save template and save this one as tester.tpl. You can override the current value. Tester.tpl  is the view that we will see when we start the strategy tester.   But back to coding. We continue by using copy buffer to copy data according to the envelopes definition that we have created here, either for buffer zero or for buffer one, for candle zero. That’s the current candle and we want to copy the values for three candles and store them in the upper or in the lower band array. It’s now possible to get the current value for candle zero. I have out commented it in this version, because I’m more interested in the values for the candle before. That’s candle one. To get the last upper band value or the last lower band value, I will look into our array at candle one and with normalize double and comma six, I make sure that the value is using six digits behind the dot. Because that’s also what the original envelopes indicator will show. Now we can call a custom function called CheckEnvelopesSelltrailingStop and pass the last upper band value as a parameter. This function doesn’t exist, so we need to create it in a few seconds. The last thing in the Ontick function is to assign the last upper band value to the last stop envelopes value here, because we need that the next time we call the function.   Now let’s actually create the check envelopes sell trailing stop.   It will get the last upper band value as a parameter here. The type is void because we don’t return any values....
    9 min
  • MQL5 TUTORIAL BASICS – 115 SIMPLE ENVELOPESBUY TRAILING STOP
    In this video, we are going to create a buy trailing stop that is based on the envelopes indicator. 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 window and here you want to click on file, new file, Expert Advisor from Template, Continue. I will call this file simple envelopes buy trailing. Stop, 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 and we’ll include the file Trade.mqh. This one comes with MQL5 and we can create an instance of the included Class Ctrade. That instance will be called trade and we are going to use it to open a position later on. Inside of the ontick function, we start with a static variable. This is a double variable for the last value of the Stop Loss.   Afterwards, we need to calculate the Ask price and the Bid price. That can be done by using Symbolinfodouble for the current symbol on the chart, we use either symbol underscore ask or we use symbol underscore bid and with normalize double and underscore digits, we make sure that we calculate the right number of digits behind the dot, because there are currency pairs with three digits and others have five digits. For our Stop Loss, we need to open a test position. That wouldn’t be done on a real account.   But if we have no open positions and that would be true of PositionsTotal is below one, we want to open a test position on our demo account. So we use trade.Buy to buy 10 Microlot and we want to reset the last value for the Stop Loss. Because with the new position we need to set a new Stop Loss value. Now let’s continue with two arrays, one for the upper band and one for the lower end of the envelopes indicator. With arrays set as series we sort the upper and lower band array from the current candle downwards. And now we can use the included Ienvelopes function for the current symbol on the chart and the currently selected period on that chart.   We want to calculate the values for 14 candles, the next value here is a shift value. We don’t need that. So we set it to zero. We use MODE SMA for simple moving average. The result should be based on the close price and this last value here is what you see when you open a new chart and click on insert, indicators, trend, Envelopes. It’s the so-called deviation. We are going with the standard values here. Click on OK.   Here is the indicator, and now we want to right click into the chart, save the template as tester.tpl, you can override the current value because the Tester.tpl template is what we will see in the strategy tester. Now, let’s add some data. We use Copybuffer according to the envelopes definition that we have created here. To fill our array for the buffer zero and the buffer one. Buffer zero is for the upper Band and buffer one is for the lower band.   We want to copy data from the current Candle zero for three candles and store those values in our arrays. To calculate the upper band array for the current candle, we only need to look at Candle zero in our upper band array. I use Normalize double and six to cut it to six digits behind the dot. And we repeat that for the lower band value. Let’s repeat that, everything is equal, but this time we are doing it for candle one. That’s the candle before the current candle.   And of course, the variable names are different. This time, we use my last upper band value and my last lower band value. So let’s check for the conditions for the buy trailing stop. The first condition would be that the last lower band value is below the current Bid price. The second condition is we want to check if the last lower band value is bigger than the last stop envelopes value. If both conditions are true, we want to call a function that is called check envelopes buy trailing stop and we want to pass the last lower bands value as a param...
    9 min
  • MQL5 TUTORIAL BASICS – 114 SIMPLE SHIFTED PRICE EA
    In this video, we are going to find out how to trade pure price data. This is a so-called line chart and we are using the shifted price data to make, buy and sell decisions. 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 file, Expert Advisor from template, continue.   I will call this file simple shifted price EA, click on Continue, continue and finish. Now you can delete everything above the ontick function. We also want to remove the Comment lines here. Let’s start with an include statement to include the file Trade.mqh.   It contains the class Ctrade. And we create an instance and call it trade. Afterwards, we need to calculate the Ask price and the Bid price, that can be done by using Symbolinfodouble for the current symbol on the chart.   We use either symbol underscore ask or symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot. Let’s continue and create a string variable called signal, but we are not going to assign a value right now.   With MQLRates we create an array for the price information. Let’s use arraysetasseries to sort our price information array from the current candle downwards.   And now we can use CopyRates to fill the array with data for the current symbol on the chart and the currently selected period on that chart. We start with the current Candle zero and copy the price data for three candles to store it in the price information array. The next step is to create an array for the moving average values. We use the integrated function IMA for the current symbol on the chart and the currently selected period on the chart. We want to calculate the simple moving average for one candle and we want the shifted moving average to be drawn twenty candles into the future.   We use ModeSMA for simple moving average and the result should be calculated based on the close price.   With array set as series we also sort the moving average array from the current candle downwards. Now we can use copybuffer according to the definition that we have created here. We are going to copy data for buffer zero from current Candle zero. We copy price data for three candles. To calculate the current value for the moving average we just need to look into Candle zero of our moving average array and if the moving average value is below the close price for the current Candle zero, that would be a Buy signal and that’s when we assign the word buy to our signal.   Otherwise, if the moving average value is above the close price for Candle zero, that would be a Sell signal. And now we would assign the word sell to our signal. And when our signal equals sell and PositionsTotal is below one, we use trade.Sell to sell 10 Microlot. Otherwise, if the signal equals buy and we have no open positions, we use trade.Buy and buy 10 Microlot. Finally, I would like to add a comment statement that will output “The current signal is now”, followed by the calculated signal 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. Or maybe even the Premium Course on our website might be interesting for you. If you need source codes for videos like this one, you can find a shop link on our website. But for now, please click on the compile button here.   Or press F7 on your keyboard. You shouldn’t get any errors. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, please pick an empty chart. Change the view to line chart here. Right, click into the chart and select template, save template. Please save it as tester.tpl because this is the view that we are going to see in the strategy tester, you can override the current one.
    7 min