MQL5 TUTORIAL BASICS – 63 SIMPLE CHECK FOR NEW BARS

video
play-sharp-fill

In this video we are going to create a new expert advisor that is able find out if we have a new candle on the chart. Each time a new candle appears the text here changes, we also have a bar counter, so let’s find out how to do that with mql5.
To get started please click on a little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple check for new bars, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start by calculating the current candle number or the number of bars on the chart.
Bars and candles what you see on your chart depending if you use the candlestick view or the bar view and we can use a function that is included in mql5 that is called, bars. It takes two parameters, the first one is for the current symbol on the chart, the second one is for the currently selected period on that chart and it will return the number of bars in the history for a specified symbol and period.
Now we continue and create a string variable, this one is called, new candle appeared but we don’t assign a value here.
In the next step we want to find out if we have a new candle and to find out we call a function called, check for new candle and we will pass the candle number that we have calculated here.
Well, this function doesn’t exist so far so we need to code it in a few seconds.
Inside of the ontick function we want to create a comment statement that will output the text, bars on chart followed by the current candle number and in a new line we will see the text, new candle appeared followed by the return value of the function that we need to create now.
Well, our user-defined function will be called, check for candle and it will take the current candle number that we have calculated as a parameter.
And to find out if the number of candles on the chart has changed we create a so-called static integer variable here. If you mark that and press F1 you will learn that a static variable will hold its value as long as a function exists and we use it to store the last candle number inside of this function.
Let’s create another string variable for the return value, this one is called, is new candle and the default value is that we have no new candle but if the current candle number is bigger than the one that we have for the last number of candles that is when we set the value for the new candle to, yes, a new candle appeared. And now we assign the current number of candles to the variable that is called, last candle number because this is what we need when we call our function for the next time.
Finally, we need to return the calculated value for the new candle to our main function.
This is basically it.
Well, if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic video series or maybe even the premium course on our website might be interesting for you.
Actually, this was a question from one of our premium course members so if you already are a premium course member and if you have an idea for a video like this one, just send me an email.
For now, we click on the compile button and if you don’t have any errors you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader you want to click on view, strategy tester or press control and r, please pick the new file simple check for new bars dot ex5, please check the visual mode here and start a test.
Here we are! Our little expert advisor is running and you can see that the number of bars is changing and each time we have a new bar or candle on the chart, we can see here the output changes and in this little video, you have learned how to create an expert advisor for Metatrader 5 that is able to use a user-defined function to find out if we have a new candle on the chart and you have coded it yourself with a few lines of mql5 code.