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.