MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 68 SIMPLE MOMENTUM MONITOR
    In this video we are going to create an expert advisor that is able to calculate the average value for the momentum indicator, so let’s find out how to do that with mql4. 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 momentum monitor, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. First, we want to create a variable called, current momentum state, it’s a string variable so it can hold text but so far we don’t assign any values here. Afterwards, we are going to calculate the current momentum value by using the included imomentum function for the current symbol on the chart and the currently selected period on that chart. We want to calculate the results based on fourteen candles, the calculation should be done using the close price and for the current value want to see the value for candle zero. Now if you mark this function and press F1 you will see that we have a shift value, this is the last parameter and they say it’s the shift relative to the current bar the given amount of periods ago, so basically if we change this value to thirteen we would get the calculation for candle fourteen but we don’t want to repeat this line fourteen times. So let’s create another array called, momentum values, and this one can hold fourteen values for fourteen candles. We also want to create a variable called, average momentum value, the initial value will be zero and the third thing that we need is a variable that is called, momentum values summary. Let’s assign also zero to that one and now we use a for loop to go through all the elements in our array, we start with an index value of thirteen, that might look a little bit confusing because we said that we want to calculate fourteen candles. Well, the problem is that an index for an array starts with zero, so the first candle would be candle zero and candle number fourteen would be index thirteen, so basically what this says is, start from index thirteen – which is the last candle – and until you reach the index zero – which is the current candle – do what’s inside the for loop and subtract one from the current index number until you are finished. And now we can use this index number here to calculate a temporary momentum value for each of the fourteen candles. The rest in this line is basically the same and we use the momentum values summary and add the current temporary momentum value that we have calculated in this line to this summary until there are no candles left and finally we calculate the average momentum value by dividing the summary by the number of candles. And if the current momentum value is bigger than the calculated average momentum value, that’s when the momentum is above the average value and now we assign, momentum is above average to the variable that is called, current momentum state. Otherwise, if the current momentum value is below the average momentum value, that’s when we assign the sentence, momentum is below average to our current momentum state. Finally, we use the comment function to create an output on our chart for the current momentum value, the average momentum value for the last fourteen candles and the current momentum state. Well, if this was too fast for you or if you don’t have any idea what all the code here does maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you. For now, we click on the compile button or press F7, I didn’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 we pick a clean chart, I prefer to see the line chart. Now we click on insert, indicators, oscillators, momentum,
    8 min
  • MQL4 TUTORIAL BASICS – 67 SIMPLE VERTICAL LINE
    In this video we are going to create an expert advisor that is able to draw a vertical line for each sell position, it is going to open a position whenever we have the highest of one hundred candles, so let’s find out how to do that with mql4. 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 vertical line, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. First, we want to identify the highest of the one hundred candles that can be done by using the function ihighest for the current symbol on the chart and the currently selected period on that chart. We use mode high because we want to find the highest candle, we want to do that for the last one hundred candles and we start with the current candle zero. And if the highest candle equals the current candle – that would be the case when the last candle here would be higher than this one – and if orders total equals zero (that would be the case when we have no open positions) that’s when we use order send for the current symbol to sell ten micro lot. Afterwards, we want to call a custom function called draw v line and we pass the number for the highest candle as a parameter. Well, this function doesn’t exist so we need to code it in a few seconds but before we do that we want to create a chart output using the comment statement. This will output the text, the highest of the last one hundred candles, is candle number, followed by the calculated highest candle and it will also output the text, the highest price is and the price for that candle. Our user-defined function will have no return value, the name is, draw v line and we pass an integer value for the highest candle – remember this is the number of the highest candle, not the price – and before we draw a line we want to delete an old-line if we have one because we only want to see one of those vertical lines on the chart. Afterwards, we use object create to create an object that is called, line. The type of the object is obj underscore vline, if you mark that and press F1 you will see that we have a lot of deferent object types here, we will draw this one, that’s a vertical line, the reference has lots of options for that line. For example, this is how you would change the color or you could use object set integer for the current chart and change the line width. We want to draw the object on the main chart that is chart number zero. Some indicators would be below the candles that would be the chart number one, for example for the macd and the line should be drawn for the current candle, this parameter here is for the high of the highest candle that is the current candle and that’s about it. 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 one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you. This was actually one of the ideas from a premium course member and if you are already a premium course member and if you have an idea for a video like this one, please let me know. But for now, please click on the compile button or press F7 on your keyboard, I don’t have 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 click on view, strategy tester or press control and r, now pick the new file, simple vertical line dot ex4, mark the option for the visual mode here and start a test. Here we are! The expert advisor is running, I want to zoom into the chart by pressing the minus key on the keyboard and speed that up a little bit. Here we are! This is the first sell trade, this was the highest candle at that time and in this little video you ha...
    6 min
  • MQL4 TUTORIAL BASICS – 66 SIMPLE SHIFTED EMA
    In this video, we are going to create an expert advisor that is able to calculate buy and sell signals based on two shifted moving averages, so let’s find out how to do that with mql4. To get started please click on a little icon here or press F4, 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 ema, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here, we start with a few user-defined variables, these are input variables, if you mark that and press F1 you will learn that these are variables that can be changed while the program is running, you don’t have to recompile the code, you can change the settings inside of the inputs tab. The first value is for the number of candles that we are going to use to calculate the exponential moving average, the second one is a shift value, that’s twenty and we have another shift value one hundred and what those shift areas do is they move the moving average to the right side ahead of the price movement. When we zoom into the chart you can see that we have two moving averages that are similar but they are shifted to the right side, the first one is shifted twenty candles into the future and the second one one hundred candles into the future. Inside of the on tick function, we create a string variable for the signal but we don’t assign any values here because we are going to calculate them now. Let’s start with the first moving average. We use the included ima function that comes with mql4 for the current symbol on the chart and the currently selected period on that chart, we use the number of candles that we have defined here so this one will be for twenty candles, the next one is the shift value that we have defined here, that’s also for twenty candles. We are going to use mode ema that stands for exponential moving average, the values should be calculated based on the close price and we are always going to calculate the current candle value. Well, if you mark the ima function and press F1 you will see that we have two shift values, this one, and this one that might look a little bit confusing but it’s not so hard to understand. This one is where you draw the moving average so in our case we draw it on the right side of the chart and this shift value here is for the candle number that should be calculated and we are going to calculate the current candle number so we are using a shift value of zero for this parameter. Let’s repeat the whole process but this time we are going to calculate it for the second moving average but this one will use the value shift two and that is one hundred candles and that’s why it is moved far to the right side ahead of the price movement. We want to buy if the price is above both ema’s so if the bit price is bigger than the first moving average and if the bid price is bigger than the second moving average that’s when we want to buy so now we assign the word buy to our signal. In the other case if the current bid price is below the first moving average and if the bid price is also below the second moving average that is when we want to sell and if this is true we are going to assign the word sell to our signal. When our signal equals buy and the return value for orders total is zero that would mean we have no open orders and a buy signal and in that case, we use order send to buy ten micro lot. Otherwise, if the signal equals sell and we have no open orders we use order send to sell ten micro lot. Finally, we want to add a comment statement that should output the text, the current signal is, followed by the calculated signal. Okay, 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 might be interesting for you you can ...
    9 min
  • MQL4 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 depending on the buy or sell signal, so let’s find out how to do that with mql4. 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 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 create a string variable for the signal currently, we don’t assign any value here because we are going to calculate that later on. And to do that we want to use a simple moving average and to calculate that one we use the included ima function for the current symbol on the chart and the currently selected period. We want to calculate based on the last twenty candles, we don’t use any shift values here, the mode is sma for simple moving average and the simple moving average will be calculated based on the close price. Let’s continue with the conditions. First, we want to check if the moving average is above the bid price and if that is the case we want to assign the word buy to our signal and now we want to set the chart color for the background to green. Otherwise, if the moving average is above the ask price that would be a sell signal so now we assign the word sell to our signal and now we want to set the chart color for the background to red. The third case would be when the moving average is below the ask price and the moving average is above the bid price, in that case, we would have no signal so we assign the word none to our signal and this is when we want to set the chart color for the background to black. Well, if you mark that and press F1 you will see that you can set lots of colors in our case we have used clr black here, I would like to add a comment statement on the chart that will create an output, the current signal is, followed by the calculated signal. And now we need to code this user-defined function because it doesn’t exist so far. The name for our function is set chart color background, we use void here because we don’t have a return value and we need to pass a color I have called it background color but you can call it whatever you like and inside of our function we use chart set integer for the current symbol on the chart, we want to change the property chart color background – all in capital letters – and here we pass the background color that we have received here and that’s about it. 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 one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you, this was actually an idea of a premium course member and if you are already a member of the premium course and have an idea for such a video, please send me an email. Okay, so far so good! Now let’s click on the compile button or press F7, I don’t see any errors here and if that is the case you can click on the little button here or press F4 to go back to Metatrader. And inside of Metatrader please click on view, strategy tester or press control and r, please pick the new file, simple background color dot ex4, mark the option for the visual mode and start a test. Here is our expert advisor at work, currently we have a sell signal, the background is red and when we get a buy signal it should change the color to green, that happened right now so our little expert advisor is working and in this little video you have learned how to create an expert advisor that is able to change the background color of the chart based on the sell or buy signals that you receive and you have coded it yourself with a few lines of mql4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 64 SIMPLE MULTIPLE RSI
    In this video we are going to create an expert advisor that is able to calculate the values for the RSI indicator for different time frames, so let’s find out how to do that with mql4. 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 click on file, new file, expert advisor from template, continue, I will call this file simple multiple 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 by creating a variable for the signal, this is a string variable called signal but we don’t assign any values here because we are going to calculate that later. Calculating the current RSI value that is done by using the included iRSI function that comes with mql4, we do it for the current symbol on the chart and the currently selected period on that chart, we want to calculate the value for fourteen candles based on the close price and we don’t want to shift the indicator here, shifting means that we could use another value that is relative to the current bar, zero means that we calculate the value for the current one and now we also want to see the values for the thirty-minute time frame and the sixty-minute time frame. You could change the view for the different time frames here and to do that we simply need to exchange one value here and that’s the one for the period. For thirty minutes I use period underscore m thirty, if you mark that and press F1 you will see that we have all these time frames available here this one is for thirty minutes and the period underscore h one value is for sixty minutes, so let’s add this one and of course you need to change the names of the variables here but now we have three values, the one for the current time frame, the one for thirty minutes and the one for sixty minutes and if all those time frames show an RSI value that is above seventy – that would be the case when the signal line is above the upper dotted line here – that is what we would consider to be a sell signal so now we assign the word sell to our signal. In the other case if all the RSI values for the different time frames are below thirty that would be a buy signal and in that case, we would assign the word buy to our signal and if our signal equals buy and the return value for orders total equals zero – that would mean we have a buy signal and no open orders –and in that case we use order send for the current symbol and buy ten micro lot. Otherwise, if the signal equals sell and we have no open orders we would use order send for the current symbol and sell ten micro lot. Finally, we want to use the comment statement, this will output the names of the variables and the current signal followed by the calculated values directly on our chart. This is 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 videos in this basic video series first or maybe even the premium course might be interesting for you, you can find that one on our website. For now, please click on the compile button or press F7, you shouldn’t get any errors here and if this is the case you can click on a little symbol here or press F4 to go back to Metatrader. And in Metatrader we use a simple euro-usd one-minute chart window, let’s click on insert, indicators, oscillators, relative strength index, we want fourteen candles, the calculation should be based on the close price, please pick a color here and click on okay and now you should see a separate window containing the RSI and I would like to add the new file that is called simple multiple RSI and drag it on the chart, let’s click on okay. And here we are! You can see that all the values are calculated because this time we are using a real-time chart, the current value is sixty-five, nine, one, four, eight, that should match the value here. Now,
    8 min
  • MQL4 TUTORIAL BASICS – 63 SIMPLE CHECK FOR NEW BARS
    In this video we are going to create an expert advisor that is using a user-defined function to check for new bars on the chart, so let’s find out how to do that with mql4. 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 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. First, we want to create a string variable that is called, do we have a new bar, we don’t assign any value here because this is what we want to calculate later and to do that we are going to call a user-defined function that is called check for a new bar on chart, that one doesn’t exist so far so we need to code it in a few seconds but before we do that we want to create a comment statement that will output the bars on the chart followed by the calculated number of bars and in a new line the text new bar appeared followed by the return value for the function that we are going to create now. Our user-defined function will return a string, it is called check for new bar on chart and inside of that function, we are going to create a static integer variable called last number of bars. This term static means that the value for the variable will live inside of that function for as long as the function exists and each time we call the function we will get the last number of bars. Let’s create a return value, this one is a string variable we call it new bar appeared and the default value is that we don’t have a new bar but if the current number of bars is bigger than the last number of bars that’s when we change that and now we assign the value, yes a new bar appeared to our return value. Now we assign the number of bars to the variable that is called last number of bars because we will need it the next time we call the function and finally we use the return statement to return the calculated value that’s either, no we have not a new bar or yes a new bar appeared on the chart. 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 other videos in this basic video series or maybe even the premium course on our website might be interesting for you, this was actually a question from a premium course member and if you are a premium course member and have an idea for a video like this one, please send me an email. For now, we click on the compile button or press F7. I don’t have any errors here and if this is the case we can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we want to click on view, strategy tester or press control and r, please pick the new file simple check for new bars dot ex4, mark the option for the visual mode here and start a test. Here we are! The expert advisor says that we have one hundred and one candles on the chart, that changed and each time the counter value changes you can see that we have a new bar on the chart so our little expert advisor is working as expected and in this little video you have learned how to create a user-defined function that is able to calculate if we have a new bar on the chart and you have coded it yourself with a few lines of mql4 code.
    6 min
  • MQL4 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 mql4. To get started please click on a 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 margin monitor, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. This is one of the rare cases where mql4 and mql5 are almost similar. We will use something that is called account info double. If you know mql5 you will already know how that works but for mql4 it use to be completely different but after an upgrade, it is possible to use account info double and get the required information. We will start with account margin and as you see there are lots of more entries here, the procedure is the same for each of the property values, so let’s repeat that for the free margin and now you can see that this term here has a different color than this one and that is because the old way to get those values in mql4 included a function that was called account margin. So let’s change that to my account margin and now it’s black. Let’s repeat the same thing for the initial account margin that will work but currently inside of the mql4 reference they say that it is not supported, that’s also true for the property account margin maintenance. This is one of those cases where I don’t understand why they implement something that is not supported but I have included it anyhow because it works with mql5. Now let’s continue with the account margin level, this is the calculated value here and it should be equal to what you see for the margin level here in Metatrader 4. The next one would be the account margin maintenance, as I said this is not supported in Metatrader 4 but it is part of the language now. The next property is one of the interesting ones, this is the stop out call. In my case, the stop out call would be at sixty percent of my account level. The next one is the stop out level, that’s when your broker is going to close everything and that would be reached when my account equity goes below forty percent. Let’s include the account profit as you see this is another function, so let’s call that my account profit and now there is something that is called account info integer and to calculate the margin stop out mode we would use account info integer and here the parameter we have to pass is account margin so mode, but this would return an integer value and I would like to have that a little bit more readable, so let’s create a string variable called account margin stop out mode readable, we don’t assign a value here and if the account margin stop out mode would return a zero we assign the sentence, account stop out mode is in percent to our variable here. Otherwise, if the return value for the account margin stop out mode is one that’s when we would assign the sentence account stop out mode is in money. Finally, we want to add a comment statement to create an output that will show us all the calculated values on our chart. 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 one of the other videos in the basic video series first or maybe even the premium course on our website might be interesting for you. Actually, this was a suggestion of a premium course member and if you are a premium course member and have ideas for videos like this one, just send me an email. Okay! For now, we can click on the compile button or press F7, I have messed up something up here! Oh, this one is wrong and here I have to remove the braces and now when I recompile I don’t get any errors and if this is the case we can click on the little button here or press F4 to go back to Metatrader.
    7 min
  • MQL4 TUTORIAL BASICS – 61 SIMPLE OVERLOADED FUNCTION
    In this video we are going to create an overloaded function that is able to take two or three parameters, so let’s find out how to do that with mql4. 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 function overloading, 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 a difference that is a double value and we get it when we subtract the bid price from the ask price. Afterwards, we want to call our user-defined function that one will be called add values, it will take two parameters, the ask price and the bid price and it will return a result value where the two values from the two parameters are added and later we will create another overloaded function with the same name but the overloaded version will be able to take three parameters. Inside of the ontick function we need to add a comment statement to output the text, the result is, followed by the calculated value and this function doesn’t exist so far so we need to create it now, the return type will be double, the name of the function is add values and it takes two parameters, the first one is for the ask price, the second one is for the bid price and if you ask yourself why this name is not the same as this one, well we are free to use any name we want inside of the user-defined function. I would like to do a print statement inside of the journal tab, this one should output that we are inside of the function that can handle two parameter values, it doesn’t do very much it just is going to create a return value by adding the ask value and the bid value and afterwards, we are going to use the return statement to return the value that we have calculated to the main function. That was very easy! You can compile the code now and you shouldn’t get any errors but so far we have no overloaded function and that’s why we are going to create the next function with the exact same name but this version can handle three parameters, the third parameter is the difference value. Once again I would like to use the print statement to output the text, this is the function that takes three parameter values, this function will add all three parameters and it also will return the calculated value to the main function. Now let’s click on the compile button, in my case I don’t have any errors and no warnings but if you don’t have an understanding 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 first or maybe even the premium course on our website might be interesting for you. Actually, it was a premium course member that suggested this topic for video and if you are a premium course member already and if you have some ideas that could become such a video here just send me an email. Okay, so why did we do all this here? Well, if you cut out the parameter part here you will see that the help function will show us that we can use one of two different versions for this function that is the overloading. The first function will take the ask value and the bid value and when you click here you will see that the second function is able to handle three parameters, so let’s find out if this works as designed. 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 view, strategy tester or press control and r, please pick the new file, simple function overloading dot ex4, mark the visual mode option here and start your test. Here we are! The expert advisor is running, it returns a result and when we click on the journal tab we see that we are using the function that takes two parameter values,
    7 min
  • MQL4 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 based on the equity, so let’s find out how to do that with mql4. To get started please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple dynamic sell position size, click on continue, continue and finish. Now we can delete everything above the on tick function and let’s also remove the two comment lines here. Inside of the on tick function we are going to calculate the position size, the variable type is double so it can have floating type values. Basically, I would like to have my position size to be calculated based on the account equity, the equity is basically what is left over after you have paid for all commissions, costs and open positions, the calculation should be the account equity divided by one hundred thousand and I use normalize double and the value two to calculate a position size with two digits behind the dot, that’s what we see here because otherwise, you might see a lot of errors here, saying something like the position size is wrong and this is a way to avoid requotes from the broker. Afterwards, we want to find out if the account equity is bigger or at least equal to the account balance and if this is the case we want to also check if orders total returns a value of zero, if this is true we have no open positions (and if this is true) and we are going to use the order send statement for the current symbol, we want to open a sell trade for the position size that we have calculated here. Finally, we want to use the comment statement to output the text balance, equity and position size followed by the calculated values on our chart. This is pretty much 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 of this basic video series first or maybe even the premium course might be interesting for you, actually this video was an idea of a premium course member so if you are already a premium course member and have an idea for such a video just send me an email. For now, we click on the compile button, we don’t see any errors here and if this 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 select the new file, simple dynamic sell position size dot ex4, mark the option for the visual mode here and start a test. Here we are! The expert advisor is running, the current position size is one, that might change in a few seconds when the equity goes below one hundred thousand dollars, so let’s speed that up a little bit! Actually, it’s now one point zero one, so let’s fast forward and now it’s zero dot ninety-nine because the equity is ninety-nine thousand four hundred dollars so our little expert advisor works as expected and in this little video you have learned how to create an expert advisor that is able to automatically calculate position sizes for sell trades based on the equity and you have coded it yourself with a few lines of mql4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 59 SIMPLE USER DEFINED BUY RANGE
    In this video we are going to create an expert advisor that is able to open buy positions within a range that we can define, so let’s find out how to do that with mql4. 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 buy range, 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 by defining an upper limit and please notice the modifier extern, this one makes it possible for the user to change the value inside of Metatrader, this is a double value because we will need a floating type, the name is upper limit and the initial value is ten. Now let’s repeat that for the lower limit. Everything is the same accept of the variable name and this time the initial value is zero. Inside of the on tick function, we want to check if the return value for orders total is zero. If this is the case we have no open orders and no open positions and that is when we want to check if the price is within our defined range. If this is the case we would receive the return value true and if both conditions are true we would use the order send command to open a buy trade for ten micro lot. And to see what’s going on we will use comment, this will output the upper and the lower limit, the current ask price, the text price is within the range and the return value for this function, but this function doesn’t exist so far so we need to create it now. Our function will return a Boolean value and that means that the return value can either be true or false. Actually, it will return one for true and zero for false. Inside of our function we want to create a Boolean return value, the initial value is false. Now we want to check if the ask price is above the lower limit and if the ask price is below the upper limit and if this is true we will assign the value true to our return value. Finally, we use the return statement to return the value to our main module. This is 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 might be interesting for you, you can find that one on our website. Actually, this video was requested by one of the premium course members so if you are a member of the premium course and if you have a request for a video like this just send me an email. Okay! Now, let’s click on the compile button. We don’t see any errors here and in that case, you can click on the little button here or press F4 on your keyboard. Now we are back in Metatrader and here we want to click on view, strategy tester or press control and r, please pick the new file, simple buy range dot ex4. You can now define the upper and lower value here in the expert properties, please mark the visual mode here and start a test. Here we are! The expert advisor says that the ask price is within our defined range so it opens buy trades. Let’s speed that up! And now the price has moved, the price is no longer within the range so it doesn’t open new buy trades and in this little video you have learned how to create an expert advisor that is able to define an upper and a lower limit and it will only open new trades within that range and you have learned how to code it yourself with a few lines of mql4 code.
    6 min

About MQL4TUTORIAL.COM

From the publisher's feed

MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming