MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 74 SIMPLE ENVELOPES INDICATOR
    In this video we are going to create an expert advisor for this indicator here, this is 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, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start with an include statement to include the file trade dot mqh, this one comes with mql5 and it will give us the ability to use simplified trading functions, now let’s create an instance of the class ctrade, we will call it trade and we are going to use it later on to open positions. Inside of the ontick function we start by calculating the ask price that is done by using symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask – all in capital letters – and I use normalize double and underscore digits to automatically calculate the right number of digits behind the dot depending on the currency pair that might be three or five digits. Let’s repeat that for the bid price, everything is pretty similar but of course, we need to change the name for the variable and this time we are going to use symbol underscore bid. Now let’s create a string variable called signal, this will hold the signal but we are going to calculate that later on so we don’t assign a value here. And to get some price information we use mql rates, this is the structure that stores the information about prices, volumes and spread, it will create an array that we now need to sort from the current candle downwards, we can do that by using array set as series and with copy rates, we can now copy data for the current symbol on the chart and the currently selected period, we start with candle zero and we want to copy the price information for three candles and store it in our array but this is the old way to do it, let’s use underscore symbol and underscore period if you mark that and press F1 you will see that you can exchange the two functions here. Okay! Now we need two more arrays, one is for the upper band and one for the lower band. Those two arrays also need to be sorted by using array set as series and now we can define the envelopes that is done by using ienvelopes for the current symbol on a chart and the currently selected period on that chart, we do that for fourteen candles, we don’t use a shift value here, the calculation method is mode underscore sma that stands for simple moving average, I would like to have the results based on the close price and this last value here is also what you will see if you click on insert, indicators, trend, envelopes, this is the deviation value and that’s what we use here. Now we can fill our arrays with data, we use copy buffer to fill the upper band and the lower band array according to the envelopes definition that we have created here. The upper band is for buffer zero, the lower band is for buffer one and we do it from the current candle zero for three candles and now we can calculate the values all we need to do is to look into the value for candle zero in the upper or the lower band array and I also use normalize double for six digits here because that’s what you get when you point your mouse at one of the two bands, it has six digits behind the dot. Now let’s define the entry conditions! If the close price for candle one is below the lower band value we would consider that to be a buy signal so we assign the word buy to our signal. In the other case if the close price for candle one is above the upper band value we would consider that to be a sell signal and now we assign the word sell to our signal and if our signal equals sell and positions total is below one that would mean we have no open positions but a sell signal and now we ...
    9 min
  • MQL5 TUTORIAL BASICS – 73 SIMPLE SPREAD FILTER
    In this video, we are going to create an expert advisor that is able to allow or forbid trading based on the current spread, 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 spread filter, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start with an include statement to include the file trade dot mqh. This one comes with mql5 and it provides simplified trading functions. Afterwards, we use the class ctrade to create an instance called trade and we are going to use that one later to open new positions. And we would like to create a user-defined input variable for the max allowed spread, that value can be changed later on without recompiling the code. Inside of the on tick function we want to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol ask or symbol bid based on what we want to calculate and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. For the trading entry, we want to create a string variable, this one is called signal but we don’t assign a value here because we are going to calculate that later on. We need some price data therefore we use mql rates to create a price array. With array set as series, we are going to sort our price array from the current candle downwards and afterwards, we use copy rates to fill our price array with data for the current symbol on the chart and the currently selected period, we start with candle zero and we want to copy the values for three candles. Now let’s calculate the current spread, this is done by using symbol info integer for the current symbol on the chart and we use symbol underscore spread to calculate the current spread. Let’s create another string variable called spread filter, this is going to be used for our filter and if the current spread is bigger or equal the max spread that we have allowed the spread filter will get the value, trading is not allowed because the spread is too high. Otherwise, if the current spread is below or equal to the max value that we have allowed that’s when we assign the value, trading is allowed to our spread filter. Now let’s create some simple entry conditions, in this case, I’ll check if the close price for candle one is bigger than the close price for candle two, in that case, I want to buy so we assign the value buy to our signal. Otherwise, if the close price for candle one is below the close price for candle two that’s when we want to sell so now we assign the word sell to our signal but before we allow any positions we make it a precondition that the spread filter has the value, trading is allowed and only if that is the case we check the entry conditions. If the signal equals sell and positions total returns the value below one that would mean we have no open positions but a sell signal and that’s when we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and we have no open positions that’s when we use trade dot buy to buy ten micro lot but we only do that if the spread filter equals trading is allowed. Finally, we want to use the comment statement to create an output for the current spread, the max spread that we have allowed and the spread filter. That’s about it. If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you. Actually, this was a suggestion from another premium course member and if you are already a premium course member and have an idea for a vi...
    8 min
  • MQL5 TUTORIAL BASICS – 72 SIMPLE TRIPPLE EMA
    In this video we are going to code an expert advisor for this triple exponential moving average, 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 triple 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 an include statement to include the file trade dot mqh. This one comes with mql5 and now we use the class ctrade and create an instance called trade that we are going to use later on to open positions. We want to calculate the ask price for buy positions that 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 automatically calculate the right number of digits behind the dot. Let’s repeat that for the bid price. Everything is identical except for the variable name and this time we are going to use symbol underscore bid as the second parameter. Afterwards, we use the function mql rates to create an array called price info and with array set as series, we are going to sort the array downwards from the current candle and finally, we can use copy rates to fill the array for the current symbol on the chart and the currently selected period on that chart. We start with candle zero – that’s the current candle – and copy the price data for three candles to store it inside of our price info array. Let’s create a string variable called signal for our signal but we are not going to assign any value here because this is what we want to calculate now, therefore we create three arrays for each of the exponential moving average values. This one is for ten candles, the second one is for fifty candles and the third one is for one hundred candles. The integrated ima function that comes with mql5 can be used to create an exponential moving average for the current symbol on the chart, we use underscore period to calculate it for the current period. In this example I have done it for ten candles, we don’t use a shift value here and we want to create an exponential moving average, therefore we use mode underscore ema and the result should be calculated based on the close price. Now let’s repeat the whole thing for the other two exponential moving averages. Everything is identical except for the name for the variable and the number of candles. This is the last one for one hundred candles. Those arrays also need to be sorted from the current candle downwards so let’s repeat that for all the three arrays and as you might have guessed we use copy buffer to fill each array with data according to the definition that we have created. We do that for buffer zero – that’s the signal line – starting from candle zero and in this case, we do it for ten candles. Please make sure that you use different names for the ema and for the ema arrays here. And now we can calculate if we have a buy signal and that would be the case if the exponential moving average for ten candles is bigger than the one for fifty candles and if the exponential moving average array for fifty candles is bigger than the one for one hundred candles if that is the case we have a buy signal and that’s when we want to assign the word buy to our signal. In the other case, if we have a moving average for ten candles that is below the one for fifty candles and if the one for fifty candles is also below the one for one hundred candles, that’s when we want to sell, so now we assign the word sell to our signal and if our signal equals sell and the return value for positions total is below one – that would mean we have no open positions but a sell signal and that’s when we use trade dot sell to sell ten micro lot. In the other case,
    8 min
  • MQL5 TUTORIAL BASICS – 71 SIMPLE ARROW SYMBOLS
    In this video we are going to draw symbols on our chart, you see that we have different symbols appearing here, so let’s find out how to do that with mql5. To get started please click on a little symbol 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 arrow symbols, 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 calculate 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 calculate the right number of digits behind dot. Let’s also repeat that for the bid price, it’s almost the same except for the variable name and we are going to use symbol underscore bid in this. I would like to place the object in the middle so let’s calculate that one. We get the medium price when we add up the two values and divide them by two. Now let’s use the random generator to draw random symbols for the initialization I use mathsrand and gettickcount, when you mark mathsrand and press F1 you will see that it is generating a series of pseudorandom integer values and when we use gettickcount we get the number of milliseconds since we started the system so in this combination we will get a unique value and we will use that value to now use mathrand that will return another integer value, in our case, we will calculate a number within that range and to draw our symbol we now use object create for the current currency pair on the chart, this is the name of the object, it’s called my object and I would like to have an arrow so I use the object type obj underscore arrow here, now let’s press F1 and you will see that arrows are just one of many available types but we can have several shapes, the next parameter is used to define if the arrow should be drawn in the upper or the lower window. This would be window zero but if you use oscillators you could have a second window below the candles, we don’t need that. Time current is used for the last known server time, in our case, the object will be drawn here because that’s the current time and the anchor point for the object should be the medium price, so let’s set the value on the main chart for the object with the name my object, I would like to set the value for the object property arrowcode and that is where we use the random arrow code that we have generated here. Objectsetinteger is a way to set several values, for example, I can also set the object property width, in my case I would like to use the value twenty for bigger objects and each time when the tick comes in I would like to use objectmove to move our existing object to the current point in time. Finally, let’s create a chart output for the ask price, the bid price, the medium price 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 or maybe even the premium course on our website might be interesting for you. If you are a premium course member and have ideas for a video like this one please let me know. This was a suggestion that I have received this week, for now, please click on the compile button or press F7 and if we don’t have any errors here we can click on the little button here or F4 to go back to Metatrader. Inside of Metatrader please click on view, strategy tester or press control and r. Here we want to pick the new file, simple arrow symbols dot ex5, please also mark the option for the visual mode here and start a test. Here we are! You can see that we have different symbols each time, not every return value will have a valid symbol code but in this video, you have learned how to create an expert advisor that is able to draw symbols directly on ...
    7 min
  • MQL5 TUTORIAL BASICS – 69 SIMPLE PRICE CORRELATION
    In this video, we are going to calculate a moving average for two currency pairs and create an expert advisor to find out if we have a correlation. To do that please click on a little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file, simple currency correlation, 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 ask price for the current currency pair that can be done by using symbol info double for the current symbol on the chart, we use symbol ask and underscore digits and normalize double to calculate the current ask price and the right number of digits behind the dot. So far so good! But now we want to know the ask price for another currency pair. And to calculate that one I do almost the same except that I have hard-coded the currency pair for the first parameter. To make that work for your account you need to check if the currency pair is exactly shown as we have hard-coded it because some account type might use suffixes, for example, my RoboForex cent account would show the suffix dot m and if that is the case you would need to hard-code it here. Please notice this is just an example, usually, you wouldn’t hard code anything but in this little video, we just want to show the concept. So let’s continue with the simple moving average. First, we need to create an array, afterwards, we create a definition for the moving average by using the ima function for the current symbol on the chart – I have also hard-coded the time period here, I use period underscore m1, if you mark that and press F1 you will see that this one stands for one minute, half an hour would be period m thirty and so on. I want the calculation for the result based on the last twenty candles for the current candle without any shift values, it should be a simple moving average so I use sma and the result should be calculated based on the close price. Now we use copy buffer to fill the moving average array according to the definition that we have created here for buffer zero, that’s this line here, we start with the current candle zero and we want to copy data for three candles and store it in our array. And to calculate the value for the current candle we just have to look inside of the value for candle zero in our moving average array and assign it to our variable my moving average twenty. A little hint here, I usually used a float type to save some memory and to cut a few digits behind the dot but that might cause a little problem after one of the recent updates I would recommend to stick with the original variable types, so this is how we calculate the moving average array for the current currency pair and now we are going to repeat that for the second currency pair, it’s very similar but this time we are going to hard-code the currency pair as we did for the ask price, everything else is the same except for the variable names. And once again to calculate the current value for the Australian against the US dollar we look into the value for candle zero in our moving average array. Finally, we want to create a chart output by using the comment statement, this one will output the ask price for the current currency pair and the ask price for the second currency pair followed by the current moving average value and the moving average value for the second chart. 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 or maybe even the premium course on our website might be interesting for you. By the way, this was again a question from a premium member and if you are already a premium member and want to make a suggestion for one of these videos, please let me know. For now,
    9 min
  • MQL5 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 an array, in this case for the momentum indicator, 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 momentum monitor, 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 this time we are going to include this file, it’s for math functions, this file math dot mqh will help us to calculate the average value for an array, it includes additional functions that are not part of the mql5 core language. We start by creating a string variable called current momentum state, but we are not going to assign any values here. Afterwards, we are going to create an array for the prices, we use array set as series for the price array to sort it from the current candle downwards and now we use the imomentum function that is included in mql5 to create the value for the momentum indicator, for the current symbol on the chart, the current period on that chart, the value should be calculated based on fourteen candles and it is calculated based on the close price. When you click on insert, indicators, oscillators, momentum, you will see the same values here and this is how the indicator looks like. Well, the problem with these kinds of indicators is that they do not have a fixed upper or lower value and we would like to know if the current value is above the average value, so how do we do that? Well first we want to use copy buffer to fill our price array with data according to the imomentum definition that we have created here, we do that for the only buffer or line that the indicator has, we start with candle zero and we want to copy the prices for fourteen candles. And now we calculate the current momentum value by looking into our price array and taking the price from candle zero. Well, that was simple. To get the average momentum value you would usually now need to go through all the prices inside of the array, add each price to the value and divide the result by fourteen because that’s the number of elements inside of the array. Instead of doing that we use a predefined function that comes with the math library and this function is called math mean, you might notice that this function is not highlighted. The other ones have a different color and the reason is that this one is not part of mql5, instead, it’s a sub-function that comes with a standard library and it calculates the mean value of array elements, in this case, it will calculate the average momentum value for our price array and if the current momentum value is bigger than the average momentum value that’s when we assigned the value, momentum is above average to our current momentum state. Otherwise, if the current momentum value is below the average momentum value we would assign, momentum is below average to our current momentum state. So let’s create a chart output for the current momentum value, the average momentum value and the current momentum state, and that’s about it. Well, if you mark math mean and press F1 you will see that there is no entry in the current reference for mql5, you will find other functions for the maximum or the minimum value, on a website we can learn that it is a standard library, mathematics, statistics, sub-functions, so let’s try that. We navigate from mql5 reference to math functions, standard library, sub-functions, and here we are, math mean is included, so it’s possible to find it once you know that it exists. If you are interested you can look at all the other functions that come with the math library and if you don’t understand what all the code here does or if this was too fast for you you maybe wan...
    8 min
  • MQL5 TUTORIAL BASICS – 67 SIMPLE VERTICAL LINE
    In this video we are going to draw vertical lines whenever we open a sell trade, this one is because we have calculated that the last candle was the highest candle, 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 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, we start with an include statement to include the file trade dot mqh that contains a class called ctrade and we are going to create an instance called trade. Inside of the ontick function we start by calculating the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Let’s create an array for price information that can be done by using the function mql rates, now we sort the array from the current candle downloads by using array set as series for our price information array and with copy rates we fill our array for the current symbol and the currently selected period with price data starting from the current candle zero. We want to copy the price data for one hundred candles and store it in our price information array. I would like to sell when we have the highest candle out of the last one hundred candles, so let’s create a variable called highest candle and we also need to create an array for the highest candle prices. Let’s use array set as series for that array to also sort this one from the current candle downwards and to get the highest prices we use copy high for the current symbol on the chart and the currently selected period on that chart to copy the high prices for each candle starting with candle zero for one hundred candles and we store those values in the high array. That makes it possible to get the highest candle by using a function called array maximum, this will give us the largest element in our array in our case that’s the number for the highest candle and if the highest candle on the chart is the current candle and if positions total delivers a return value of zero that’s when we want to sell so we now use trade dot sell to sell ten micro lot. Afterwards, I would like to use a user-defined function to draw a vertical line but before we are going to code it let’s add a comment statement to output the text, highest candle, followed by the number of the highest candle out of the last one hundred candles that is the output that you can see right here. Now let’s find out how to draw that vertical line! Our user-defined function will have no return value so we use void here, it doesn’t take any parameters and we start by using object delete for the current symbol on the chart to delete the formal line that we have drawn because we only want to see one line on the chart, the other lines have been deleted. Afterwards, we use object create for the current symbol on the chart to create an object that is called line, the type of the object is obj underscore v line for vertical line, if you mark that and press F1 you will see that there are lots of other object types here, the object will be drawn in our main chart, we use time current because that will give us the time of the last received tick or price change if you want, if you look inside of the mql5 reference you will see all the parameter descriptions here and we can change some object properties, so let’s do that for the current color, we use object set integer on the main chart zero for an object called line, the property for the color and set it to clr magenta. If you press F1 you will see that there are lots of predefined colors. Let’s also change the object width, in my case, I will set it to one and that’s about it.
    8 min
  • MQL5 TUTORIAL BASICS – 66 SIMPLE SHIFTED EMA
    In this video, we are going to use shifted exponential moving averages that are drawn into the future to find out if we have a buy or sell signal, 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 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 an include statement to include the file trade dot mqh. Afterwards, we create an instance of the class ctrade that will be called trade and we are going to use it to open and close positions. Let’s create three user-defined input variables, one for the number of candles that we are going to use to calculate the moving averages and two shift values, those shift values here are used to draw the exponential moving average on the right side of the current price, it looks as if they can predict the future but actually they are just moved to the right side. Inside of the ontick function we want to calculate the ask price and the bid price by using symbol info double for the current symbol on the chart, we use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. Afterwards, we create a string variable called signal for our signal, please don’t assign a value here because we are going to calculate that now. Let’s create two arrays, one for the first moving average and another one for the second moving average, afterwards, we are going to use array set as series for both moving averages to sort them from the current candle downwards. Let’s define the properties that is done by using the ima function for the current symbol on the chart and the currently selected period on that chart. We are going to calculate the number of candles that we have defined here, in our case twenty candles, the result for the moving average will be shifted by twenty candles, we use mode underscore ema for an exponential moving average that is calculated based on the close price and here we repeat the same thing for the second moving average, that’s the exact same number of candles but this time we are going to use another shift value, in our case one hundred candles. And now we are going to fill the array with data according to the moving average definition that we have created here, we do that for the first buffer starting with candle zero for three candles and store the result in the moving average array. Let’s repeat that for the second moving average except for those two variable names everything is the same and now we want to check if the price is above both moving averages, so if the ask price is bigger than the value for candle zero in the first moving average array and if the ask price is also above the value for candle zero in the second moving average array that’s when we have a buy signal so we assign the word buy to our signal. If the bid price is below candle zero for the first moving average array and if the bid price is also below candle zero for the second moving average array that’s when we have a sell signal and now we assign sell to our signal and when our signal equals sell and positions total delivers a value that is below one that would mean we have a sell signal and no open positions and in that case, we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot. Finally, we want to create a chart output that is done by using the comment statement, this will output the text, the signal is now 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 w...
    9 min
  • MQL5 TUTORIAL BASICS – 66 SIMPLE SHIFTED EMA
    In this video, we are going to use shifted exponential moving averages that are drawn into the future to find out if we have a buy or sell signal, 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 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 an include statement to include the file trade dot mqh. Afterwards, we create an instance of the class ctrade that will be called trade and we are going to use it to open and close positions. Let’s create three user-defined input variables, one for the number of candles that we are going to use to calculate the moving averages and two shift values, those shift values here are used to draw the exponential moving average on the right side of the current price, it looks as if they can predict the future but actually they are just moved to the right side. Inside of the ontick function we want to calculate the ask price and the bid price by using symbol info double for the current symbol on the chart, we use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. Afterwards, we create a string variable called signal for our signal, please don’t assign a value here because we are going to calculate that now. Let’s create two arrays, one for the first moving average and another one for the second moving average, afterwards, we are going to use array set as series for both moving averages to sort them from the current candle downwards. Let’s define the properties that is done by using the ima function for the current symbol on the chart and the currently selected period on that chart. We are going to calculate the number of candles that we have defined here, in our case twenty candles, the result for the moving average will be shifted by twenty candles, we use mode underscore ema for an exponential moving average that is calculated based on the close price and here we repeat the same thing for the second moving average, that’s the exact same number of candles but this time we are going to use another shift value, in our case one hundred candles. And now we are going to fill the array with data according to the moving average definition that we have created here, we do that for the first buffer starting with candle zero for three candles and store the result in the moving average array. Let’s repeat that for the second moving average except for those two variable names everything is the same and now we want to check if the price is above both moving averages, so if the ask price is bigger than the value for candle zero in the first moving average array and if the ask price is also above the value for candle zero in the second moving average array that’s when we have a buy signal so we assign the word buy to our signal. If the bid price is below candle zero for the first moving average array and if the bid price is also below candle zero for the second moving average array that’s when we have a sell signal and now we assign sell to our signal and when our signal equals sell and positions total delivers a value that is below one that would mean we have a sell signal and no open positions and in that case, we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot. Finally, we want to create a chart output that is done by using the comment statement, this will output the text, the signal is now 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 w...
    9 min
  • 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