LEARN MQL5 TUTORIAL BASICS - 89 SIMPLE HIGH PRICE TREND LINE

preview_player
Показать описание


With MQL5 for Metatrader5 we create an Expert Advisor that is able to draw a dynamic trend line for the highest price of the highest candle on the chart directly to the current high of the current candle.

In this video we are going to create an expert advisor that is able to draw a line object from the highest price on the chart to the current high of the current candle to get the current price trend, so let’s find out how to do that with mql5.

To get started please click on this 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 high price line, click on continue, continue and finish.

Now you can delete everything above the ontick function and the two comment lines here.

As we only want to draw a line for the highest candle on the chart we just need to get the number of candles that are visible.
And to do that we use chart get integer for the current main chart and the property we need to get is called chart first visible bar – all in capital letters –, the last parameter here is for the sub-window, window zero means we use the window with the candles. If you like oscillators like the MACD you will see another window below the main window that would be window one but in our case, we will draw the line in the main chart where the candles are.

Now let’s create a variable for the highest candle, we also want to create an array that is called high to hold all the high prices for each candle on the chart. Let’s use array set as series to sort the array downwards from the current candle and now we can use a function that is called copy high, it contains the data of highest bar prices and we need to use a few parameters.
The first one is for the current symbol or the current currency pair on the chart, the second one is for the period that we are using on that chart, these two values are able to automatically detect that.
This is the starting index that’s candle zero or the current candle and we want to check all the candles on the chart that we have calculated here and store the values for the highest prices in our high array. And that makes it possible to get the highest candle by using array maximum.
Array maximum searches for the largest element in our array that is the array that we called high, it starts to search from the current candle zero and it will go through the values for all the candles on the chart and store the highest value in this variable called highest candle.
Now let’s use mql rates to get the price information for all the candles on the chart, not only the highest prices but all the prices. Mql rates stores information about prices, volumes and spread.
We call the array price information, let’s also use array set as series to sort this one from the current candle downwards and now we want to use copy rates to copy the price data for the current symbol on the chart and the currently selected period starting from candle zero for all the candles on the chart and store the results in the price information array.
And before we create an object we use object delete because if we already have drawn lines on the chart we are going to delete all the lines that are called simple high line trend for the current symbol on the chart, that will remove the old lines. And now we can use object create to create a new one that will require a few parameters, so let’s go through the values one by one.

The first parameter is for the current symbol on the chart, the second parameter is for the object name, in this case, it’s called simple high line trend, you could use any name but you need to use the same name everywhere, parameter three is called obj trend and when you mark that and press F1 you will see that this will draw a trend line, we could create lots of other object types, here is a list.
Parameter number four is for the window where we want to create the object, as I said I would like to have the line in the main window and it should start at the highest candle in our price information array and this is a point in time because we all know this is a time based and this is a value-based scale, so let’s start with the time for the highest candle and in the next parameter we will pass the value for the highest price of the highest candle, this is where the line should start.
Now let’s define our second point, this is the time for candle zero or the current time for the current candle and we want the line to be drawn through the highest price of the current candle.
That’s it for the object creation.

Now let’s change some properties! To change the object color we are going to use object set integer for the current chart and the object that is called simple high line trend, we want to change the object property color, I have used red because I want to see a red line.
Рекомендации по теме