MQL5 Tutorial – Advanced Trading Range Calculation

video
play-sharp-fill

 

In this video we are going to create an Expert Advisor that can define a trading range so what is a trading range? You see the two lines here and the upper line is above the highest candle of the last one hundred candles and the lower line is below the lowest candle of the last hundred candles and here we have a very narrow range, it is calculated here and it says it’s now about 0.1659 and we could wait until the range is above one if we just want to trade a breakout. Now let’s see what happens if the price changes. I will speed it up a little bit here, now you can see our range grow, it is 0.37 right now and now it’s above one- that’s a really big candle here. And if you like to trade volatility maybe you want to learn how to calculate something like this, now how can we create an Expert Advisor that is able to calculate a trading range?

To do that please click on the little button here in Meta Trader or press your F4 key and that will bring up the Meta Editor, and we need to create an mq5 file with the name Advanced Trading Range, it has only one function called Void OnTick, here we are going to create a variable for our trading range. It’s a double variable, now we create a variable for the highest and the lowest candle called highest candle and lowest candle. This is an integer variable because it’s the number of the candle. And we need to arrays, these are double arrays called high and low, and we sort both arrays downwards from the current candle by using array set as series for the array with the name high and array set as series for the array with the name low. And now we want to fill our array with data for one hundred candles; the high array will be filled by using Copy High for the current symbol and the current period from the current candles zero for one hundred candles, and we need the highest points of the candles. And with Copy Low for the current symbol on the chart and the current period from the current candles zero for one hundred candles we now need to know the lowest value for one hundred candles.

Let’s actually calculate the highest candle that is done by using array maximum for the array with the name high from the current candle zero for one hundred candles. And to calculate the lowest candle we use array minimum for the Array Low from the current candles zero for one hundred candles. Let’s create another array for price information, it will be called Price Information and we create it by using mql rates. We start it from the current candle downwards by using array set as series for our array price information we have created here and with copy rates, we copy price data into our array for the current symbol and the current period from the current candle for all the bars that are visible on the chart and the result will be stored in our array called Price Information that we have created here. So let’s create the upper line first we set the object properties for the upper line, that is done by using Object Create for the current symbol, it will be called line one. We use OBJ_HLine because it’s a horizontal line and this zero stands for the window we are going to use, in this case it’s the main window, some indicators us a window below the main window here and in that case we would use another value. The last two parameters are for the date/time and for the price we also use zero for the date/time and this expression here will give us the highest candle of the price information and from that candle we want to know the highest point. These two lines set the object colour and the object width and with Object Move for the current symbol and the object with the name Line One the rest of the parameters here equal the same values we have used above.

We want to actually move the line every time our trading range changes. And for the lower line we actually do the same, the only difference is that our object now has the name line two and instead of the highest point we use the lowest point for the lowest candle instead of the highest point for the highest candle that we have used for line one. And to calculate our trading range we take the highest high of the last one hundred candidates minus the lowest low of the last one hundred candles.

In the last step we will use the comment function to output the text “The current trading range is… ” followed by the calculated trading range. Now let’s compile the code and that worked without any errors so we now click this little button here or press F4 on our keyboard to go back to Meta Trader, and in Meta Trader you want to click on View Strategy Tester or press control and R on your keyboard and here you want to select the advanced trading range.ex5 file and enable the visualization option here and start your test.

And this is how it looks like, right now are trading range is above four. I will zoom in a little bit and now you can see it shrink and when it falls below one we don’t want to trade because now we are in a sideways market. Okay now you know how to create an Expert Advisor that is able to calculate a trading range and you have done it yourself with a few lines of MQL 5 code.