In this video, we are going to create a channel object for a so-called deviation channel. Let’s find out how to do that with MQL5.
To get started, please click on the 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 SimpleDevChannel. Click on Continue, Continue and Finish. Now you can delete everything above the OnTick function, and let’s also remove the two comment lines here.
First, we want to calculate the number of candles on the chart. That can be done by using ChartGetInteger. For the chart id zero. We use CHART_FIRST_VISIBLE_BAR. The last parameter here is for the sub window. In our case, that is sub window zero. That’s the window with the candles.
If you like oscillators, you know that those are drawn in a separate window below the candles. But, we don’t want to do that, so we use zero here. Now let’s create a variable for the lowest candle, and we also need to create an array for the low prices for each candle. I will call this one low, and as you might have guessed, we also want to create a variable for the highest candle and the array for the highest prices called high.
Now we use ArraySetAsSeries for both arrays to sort them from the current candle downwards. Now let’s fill the low array by using CopyLow for the current symbol on the chart and the currently selected period on that chart. We will start with candle zero for all the candles on the chart and copy the lowest prices into the low array.
Of course, we need to repeat that for the highest prices. Therefore, we use CopyHigh. That will give us all the highest bar prices for the current symbol on the chart and the currently selected period, starting from candle zero for all the candles on the chart and the values will be stored in the high array.
To get the lowest candle on the chart, we use ArrayMinimum. That will give us the lowest element in the array, and we are looking for it in the low array, starting from candle zero for all the candles on the chart, and store the lowest candle number in this variable. That’s the number of the candle, not the value.
You might have expected that we also need to do that for the highest candle. This time we use ArrayMaximum, and we want to search inside of the high array, starting from candle zero for all the candles on the chart and store the result in the variable highest candle.
Let’s continue and use MqlRates to get all the price information that we need. It will create an array called price information, and we use ArraySetAsSeries to also sort this one from the current candle downwards.
Afterwards, we use CopyRates for the current symbol on the chart and the currently selected period, starting from candle zero, that’s the current candle, for all the candles on the chart and store the result in our price information array.
Let’s use ObjectDelete to delete any former objects for the current symbol on the chart. It will delete objects called simple channel object, and we will not have such an object on the very first time we code the function. But afterwards, we want to make sure that we delete the old channel before we draw a new one. That is done by using ObjectCreate.
Let’s go through all the parameters we need. The first one, _Symbol, is for the current currency pair. The second one is the name of the object. I have called it Simple Channel Object. The third parameter is the type of the object. In this case, we are using OBG_STANDARD DEVIATION CHANNEL. If you mark that and press F1, you will see that it is just one of the possible object types. It will draw a standard deviation channel object, right on the chart.
In the next parameter, we need to define that we want to have the object on the candle chart. That’s window number zero. I would like the object to be drawn for all the candles on the chart. I have to subtract one candle here because this is always a point in time, and these are values. As a value, I would like to add the lowest candle and the lowest price of that candle. That’s the first point.
The second point is the time where candle zero is drawn. Candle zero is the current candle on the chart. And again, I will pass the lowest candle and the lowest price here.
The next parameter is for the second line. I would like to have that drawn for all the candles on the chart. But this time I would like to pass the highest candle and the highest price for a reference point. Let’s close that, and that was ObjectCreate. You can play around and use other parameters here, but for this simple example, this is good enough.
Now, let’s change a few properties. The first one will be changed by using ObjectSetInteger for the chart ID zero. I would like to change the property for the object that is called SimpleChannelObject. The property is called Object Property_color. I would like to have a yellow color.
Now let’s continue and change the deviation. Everything is similar, but this time we are using ObjectSetDouble, and the property we want to change is called ObjectPropertyDeviation.
I have chosen the value two here. Let’s set another property. This time it is called ObjectPropertyWidth. I have set it to one. Last but not least, we want to change the ObjectPropertyRayRight, and set it to true because that will draw our channel right into the future.
Okay, that’s about it. If you have no idea what all the code here does, or if you think that this was too fast for you, 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.
For now, please click on View, Toolbar, and click on the Compile button or press F7 on your keyboard. You shouldn’t get any errors, and if that is the case, you can click on a little button here or press F4 to go back to MetaTrader. In MetaTrader, you want to click on View, Strategy Tester, or press Control and R.
Please pick the new file, SimpleDevChannel.ex5, mark the option for the visible mode. Maybe you need to scroll down a little bit. This is where we need to enable it. Now please start your test. Here we are. We see that our channel is drawn, and when we zoom into the chart, it is rearranged. You’ll see that the majority of all candles is drawn inside of our deviation channel.
And in this little video you have learned how to create a deviation channel object, and you have coded it yourself with a few lines of MQL5.