Sometimes it is helpful to count the new candles on the chart.
The output in the left upper corner on the chart says we had four candles since we started this Expert Advisor,.
One, two, three, four, this is the fifth one.
Now you might ask yourself why is this important?
Well an Expert Advisor can trade several positions on a chart, and you also can trade several currency pairs and every single candle on the chart can have lots of different price moves.
And every time a price changes the OnTick function will be triggered but a lot of trading strategies use close prices.
So instead of maybe fifteen calculations within this candle, you would only need one calculation for the close price.
Even a high tech computer may have a resource problem, if you try to calculate several dozen trades on several dozen charts because when the market starts to move really fast your computer might be overwhelmed and it may cause a crash.
So if you want to avoid computer crashes, it’s a good idea to save resources.
To do that you can simply check if a new candle is created and now we will create a simple little Expert Advisor to do that.
So let’s click the little button over here or hit the F4 key to bring up the Meta Editor.
Select File, new Expert Advisor from template, continue, and we will give it the name Simple New Candle.
Now click on continue, continue, finish, and remove everything above the OnTick function.
Let’s delete the two comment lines here. Okay now the first thing we need is the price array to hold our price data.
So let’s create that, that can be done with MQLRates and we will call a price data.
Now we want to sort our array from the current candle downwards with arraysetasseries and in the next step we need to fill it with data.
That is done by using copyrates for the current currency pair on the chart and the current selected period.
For example I prefer a one minute charts, we want to start with the current candle zero, and copy the price for three candles into our little array price data.
To count the number of new candles since the last start of the Expert Advisor, we need a new candle counter.
This candle counter will be an integer and it will be static, that means it will never lose its value even if we would like to leave the function.
Okay let’s move on, now we are creating datetime variable that will hold a time stamp; this one will hold the time stamp for the last time we checked and it is also static so it will never lose its value.
And for the current candle we need another datetime variable and that will be called time stamp current candle and we will fill it by getting the time value of the current candle from our price data array.
And if the time stamp for the current candle is different from the timestamp the last time we checked, we want to remember the current timestamp for the next time by assigning it to the timestamp last check variable.
Now we want to add the value one to our current candle counter and the last thing we need us the output on the chart.
So let’s compile the little Expert Advisor here and we have zero errors and zero warnings.
So let’s hit the F4 key or click this button to go back to MetaTrader, select View Strategy Test or hit the control and the R key. Look out for the simple new candle.ex5 file.
Mark the visualization option and click on start.
And now you should see your candles on the chart being counted and that can be done very fast and you did it in five minutes by creating this little Expert Advisor here.
Download “MQL5 Simple New Candle Expert Advisor from MQL5 Tutorial”SimpleNewCandle.txt – Downloaded 420 times – 1 KB