MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 84 SIMPLE BOLLINGER BANDS SELL TRAILING STOP
    In this video we are going to create an expert advisor that is able to move a sell trailing stop for the upper bollinger band, 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 bollinger bands sell trailing stop, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start with an include statement to include the file trade dot mqh, this one will give us some simple trading functions, it contains the class ctrade and we are going to create an instance of ctrade that will be called trade. Inside of the ontick function, we want to create a static double variable, called last stop bollinger bands value. We are going to use it later to move our trailing stop and compare the current value with the last value. We also need 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 underscore bid and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot depending on the currency pair that might be three digits or five digits. And to move our sell trailing stop we need to have a test position so if positions total is less than one we use trade dot sell to sell ten micro lot and now I will set the last stop bollinger bands value to an unrealistic high value of one thousand, we also could set it to zero here but we would need to add another condition later on and for this simple example it is good enough. Now let’s create an upper band array and a lower band array. Actually, we have a middle band but we don’t need it, so let’s use array set as series for the upper and the lower band to sort both arrays from the current candle downwards. Now we can define the bollinger bands by using the included ibands function for the current symbol on the chart and the currently selected period on that chart, we want to calculate it for twenty candles, don’t use a shift value, the deviation value is two and we are going to calculate the results based on the close price. With copy buffer we can now fill our array according to the bollinger bands definition that we have created here, we will do it for buffer one and buffer two. Buffer one is for the upper bend array, we start with candle zero and we want to copy the values for three candles and store them in the upper band array or in this case store them in the lower band array and that makes it possible to calculate the last upper band value and the last lower band value by just looking at candle one in the upper and in the lower band array. Let’s continue with the conditions because if my last upper band value is above the ask price and if my last upper band value is below the last stop bollinger bands value that is when it’s time to check the bollinger sell trailing stop, we are passing the parameter my last upper bands value here and afterwards, we assign the last upper band value to the variable last stop bollinger bands value because the next time we call the function the current value will be the last one. Now let’s continue with the user-defined function, we are using void because we don’t need a return value, the name is check bollinger sell trailing stop, this is the parameter that we have passed and now we use a for loop to go through all the open positions. Position get symbol will give us the currency pair for the current position. Now we need to check if the current symbol on the chart is the same symbol as the position symbol and maybe we should close the ontick function here, so let’s continue with the position ticket, we will get that by using position get integer and the parameter is position underscore ticket.
    9 min
  • MQL5 TUTORIAL BASICS – 83 SIMPLE BOLLINGER BANDS TRAILING STOP
    In this video we are going to create an expert advisor that is able to calculate a buy trailing stop for the bollinger bands indicator, 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 bollinger bands buy trailing stop, 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 include the file trade dot mqh, this one comes with mql5 and it contains the class ctrade and we are going to create an instance from ctrade that is called trade. Inside of the ontick function, we want to create a static double variable, called last stop bollinger bands value. Afterwards, 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 ask or symbol bid and we use normalize double and underscore digits to automatically calculate the right number of digits behind the dot. For a trailing stop we need to have an open position, so let’s create a test position, whenever positions total is below one we use trade dot buy to buy ten micro lot and we set the value for the last stop bollinger bands value to zero. Now let’s create an array for the upper band array and for the lower band array. Actually, the bollinger bands have three arrays for the upper, the middle, and the lower band but in this case, we only need the two extremes. Let’s use array set as series to sort both arrays from the current candle downwards. Now we can define the bollinger bands by using the integrated ibands function for the current symbol on the chart and the currently selected period, we want to calculate the result for twenty candles, we use no shift value, the deviation value is two and we want to calculate the results based on the close price. Now we use copy buffer to copy the data according to the bollinger bands definition that we have created here, we use buffer one for the upper bend array and buffer two for the lower band array starting from the current candle zero and we want to copy the values for three candles and store them in the array. And now we can calculate the last candle by looking into candle one of our upper band array or our lower band array, this really depends on if you are looking for a buy or a sell trade. For a buy trade, we want to check if the last lower band value was below the current bid price and we also want to know if the current value is above the last bollinger bands value, and if this is true we want to call a function called, check bollinger buy trailing stop and we pass the value my last lower band value. Afterwards, we are going to assign the last lower band value to the last stop bollinger band value. So far so good but this function doesn’t exist so far, so let’s create that now! We are using void because we have no return value, the name of the function is check bollinger buy trailing stop and we pass the last lower band value here. Now we use a for loop to go through all the open positions, with position get symbol for the current counter value we will get the position symbol and that makes it possible to check if the current symbol on the chart equals the position symbol, if that is true, we use position get integer, position underscore ticket to get the current position ticket number and with position get double for the current stop loss – the parameter is called position underscore sl – we get the current stop loss and only if the current stop loss is below the last lower band value we use trade dot position modify for the current position ticket to move the current stop loss to the last lower band value, this parameter is for the take profit and we will leave that one unchanged. Finally, we want to close the if loop,
    8 min
  • MQL5 TUTORIAL BASICS – 82 SIMPLE SMA SELL TRAILING STOP
    In this video we are going to create an expat advisor for a simple moving average sell trailing stop, 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 sma sell trailing stop, 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 include the file trade dot mqh, this one comes with mql5 and it provides trading functions, it also contains the class ctrade, so we are going to create an instance of that class called trade. We start by creating a static variable for the last stop moving average value, now 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 and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. To use a stop loss we need to open a test position, we are going to use trade dot sell to do that and the condition is whenever positions total is below one – that means we have no open positions – and that’s when we want to sell ten micro lot. We also want to set the last stop moving average value to a very high value, actually, you could also set it to zero but you would need to add another condition because usually, we are going to check if the current value is lower than the last stop moving average value and that one will never be below zero so let’s go with the high value for now. In the next step, we are going to create an array for the moving average. With array set as series, we are going to sort the array from the current candle downwards and now we can use the integrated ima function for the current symbol on the chart and the currently selected period on that chart to create a simple moving average for five hundred candles, we don’t use a shift value here, we are going to use mode underscore sma as it stands for simple moving average and we want to calculate the results based on the close price. Let’s continue with copy buffer, this one is used to fill the array according to the moving average definition that we have created here, we will grab the values for buffer zero that’s the moving average signal line here, we will start with candle zero – that’s the current candle – and we will copy the values for three candles and store them in the array. And now that we have done that we can calculate the stop moving average value by looking into candle one of our moving average array. The first condition we want to check to move a sell stop loss is if the current stop moving average array is above the ask price otherwise we would not be able to change it. We also want to know if the current stop moving average value is below the last stop moving average value, and if this is true we are going to use a function called check sma sell trailing stop and pass the bid price and the stop moving average value as a parameter. This function doesn’t exist so far, so we need to create it now and please don’t forget to set the last stop moving average value and assign the current stop moving average value because the current one will be the last one the next time we call this function. Our trailing stop function will use void because we don’t need to return anything, the name is check sma sell trailing stop and we pass the bid price and the stop moving average value as parameters here. Now we use a for loop to go through all the open positions, we will count down until there are no other positions left, afterwards we use position get symbol for the current counter of the for loop to get the currency pair symbol for the current position and only if the symbol on the chart matches the currency pair symbol we are going to continue.
    9 min
  • MQL5 TUTORIAL BASICS – 81 SIMPLE SMA BUY TRAILING STOP
    In this video, we are going to create an expert advisor that is able to create a a simple buy trailing stop that is based on a simple moving average value, 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 sma buy trailing stop, 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 include the file trade dot mqh, this one comes with mql5 and provides simplified trading functions afterwards we create an instance of the class ctrade and call it trade. Inside of the ontick function we start with a static double variable called last stop moving average value, we continue by calculating the ask price and the bid price that can be done by using normalize double, symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask or underscore bid, with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot. To have something that we can use for the trailing stop we need to open a test position, so whenever positions total is smaller than one or in other words when we have no open position we use trade dot buy to buy ten micro lot. That will open a new position and that’s when we set the last stop moving average value to zero because for each position we need a new value. Let’s go on and create an array for the moving average, now we want to sort the array from the current candle downwards that is done by using array set as series for our moving average array that we have created here. Now let’s define the properties, we want to use the ima function for the current symbol on the chart and the currently selected period on that chart, we want to use it for five hundred candles starting from the current candle zero, we don’t use a shift value and we use mode underscore sma because this stands for simple moving average and the calculation should be done based on the close price. Afterwards, we use copy buffer to fill our moving average array according to the definition that we have created here for buffer zero, we start with candle zero – that’s the current candle – and we copy the values for three candles and store them in the array. And to get the value that we need we simply have a look at the value for candle one in our moving average array and we assign the value to stop moving average value. Now that we have done that we can check if the stop moving average value is below the current bid price and the second condition will be if the stop moving average value is bigger than the last stop moving average value and if that is the case we want to call a function that is called check sma buy trailing stop, we will pass the ask price and the stop moving average array value as parameters here and afterwards we want to assign the current value for the next function call, so we store the value for stop moving average value in the variable called last stop moving average value. Please remember this was a static variable so it will keep the values inside of this function, well this function doesn’t exist so far, so we need to create it now, the name of the function will be check sma buy trailing stop, we use void because we don’t use a return value here and we pass the ask price and the stop moving average value as parameters. Now let’s use a for loop to go through all the open positions, we use position get symbol to get the symbol for the current position because we need to check if the current symbol on the chart is equal to the position symbol and if this is true we get the position ticket by using the function position get integer position underscore ticket. To get the current stop loss we use position get double and the parameter i...
    9 min
  • MQL5 TUTORIAL BASICS – 80 SIMPLE SPREAD CHECKER
    In this video, we are going to create an expert advisor that can calculate the highest and the lowest spread for a certain number of candles in a currency pair, so let’s find out how to do that with mql5. To get started please click on the 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 checker, 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 input value for the candles to count, in my case I want to use five candles and find the highest and the lowest spread for those five candles, this input modifier here will allow us to change the value without recompilation by changing the expert properties. Inside of the ontick function we start with a double variable called lowest spread value and in this case, I’m going to set the initial value to one hundred thousand, this is definitely not the lowest spread value that you will see but we are going to calculate that later. Let’s create another variable, this time for the highest spread value, I will set the initial value to zero and this is also not the highest spread value that you will see. Afterwards, we use mql rates to create an array for price information. Mql rates stores the information about prices, volumes and spread and spread is what is interesting for us. Afterwards, we use array set as series to sort the array from the current candle downwards and now we fill the price array with data by using copy rates, in my case for the current symbol on the chart and the currently selected period on that chart, I’m going to start with the current candle zero and I will copy the data for two hundred candles and store it in the price information array. Now we can use a for loop to go through all the candles that are interesting, the current candle number will be the number of candles to count that we have defined here, in our case five and for as long as the current candle number is bigger or equal to zero we will go through the for loop and count down the number of candles until this condition is no longer true, and to get the current candle spread we look inside of the price information array for the current candle number and we want to know the spread, so we use dot spread. To make it easy to check if the values are correct we want to add a print statement that will output the current value for the candle, the candle number we are processing right now and the current candle spread in our journal and if the current candle spread is lower than the lowest spread value, we want to save it as the lowest spread value. In the other case if the current candle spread is higher than the highest spread value that’s when we want to save the current candle spread as highest spread value. Finally, we want to create a chart output by using the comment statement, this will show us the lowest and the highest spread on the chart and finally, we want to end the for loop and close the ontick function. 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, this was another question from a premium course member and if you are already a premium course member and have an idea for a video like this one please send me an email. So far so good! Please click on the compile button or press F7 on your keyboard, I don’t have any errors and if this is the case you 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 ctrl and r, please pick the new file, simple spread checker dot ex5, mark the option for the visual mode and start your test. And here we are!
    7 min
  • MQL5 TUTORIAL BASICS – 79 SIMPLE COLOR CHANGER
    In this video, we are going to create an expert advisor that is able to change the colors for several objects on the chart, so let’s find out how to do that with mql5. To get started please click on the little icon here or press the F4 key 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 color changer, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by creating an array, we will use mql rates to get the price information because mql rates is a structure that stores the information about price, volumes and spread. Afterwards, we use array set as series to sort our price information array from the current candle downwards and with copy data, we can fill our price information array with data for the current symbol on the chart, we are going to use the currently selected period on that chart and we will copy the data starting from candle zero for three candles and store it in our price information array. Now that we have done this we are able to calculate the close price for candle one that is done by looking into the value for candle one in the price information array. We are interested in the close price, so if you cut that out and press the period button on your keyboard you will see that you can get close, high, low or open prices, we will go with the close price. Afterwards, we do the same for the close price of candle two. Everything else is similar but this time we are going to look for the close price of candle two in our price information array and if the close price for candle two is below the close price of candle one we want to change the colors on the chart that is done by using chartsetinteger. You probably would expect something like chartsetcolor, so this is a little strange. We will do that for the current chart and the property we are going to use change first is chart color candle bull, I will set it to green, so I use clr green here and if you mark that and press F1 you will see that we can use a lot of different predefined colors here and this will change the body color for the bull candles. Let’s do the same thing here for the bear candles. Everything is pretty much the same except for the property that we are going to change, this time is chart color candle bear and now we use chartsetinteger to change the property for chart color chart up. This is for the outer color of the bull candles and I’ll also change this one to green and repeat the whole thing and do it for the bear candles by using chart color chart down. The next one is for the chart mode and here we can pick one of three values. Chart mode will change the type, we can have candlesticks, bars or line chart, we can pick one of these values and I decided to go with a bar chart. I want to have a grid on the chart, so I use chartsetinteger to set the property for chart show grid to true. Let’s change the foreground color for the data by using chart color foreground and I will set it to yellow here and finally, I would like to set the background color, so I use chart color background and set it to black. In the other case when the close price for candle two is above the close price for candle one I would like to see other colors. This is basically the same stuff so I will use copy and paste here, this time we will see the color red for the candles, that’s also what I use for the chart mode, I would like to see candles on the chart, I don’t want to see the grid, so I set the property to false. The chart color foreground should be magenta and the chart color background should be white. But you can not only set values for the chart properties it’s also possible to read values. In our case we will read the max price and the min price, that is done by using chart get double for the current chart and the properties are chart price max and chart ...
    8 min
  • MQL5 TUTORIAL BASICS – 78 SIMPLE MULTI CURRENCY ORDER
    In this video, we are going to create an expert advisor that is able to open positions for multiple currency pairs in the back-test, so let’s find out how to do that with mql5. To get started please click on the little icon here or press the F4 key 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 multiple currency order, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We will start with an include statement to include the file trade dot mqh, it contains the class ctrade and we create an instance called trade that we are going to use to open positions later on. Inside of the on tick function, we start by calculating the ask price 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 make sure that we calculate the right number of digits behind the dot. Well as we are going to trade several currencies we also need ask prices for the other currencies, so let’s calculate the ask price for the GBPUSD. We also use symbol info double but this time the first parameter is not underscore symbol but the name of the currency pair. You might have different names, so please make sure that you spell that correctly. Now let’s repeat the whole thing, this time I would like to calculate the ask price for the currency pair USDCAD, so that’s what we use here and of course, we need to repeat the whole thing for the bid prices that is done by using symbol info double underscore symbol and symbol bid for the current currency pair and please change it here for the currency pairs you want to trade. Now let’s create a string variable called signal, so far we don’t assign any value here because we are going to calculate that now. For this simple example I am going to use a random entry signal, so let’s use mathsrand to create an initial value for the random calculation. We use gettickcount as a parameter because this function returns the number of milliseconds since we have started the system, so this will create a base value that we use to create our random values. And this is done by using mathrand we just need two values, so we use percent two and if the random number that we have calculated equals zero we would assume that this is a buy signal, so we assign the word buy to our signal. Otherwise, if the random number equals one that would be a sell signal and now we assign the word sell to our signal and whenever we have a sell signal and positions total returns a value that is below ten we would use trade dot sell to sell ten micro lot for each of our currency pairs here. This expression here stands for the current currency pair, you could also use underscore symbol like we did here, basically, it stands for no value. Let’s repeat the whole thing for the buy trades, so if our signal equals buy and positions total is below ten we use trade dot buy to buy ten micro lot either on the current chart or for the two other charts that we have defined. And that’s about it. If you have no idea 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 the basic video series first or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, I didn’t get 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, we click on view, strategy tester or press control and r, please click on the file, simple multiple currency order dot ex5, mark the option for the visual mode and start a test. Here is our expert advisor! We already have lots of orders and as you can see we have orders in different currency pairs in the strategy tester,
    7 min
  • MQL5 TUTORIAL BASICS – 77 SIMPLE AWESOME INDICATOR
    In this video, we are going to create an expert advisor that is able to trade the awesome indicator with the user-defined buy and sell value, so let’s find out how to do that with mql5. To get started please click on this little symbol here or press the F4 key 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 awesome indicator, 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. To do that we create an instance of the class ctrade, this is called trade. In the next step, we are going to create an input modifier for a float variable, the variable will be called signal value and this is the value that we are going to use for buy and sell trades, we are able to modify that later in the expert advisor settings without recompilation. Inside of the on tick function we need to calculate the ask price and the bid price, this is done by using symbol info double for the current symbol on the chart we either use symbol underscore ask or a symbol underscore bid and with normalize double and underscore digits we can automatically calculate the right number of digits behind the dot. Now let’s create an empty string for the signal but we are not going to assign any value here because that needs to be calculated. In the next step, we use mqlrates to get some price information because mqlrates stores the information about price, volumes and spread. Let’s sort the price information array downwards by using array set as series and now we use copy rates to copy some data for the current symbol and the currently selected period, starting from candle zero for three candles and store it in the price information array. We need a second array for the indicator values, let’s call that one price array and we use the included function iao that comes with mql5 to get the awesome oscillator definition for the current symbol on the chart and the currently selected period on that chart. Now let’s use array set as series also for this price array and now we can use copy buffer to fill our price array according to the awesome oscillator definition that we have created here, we do it for buffer zero, we start with candle zero – that’s the current candle – and we store the price information for three candles in our price array. Now let’s get the current awesome oscillator value that is done by looking into the value of candle zero inside of our price array and I use normalize double and I want to have six digits and that’s because this is also what we see on the original value here for the oscillator, so let’s find out if we have a buy signal that would be the case if the current awesome oscillator value is bigger than the signal value that we have defined here and if this is the case we consider that to be a buy signal so now we assign the word buy to our signal. In the other case if the awesome oscillator value is below zero minus signal value, so this expression is going to give us the negative value and if the current awesome oscillator value is below that we would consider that to be a sell signal and now we assign the word sell to our signal. And if the signal equals sell and we have no open orders – that would be the case when the return value for positions total is below one – we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and we also have no open position we use trade dot buy and buy ten micro lot. Finally, we use the comment statement to create an output on the chart. For the current signal we use double to string for the current awesome oscillator value, otherwise, we would see exponential values on the chart and below that we will also see the curren...
    8 min
  • MQL5 TUTORIAL BASICS – 76 SIMPLE ACCOUNT INFORMATION
    In this video, we are going to create an expert advisor that is able to output account-related information directly on our chart. To get started please click on a little I can here or press F4 on your keyboard, now you should see the Metaeditor window until you want to click on file, new file, expert advisor from template, continue, I will call this file, simple account info, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start with getting the account server information that is done by using account info string and the parameter we are going to use is account underscore server, so in my case, the return value is MetaQuotes Demo for this account. Account info is available for string, for integer and for double values, so let’s continue with another string value. Account info string account underscore currency will return the currency that we are using, in my case I’m using Euro. Another string value is the account name, we can get that by using account info string account underscore name, my account name is Raimund Bauer. Now let’s continue with an integer value for the account trade mode, we get the integer value by using account info integer and the account trade mode is something that will return a value for the trade mode that is either zero, one or two, let’s see if we can get the values. Here is the reference entry for the trade mode and if you click on this little link you will see this can return demo, contest or real account but as the return value is an integer value it would be, zero, one or two. I would like to create a readable account trade mode version that can output a text and if the account trade mode equals zero I would like to see the text, demo account, if the return value for account trade mode is one that would be a contest account for trading contests, and if the account trade mode equals two that would be a real account. Now let’s continue with the account login, this is another integer value and it returns the account number. The next one is called account company, we get it by using account info string account underscore company, in my case, it’s the MetaQuotes Software Corporation. The account leverage is also very interesting, this again is an integer value, the parameter is account underscore leverage and in my case, it’s five hundred, actually, it’s 1 to 500, this is a very high leverage. The next one is also very interesting, it will tell us how many orders we can have, we get it by using account info integer account underscore limit underscore orders, in my case I can have 200 orders. Now let’s get the free account margin, this is the first time we use account info double, the parameter is account underscore margin underscore free, here is the output for this account. Let’s get two bool values, this looks a little bit strange because this is a bool but we are going to use account info integer to get it. The first one is for the account underscore trade allowed property, this is going to tell us if we are allowed to trade at all. And the second one is going to tell us if we are allowed using expert advisors and last but not least I would like to have the account margin mode, this is an integer value and it’s important because, for the type of automated trading that we do we need to have a hedging account, you can find out if your account is a hedging account by looking at this text, you should see the word hedge here. And we learn that zero stands for netting mode, a one would mean we have an exchange mode and two is the desired hedging mode, I can never remember that so let’s create a readable account margin mode. If the account margin mode equals zero I would like to see the output netting mode, for one it will output exchange mode, and for two this is the hedging mode. Now let’s create a chart output for each of the values, I have used the readable account trade mode and the readable account margin mode her...
    8 min
  • MQL5 TUTORIAL BASICS – 75 SIMPLE TIME FILTER
    In this video, we are going to create a filter for time-based entries 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, I will call this file, simple time 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 will give us simplified trading functions, so let’s create an instance of the class ctrade, we will call that one trade. Now let’s create a user-defined input variable, this is a string variable called start trading time. In my case I use 9:00 am and I would also like to have a stop trading time, this is set to 10:00 am and we will only be able to trade within the allowed trading hours and to that, we also need to create a string variable for the current time, and if the current time is not within the allowed trading hours we will set the variable, trading allowed to false, that’s also the default value here. Inside of the ontick function we use symbol info double for the current symbol on the chart to calculate the ask price that is done by using symbol underscore ask and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. The ask price is enough for this simple example because we are not going to use any entry signals to see if the first trade is actually opened at 9:00 am. Now let’s use time local, this will return the local time of a computer where the client terminal is running, so this is your current time but we are going to use a string value, so we take the time here and use timetostring for the current time to get time underscore minutes and that will give us the current hours and the current minutes and because we have used timetostring this will be a string variable so now we have the current time and we are going to use a function that is called check trading time and only if this one returns true our expert advisor is allowed to open any positions and if positions total returns a value of zero – that would mean that we have no open position – and this is when we are going to use trade dot buy to open a buy trade for ten micro lot just based on the time filter. You wouldn’t do that on a real account but for this simple example, it is good enough. We also want to add a comment statement, this will give us the result if trading is allowed or not, the current time, and the values for the start trading and the stop trading time. That’s it for the main function. So let’s create this customized function that we want to call, the return value will be bool, so it’s either true or false, the name of the function is check trading time and if we can find a substring that is equal to the start trading time inside of the current time that’s when we set trading is allowed to true. String substring will start with the very first character, you could use length equals minus one, in our case, I have set it to five and only when the start trading time is included in the current time we want to allow the expert advisor to open a new position. Otherwise, if the stop trading time is included in the current time that’s when we want to set trading allowed to false. Finally, we use the return statement to return the value for trading is allowed to our main function. Okay, 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. By the way, this was another idea of a premium course member so if you are a premium course member already and you have an idea for a video like this one please let me know. Now,
    8 min