MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 65 SIMPLE BACKGROUND COLOR
    In this video we are going to create an expert advisor that is able to change the background color based on buy and sell signals, 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 background color, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here, first we need to get the ask price, this is done by using symbol info double, for the current symbol on the chart we use symbol underscore ask and with normalize double and underscore digits we make sure that the number of digits behind the dot is automatically calculated that could be either three or five digits depending on the currency pair. Now we repeat the process using symbol underscore bid this time to calculate the bid price and we also need to create a string, this will be called signal and we don’t assign a value here because we are going to calculate that now. Our expert advisor is going to use a moving average, so the first thing to do is to create a moving average array, this is going to hold our values and we use the included ima function that comes with mql5 to create a moving average for the current symbol on the chart and the currently selected period. Our moving average is going to calculate the values based on the last twenty candles, we start with the current candle, the method should be mode underscore sma for simple moving average and we calculate the values based on the close price. Afterwards, we use array set as series to sort the array from the current candle downwards and with copy buffer we fill our moving average array according to the definition that we have created here, for one line, we start with candle zero that’s the current candle and we copy the values for three candles and afterwards, we can get the moving average value by just looking into candle zero of our moving average array. And if that value is below the bid price we consider that to be a buy signal so we assign the word buy to our signal and for a buy signal we are going to call a function called, set chart color background and set the color to green. In the other case if the value for the moving average is above the ask price we consider that to be a sell signal so now we assign the word sell to our signal and we call the background color function and set the background color to red. Let’s add a third condition, if the value for m a moving average is below the ask price and above the bid price that would mean that we have no signal so we set the value for our signal to none and in that case we would like to call the custom function and set the background to black. Well, if you mark that and press F1 you will see that there are lots of other colors that you could choose but this function doesn’t exist so far so we need to create it in a few seconds, let’s add a chart output by using the comment statement and that will show us the text, the current signal is and the calculated signal directly on our chart. Now let’s create the custom function, it doesn’t return a value so we use void here, the name of the function is set chart color background and it takes a parameter, this one is of the type color, I have called it background color but you can call it whatever you like and inside that function, we are going to use chart set integer for the current symbol on the chart, we want to set the color so we use chart underscore color underscore background – all in capital letters – and the last parameter here is the background color that we have passed here. And that’s about it. Well, if this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other basic videos first or maybe the premium course on our website might be interesting ...
    7 min
  • MQL5 TUTORIAL BASICS – 64 SIMPLE MULTI RSI
    In this video we are going to create an expert advisor for the RSI indicator, in this case for multiple values in different time frames, 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 multi RSI, 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 with an include statement to include the file trade dot mqh. Afterwards, we are going to create an instance of the class ctrade, this is called trade and we are going to use it to open positions later on. Inside of the ontick function we calculate the ask price and the bid price that is done by using symbol info double, for the current symbol we use either symbol ask or a symbol bid – all in capital letters – and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Afterwards, we create an empty string called signal, we don’t assign a value here and now we need three arrays, one for the current time frame on the chart, one for the thirty-minute time frame and one for the sixty-minute time frame. Let’s define the current one, this is done by using the iRSI function for the current symbol on the chart and the currently selected period on that chart. We want to calculate the value for fourteen candles and the result should be calculated based on the close price. Let’s use array set as series to sort the RSI array for the current value, afterwards we use copy buffer for the current value that we have defined here and we store the values in the RSI current array and afterwards, we are able to calculate the current value. All we need to do is to get the value for the current candle zero out of our RSI array current and with normalize double we cut the result to show two digits because that is what the original RSI indicator also does and that’s it for the first value. Now we repeat the whole process to get values for thirty minutes, now we use the iRSI function for the current symbol on the chart but this time we are going to use period underscore m thirty and if you mark that and press F1 you will see that we have values for all the possible timeframes here, this one is for thirty minutes and this one is for one hour, so, for now, we use period underscore m thirty. Again we use array set as series to sort the RSI array for thirty candles, afterwards we use copy buffer according to the definition that we have created here and store the values inside of our RSI array thirty and to calculate the RSI value for thirty minutes we take a look at candle zero inside of our RSI array thirty and again we use normalize double and cut the result to have two digits behind the dot. To get the values for one hour we do the same again, this should be no surprise anymore we just use different variable names and this time we use period underscore h one, everything else is basically the same. And if all three values are above seventy that would mean that the blue indicator line here is above the upper dotted line for the seventy percent level, that would be a sell signal and now we would assign the word sell to our signal. If all three values are below thirty we would consider that to be a buy signal because that would mean that the signal line is below the lower dotted line here and that’s when we want to buy. So if the current signal equals sell and we get a return value that is below one for positions total that would mean that we have a sell signal and no open positions and now we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we have no open positions we would use trade dot buy and buy ten micro lot. Finally, we use the comment statement to create a chart output for the current...
    9 min
  • MQL5 TUTORIAL BASICS – 64 SIMPLE MULTI RSI
    In this video we are going to create an expert advisor for the RSI indicator, in this case for multiple values in different time frames, 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 multi RSI, 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 with an include statement to include the file trade dot mqh. Afterwards, we are going to create an instance of the class ctrade, this is called trade and we are going to use it to open positions later on. Inside of the ontick function we calculate the ask price and the bid price that is done by using symbol info double, for the current symbol we use either symbol ask or a symbol bid – all in capital letters – and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Afterwards, we create an empty string called signal, we don’t assign a value here and now we need three arrays, one for the current time frame on the chart, one for the thirty-minute time frame and one for the sixty-minute time frame. Let’s define the current one, this is done by using the iRSI function for the current symbol on the chart and the currently selected period on that chart. We want to calculate the value for fourteen candles and the result should be calculated based on the close price. Let’s use array set as series to sort the RSI array for the current value, afterwards we use copy buffer for the current value that we have defined here and we store the values in the RSI current array and afterwards, we are able to calculate the current value. All we need to do is to get the value for the current candle zero out of our RSI array current and with normalize double we cut the result to show two digits because that is what the original RSI indicator also does and that’s it for the first value. Now we repeat the whole process to get values for thirty minutes, now we use the iRSI function for the current symbol on the chart but this time we are going to use period underscore m thirty and if you mark that and press F1 you will see that we have values for all the possible timeframes here, this one is for thirty minutes and this one is for one hour, so, for now, we use period underscore m thirty. Again we use array set as series to sort the RSI array for thirty candles, afterwards we use copy buffer according to the definition that we have created here and store the values inside of our RSI array thirty and to calculate the RSI value for thirty minutes we take a look at candle zero inside of our RSI array thirty and again we use normalize double and cut the result to have two digits behind the dot. To get the values for one hour we do the same again, this should be no surprise anymore we just use different variable names and this time we use period underscore h one, everything else is basically the same. And if all three values are above seventy that would mean that the blue indicator line here is above the upper dotted line for the seventy percent level, that would be a sell signal and now we would assign the word sell to our signal. If all three values are below thirty we would consider that to be a buy signal because that would mean that the signal line is below the lower dotted line here and that’s when we want to buy. So if the current signal equals sell and we get a return value that is below one for positions total that would mean that we have a sell signal and no open positions and now we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we have no open positions we would use trade dot buy and buy ten micro lot. Finally, we use the comment statement to create a chart output for the current...
    9 min
  • MQL5 TUTORIAL BASICS – 63 SIMPLE CHECK FOR NEW BARS
    In this video we are going to create a new expert advisor that is able find out if we have a new candle on the chart. Each time a new candle appears the text here changes, we also have a bar counter, so let’s find out how to do that with mql5. To get started 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 check for new bars, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by calculating the current candle number or the number of bars on the chart. Bars and candles what you see on your chart depending if you use the candlestick view or the bar view and we can use a function that is included in mql5 that is called, bars. It takes two parameters, the first one is for the current symbol on the chart, the second one is for the currently selected period on that chart and it will return the number of bars in the history for a specified symbol and period. Now we continue and create a string variable, this one is called, new candle appeared but we don’t assign a value here. In the next step we want to find out if we have a new candle and to find out we call a function called, check for new candle and we will pass the candle number that we have calculated here. Well, this function doesn’t exist so far so we need to code it in a few seconds. Inside of the ontick function we want to create a comment statement that will output the text, bars on chart followed by the current candle number and in a new line we will see the text, new candle appeared followed by the return value of the function that we need to create now. Well, our user-defined function will be called, check for candle and it will take the current candle number that we have calculated as a parameter. And to find out if the number of candles on the chart has changed we create a so-called static integer variable here. If you mark that and press F1 you will learn that a static variable will hold its value as long as a function exists and we use it to store the last candle number inside of this function. Let’s create another string variable for the return value, this one is called, is new candle and the default value is that we have no new candle but if the current candle number is bigger than the one that we have for the last number of candles that is when we set the value for the new candle to, yes, a new candle appeared. And now we assign the current number of candles to the variable that is called, last candle number because this is what we need when we call our function for the next time. Finally, we need to return the calculated value for the new candle to our main function. This is basically it. Well, if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course on our website might be interesting for you. Actually, this was a question from one of our premium course members so if you already are a premium course member and if you have an idea for a video like this one, just send me an email. For now, we click on the compile button and if you don’t have any errors 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 press control and r, please pick the new file simple check for new bars dot ex5, please check the visual mode here and start a test. Here we are! Our little expert advisor is running and you can see that the number of bars is changing and each time we have a new bar or candle on the chart, we can see here the output changes and in this little video, you have learned how to create an expert advisor for Metatrader 5 that is able to use a user-defined function to find out if we have a new candle on the c...
    7 min
  • MQL5 TUTORIAL BASICS – 63 SIMPLE CHECK FOR NEW BARS
    In this video we are going to create a new expert advisor that is able find out if we have a new candle on the chart. Each time a new candle appears the text here changes, we also have a bar counter, so let’s find out how to do that with mql5. To get started 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 check for new bars, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by calculating the current candle number or the number of bars on the chart. Bars and candles what you see on your chart depending if you use the candlestick view or the bar view and we can use a function that is included in mql5 that is called, bars. It takes two parameters, the first one is for the current symbol on the chart, the second one is for the currently selected period on that chart and it will return the number of bars in the history for a specified symbol and period. Now we continue and create a string variable, this one is called, new candle appeared but we don’t assign a value here. In the next step we want to find out if we have a new candle and to find out we call a function called, check for new candle and we will pass the candle number that we have calculated here. Well, this function doesn’t exist so far so we need to code it in a few seconds. Inside of the ontick function we want to create a comment statement that will output the text, bars on chart followed by the current candle number and in a new line we will see the text, new candle appeared followed by the return value of the function that we need to create now. Well, our user-defined function will be called, check for candle and it will take the current candle number that we have calculated as a parameter. And to find out if the number of candles on the chart has changed we create a so-called static integer variable here. If you mark that and press F1 you will learn that a static variable will hold its value as long as a function exists and we use it to store the last candle number inside of this function. Let’s create another string variable for the return value, this one is called, is new candle and the default value is that we have no new candle but if the current candle number is bigger than the one that we have for the last number of candles that is when we set the value for the new candle to, yes, a new candle appeared. And now we assign the current number of candles to the variable that is called, last candle number because this is what we need when we call our function for the next time. Finally, we need to return the calculated value for the new candle to our main function. This is basically it. Well, if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course on our website might be interesting for you. Actually, this was a question from one of our premium course members so if you already are a premium course member and if you have an idea for a video like this one, just send me an email. For now, we click on the compile button and if you don’t have any errors 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 press control and r, please pick the new file simple check for new bars dot ex5, please check the visual mode here and start a test. Here we are! Our little expert advisor is running and you can see that the number of bars is changing and each time we have a new bar or candle on the chart, we can see here the output changes and in this little video, you have learned how to create an expert advisor for Metatrader 5 that is able to use a user-defined function to find out if we have a new candle on the c...
    7 min
  • MQL5 TUTORIAL BASICS – 62 SIMPLE MARGIN MONITOR
    In this video we are going to create a simple margin monitor that is going to output all the margin related information on our chart, 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 margin monitor, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We will use account info double to get most of the information that we need, this is one of the rare cases where mql4 and mql5 are almost similar. To get the desired information we need to create a variable and use account info double for a certain property, in this case, the account margin. Let’s repeat that for the free account margin. There is also a property that is called account margin initial. The documentation says that it is the initial margin and the amount reserved on an account to cover the margin of all pending orders. Well, if you see the return value is zero here, that’s also the case for account margin maintenance and as I have created an mql4 video for this topic I have seen that the mql4 reference says that this is not supported but maybe it depends on the account type, so let’s continue. The next level will be the account margin level, and when you click on view, toolbox or press control and t you should see the same value for the margin level here. There is also something called account margin maintenance, they say it is for the minimum equity reserved on an account to cover the minimum of all open positions. Well, I have an account, I have open positions but the return value for account margin maintenance is zero, for me more interesting is the account margin stop out call, that’s the level when your broker will tell you that you don’t have enough money. For my account type, this is set to forty percent and there is the final stop out level for the account, that’s when your broker is going to close everything and for my account type, this will happen when my equity goes below ten percent. I would like to add another value for my account profit to my margin monitor and that’s it for the account info double values, but there is also something that is called account info integer and we can use it to get the account margin mode, this is something that is different because Metatrader 4 didn’t know anything about margin modes. In Metatrader 5 we started by using the netting mode if you use an account with netting mode you are not able to trade several positions for the same currency pair as they say only one position can exist for one symbol, so what we are using is the hedging mode and there is also an exchange mode, I have never used that before. I am not very good at remembering integer values so let’s create a string variable called account margin mode readable and depending on the return type of the integer value we will assign the text, netting mode, exchange mode or hedging mode. You see that my margin monitor is showing that we are using the hedging mode here. Another margin related value is the account margin stop out mode. We will get that by using account info integer for account margin so mode and this property has two return types, it can either be a stop out mode that is calculated in percents or it’s a money value, so let’s create a string variable to make those return types more readable and depending on the return type we will either create an output that is showing us that the account stop out mode is in percent or if the return value is one we will see that the account stop out mode is in money. Finally, we will add a comment statement that will output all the calculated values on our chart. Okay! If this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other videos in this basic vi...
    8 min
  • MQL5 TUTORIAL BASICS – 62 SIMPLE MARGIN MONITOR
    In this video we are going to create a simple margin monitor that is going to output all the margin related information on our chart, 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 margin monitor, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We will use account info double to get most of the information that we need, this is one of the rare cases where mql4 and mql5 are almost similar. To get the desired information we need to create a variable and use account info double for a certain property, in this case, the account margin. Let’s repeat that for the free account margin. There is also a property that is called account margin initial. The documentation says that it is the initial margin and the amount reserved on an account to cover the margin of all pending orders. Well, if you see the return value is zero here, that’s also the case for account margin maintenance and as I have created an mql4 video for this topic I have seen that the mql4 reference says that this is not supported but maybe it depends on the account type, so let’s continue. The next level will be the account margin level, and when you click on view, toolbox or press control and t you should see the same value for the margin level here. There is also something called account margin maintenance, they say it is for the minimum equity reserved on an account to cover the minimum of all open positions. Well, I have an account, I have open positions but the return value for account margin maintenance is zero, for me more interesting is the account margin stop out call, that’s the level when your broker will tell you that you don’t have enough money. For my account type, this is set to forty percent and there is the final stop out level for the account, that’s when your broker is going to close everything and for my account type, this will happen when my equity goes below ten percent. I would like to add another value for my account profit to my margin monitor and that’s it for the account info double values, but there is also something that is called account info integer and we can use it to get the account margin mode, this is something that is different because Metatrader 4 didn’t know anything about margin modes. In Metatrader 5 we started by using the netting mode if you use an account with netting mode you are not able to trade several positions for the same currency pair as they say only one position can exist for one symbol, so what we are using is the hedging mode and there is also an exchange mode, I have never used that before. I am not very good at remembering integer values so let’s create a string variable called account margin mode readable and depending on the return type of the integer value we will assign the text, netting mode, exchange mode or hedging mode. You see that my margin monitor is showing that we are using the hedging mode here. Another margin related value is the account margin stop out mode. We will get that by using account info integer for account margin so mode and this property has two return types, it can either be a stop out mode that is calculated in percents or it’s a money value, so let’s create a string variable to make those return types more readable and depending on the return type we will either create an output that is showing us that the account stop out mode is in percent or if the return value is one we will see that the account stop out mode is in money. Finally, we will add a comment statement that will output all the calculated values on our chart. Okay! If this was too fast for you or if you don’t understand what all the code here does maybe you want to watch one of the other videos in this basic vi...
    8 min
  • MQL5 TUTORIAL BASICS – 61 SIMPLE OVERLOADED FUNCTION
    In this video we are going to talk about function overloading, we have an output here that says we are inside a function with two parameters and we are going to create another function with the same name that will overload the first one, so let’s see how we can 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 function overloading, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. In this case, I would like to create a customized function called add values and I want to be able to add two or three values. To have something to calculate we first want to get the ask price that is done by using symbol info double for the current symbol on the chart, I use symbol underscore ask and with normalize double and underscore digits we let Metatrader automatically calculate the right number of digits behind the dot. Now let’s repeat the same for the bid price, this is equal but this time we are using symbol underscore bid and another variable name called bid and my third value will be the difference, that is simply a double value that is calculated and called difference and this is the ask price minus the bid price. I would like to add the values for the ask and the bid price so I need to code a function that is called add values and this one takes the ask price and the bid price as a parameter. So what does that mean? Well, you might have seen other functions where you have to pass parameters like this one and when you remove the part behind the function name you will see that here is something that is called one of two and this is actually an overloaded function, we have two different ways to use it and that’s also what we want to do here in this example, I have passed two parameters, so let’s start with the first version of the overloaded function. I will call that one add values it returns a double value and it takes two parameters, one for the ask value that we have passed here and the second one for the bid value that we have passed as a second parameter. let’s use a print statement that will output inside function with two parameters in our journal. Now comes a basic calculation where the return value will be equal to the ask value plus the bid value and finally we will return the calculated value to our main function here. let’s add a comment statement here so we have an output on the chart saying the result is followed by the calculated result value and that’s it. I can now compile the code but when I remove the parameters here I have only one version of my function, so far it is not overloaded so let’s create a new version, this is the same name of the function it’s the same return type but this one can take three parameters, once again we add the print statement but this time it will say inside function with three parameters, this time we calculate the return value by adding the first, the second and a third parameter and the rest is like in the function above. Now let’s recompile the code and now when I remove the parameters here you can see that it is overloaded, we can either use the one that takes the ask value and the bid value or when we click on the overloaded description here we can also use the other one that will take the ask value the bid value and the difference value. Well, if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you. If you are already a premium course member and if you have a question like this that should be turned into a video, please send me an email, for now, we click on the compile button and if we don’t have any errors we can click the lit...
    8 min
  • MQL5 TUTORIAL BASICS – 60 SIMPLE DYNAMIC SELL POSITION SIZE
    In this video we are going to create an expert advisor that is able to dynamically calculate the position size for sell positions, so let’s find out how to do that with mql5. To get started please click on the 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 dynamic sell position size, 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 dot mqh, this one comes with mql5 and it contains some basic simplified trading functions. Afterwards, we create an instance of the class ctrade, that is called trade and we are going to use it to open a position later on but first, we want to calculate the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid – all in capital letters – and with normalize double and underscore digits we calculate the number of digits behind the dot. Depending on the currency pair this might be three or five digits and this function is going to do that for us. Now we want to get the equity that is done by using account info double, account underscore equity – all in capital letters – and we repeat the whole thing for the balance. This is the same function but this time we use account underscore balance. And I would like to have my calculated position size to have two digits behind the dot so I use normalize double here and the size should be equity divided by one hundred thousand. For the test entry, we want to check a few simple conditions. The first one is if the equity is at least equal or greater than the balance and the second condition is we want to know if positions total equals zero, if that is the case we have no open positions and that is when we use trade dot sell to open a new sell position and we use the calculated position size. To see what’s going on we can use the comment statement here that will output the words balance, equity and position size followed by the calculated values and that’s about it. If you don’t understand what all the code here does or this was too fast for you maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course might be interesting for you, actually, this video was also a request from a premium course member so if you already are a premium course member and have an idea for a video like this one just let me know. For now, let’s click the compile button here or press F7, 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 you want to click on view, strategy tester or press control and r, please pick the new file, simple dynamic sell position size dot ex5, mark the option for the visual mode here and start your test. Here we are! The current equity is one hundred thousand, so the position size is one micro lot, let’s speed that up a little bit and now we should see that the position size is changing so the little expert advisor works as expected and in this little video you have learned how to create an expert advisor for Metatrader 5 that is able to dynamically calculate the position size based on the equity and you have coded it yourself with a few lines of mql5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 59 SIMPLE USER DEFINED BUY RANGE
    In this video we are going to create a price range for buy trades, it is allowed to open positions within the two defined limits here, so let’s find out how to do that with mql5. These are two user-defined variables and our expert advisor is going to open trades within these two boundaries so let’s 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 buy range, 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 dot mqh, it comes with mql5 and it includes some simplified trade functions so let’s create an instance of the class ctrade, we will call that trade and we are going to use it to open a position later on. I also want to define an upper limit that is done by using the input modifier here because with the prefix input you can create a user-defined variable and that can be changed in the strategy tester without any further coding, it’s a double variable, I will call it upper limit and the initial value will be ten. Let’s repeat that for the lower limit, that will be zero and we also need to create a global variable for the ask price because we are going to use the ask price in two different functions. Inside of the on tick function, we 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 – and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. For the entry conditions, I would like to know if the return value for positions total equals zero, if this is true we have no open positions and I would also like to know if the price is in the allowed range and only if this is true we use trade dot buy and open a new position for ten micro lot. To see what’s going on we will use the comment statement that will output the upper and the lower limit, the ask price and a Boolean value for price is within range. To get the value true or false we need to create this function because it doesn’t exist right now. The return type of the function is bool, so what does that mean? The bool type can either have the value true or false that is equal to one for true or zero for false. First, we create a variable, this one is Boolean, the name is return value and the initial value is false and now we check if the ask price is below the upper limit and if the ask price is above the lower limit and only if both conditions are true we will set the return value to true. Finally, we use the return statement to return the value to the main module and that’s about it! If you don’t understand what all the code here does or if this was too fast for you 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. This video was actually requested from one of the premium course members so if you have a request and if you are a premium course member just send me an email, for now, we click on the compile button or press F7. And if we don’t see any errors we can click on the little button here or press F4 to go back to Metatrader. And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file, simple buy range dot ex5, mark the option for the visual mode here and start a test. Here we are! The expert advisor says that the price is within the range so we see that it is opening buy trades. Let’s speed that up a little bit! And now the price is outside of the range, we don’t see any further buy trades so our expert advisor working as expected and in this little video you have learned how you can create user-defined input variables that can be adjusted by the user and y...
    7 min