https://mq4tutorial.com
With MQL4 for Metatader4 we create an Expert Advisor to draw a Channel object right on the chart.
In this video we are going to create a channel object that will show us the highest and the lowest prices on the chart, so let’s find out how to do that with mql4.
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, simple channel object, 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 find out how many candles we have on our chart that can be done by using the function, window first visible bar because it will return the index of the first visible bar in the current chart window.
Afterwards, we need to calculate the lowest candle on the chart – that’s the number of the candle – and we can do that by using ilowest for the current symbol on the chart and the currently selected period on that chart, we use mode underscore low – all in capital letters – we want to do that for all the candles on the chart up to the current candle zero.
Let’s repeat the process this time for the highest candle! Now we use the function ihighest for the current symbol on the chart and the currently selected period on that chart. This time we use mode underscore high for all the candles on the chart up to candle zero.
Now we want to delete old objects by using object delete. I want to delete each object that is called simple channel, we will not have such an object the first time we call this function but afterwards, we want to delete the old value before we create a new object. And to do that we use object create that uses a lot of parameters, so let’s go through the list here.
The first parameter zero stands for the current chart, parameter two is the name of the object, I have called it simple channel but you can call it whatever you like as long as you use the same name everywhere.
The next parameter is for the object type, we are using obj channel and if you mark that and press F1 you will see that there are lots of different object types depending on the type you use you might need to other parameters.
Now let’s continue! This parameter will tell Metatrader where to draw the object. I would like to have the channel in the main window where we have the candles if you use oscillators like here you will have separate windows.
Now let’s define the points for our line!
The first one is a time parameter because this here is telling us the time of the candles and I would like to start from the first visible candle on the chart.
Now let’s define the price point. I would like to use low that’s the lowest price for a candle and I would like to have the lowest price for the lowest candle, that’s a value because here we have the different values, so now we have a point in time where we want to start and we have a value and I would like our channel line to be drawn to the current candle zero but actually, not to the low of the current candle, I use low of the lowest candle again and for the second line I would like that line to start at the point in time for the first visible bar on the chart like we did here and this one should go through the high of the highest candle on the chart.
Now our object is finished.
Let’s change some properties that can be done by using object set integer for the chart id zero and the object that we have called simple channel, I would like to change the object property color and change that to yellow.
Let’s repeat that! This time I want to set the object property style and I would like to have a solid line, so I use style solid here.
Let’s continue!
This time we want to change object property width, I use three that will give us a thicker line and the last property is called object property ray that will give us a kind of prediction and draw the line into the future.
So that’s 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 the 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, you shouldn’t get any errors and in that case, you can click here or press F4 to go back to Metatrader.
And in Metatrader we want to click on view, strategy tester or press ctrl and r, please pick the new file, simple channel object dot ex4, mark the visual mode option and click on start.
Here we are! We see that our channel object is drawn and when we zoom into the chart or out of the chart it will adjust, so our little expert advisor works as we want it to work and in this little video you have learned how to create a simple channel object and you have coded it yourself with a few lines of mql4 code.