MQL4 TUTORIAL BASICS – 92 SIMPLE DEVIATION CHANNEL OBJECT

video

 

In this video, we are going to create a deviation channel object like this one. 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 SimpleDevChannelObject. Click on continue. Continue and finish. Now you can delete everything above the OnTick function and the two comment lines here. First, we need to calculate the number of candles on the chart and to do that, we use the function WindowsFirstVisibleBar, that will return the index or the number of the first visible bar on the current chart. I also would like to know the number for the lowest candle, and we can calculate that by using iLowest for the current symbol on the chart and the currently selected period. For low prices, we use MODE_LOW, for all the candles on the chart starting with the current candle, 0.

Now let’s repeat the whole process and calculate the highest candle. That can be done by using iHighest. For the current symbol on the chart and the currently selected period, this time we use MODE_HIGH, for all the candidates on the chart. And we also start with candle 0. Now, before we create a new object, we use ObjectDelete to select the last channel object that we have created. We will not have such an object the very first time we call this function, but afterwards, we want to make sure that the former object that is called SimpleChannel will be deleted and afterwards we can draw another one. And that is done by using ObjectCreate. So let’s find out what parameters we need to fill. The first parameter is 0, that is for the current chart that we are using. The second parameter is the name of the object.

It is called SimpleChannel. The next parameter is called OBG_STDDEVCHANNEL. So let’s mark that and press F1. Here is the object type that we want to create. There are lots of different object types and when we click on this one, we see that it will create a standard deviation channel right on our chart. That’s what we want. And then the next one parameter we are telling MetaTrader to draw it on the window 0, that’s the main window with the candles. If you like to use oscillators, you know that there are things like the MACD and when you use that one, you will create a second window below the candle chart window, but we don’t want to do that. The next parameter is for the point in time, that’s here. We have date times here and values here, and I would like that the deviation channel is drawn from the point in time where the first candle on the chart is drawn.

The next parameter is for the lowest price of the lowest candle. I would like the channel to be drawn for the current candle that’s candle 0. Once again, I pass the low of the LowestCandle as a parameter here. And the second line should be drawn for all the candles on the chart. And here, I would like to have the high of the HighestCandle as a point of reference. And that’s it for the object creation. These values might not be perfect for you, so feel free to use your own values here, but it works for this simple example. Now let’s change a few values. The first one is for the color and to change that we use ObjectSetInteger for the chart ID 0, and the object that is called SimpleChannel, we want to change the property called object property color, and change it to yellow.

Let’s continue and do that again. This time we want to change the object property style and use a solid line. So we use STYLE_SOLID. The next one is for the object width. This one is called OBJPROP_WIDTH and we use 3. That will give us a thick line. Let’s add another one. This one is called ObjectSetInteger for OBJPROP_RAY. That will give us a prediction and draw the channel right into the future. And there is another one. This one is called OBJPROP_DEVIATION. Let’s set this value to 2, and that’s about it. If you have no idea what all the code here does, or if 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 the compile button or press F7 on your keyboard. I didn’t get any errors, and if this is the case, you can click on the 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 SimpleDevChannelObject.ex4, mark the option for the visual mode and start your test. Here we are. This is the deviation channel, and we see it is moving. So let’s zoom into the chart and you will see that the majority of the candles on the chart is inside of our deviation channel. And our little Expert Advisor is working as expected, because in this video, you have learned how to create a deviation channel object, and you have coded it with a few lines of MQL4 code.