In this video we are going to create an Arrow Indicator, like this one.
It produces arrows whenever we have extreme values; like this one is the highest high in the last 100 candles, this one is the lowest low in the last 100 candles, before it was this candle and now we are going to find out how to create an Arrow Indicator like this one with MQL5.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleArrowIndicator” click on “Continue”, “Continue” and “Finish”.
Now we delete everything above the “OnTick” function and the two comment lines here.
We want to start by creating a price array for the price data that is done by using “MqlRates”, the array will have the name: “PriceInformation”, we use the “ArraySetAsSeries” command to sort the array from the current candle downwards and now we fill the price array (PriceInformation) with data, that is done by using “CopyRates”, we do it for the current symbol on the chart and the currently selected period on the chart. We are going to start with the current candle – that is candle 0 (zero) – and do it for all the candles on the chart and the result will be stored in the “PriceInformation” array.
Let’s calculate the number of candles that we have on a chart, this is done by using the “Bars” function, it comes with MQL5 and if you mark it and press F1 you will see that it returns the number of pass count in the history of the specified symbol and period but later on we need that value to be a string so we convert the integer value to a string by using: “IntegerToString” for the number of candles and the result will be stored in the string: “NumberOfCandlesText”.
As I said the calculation is done by looking at the last 100 candles, so let’s calculate the highest and the lowest candle number within the last 100 candles and that can be done by using the function: ”iHighest” or “iLowest”.
For the highest candle we use “MODE_HIGH”, for the lowest “MODE_LOW”, we look into 100 candles and we start with candle 1 because candle 1 is the last candle that has a real close price.
You actually can see a close price for candle 0 (zero) – that’s simply the current price – and when the next tick comes in the close prices changing but for all the finished candles it’s really the price where the candle closed, so we use candle 1.
Actually we don’t only need to know the candle number but we also need the highest and the lowest price and to get that we look into our price information array (PriceInformation) and get the highest candle number or the lowest candle number and here we look at the high or below and if the lowest price for the current candle 0 (zero) is below the lowest price of the last 100 candles we want to create an arrow upwards, so we use: “ObjectCreate” to create an arrow for the current symbol, we use “NumberOfCandlesText”, for the object name “OBJ_ARROW_BUY”, let’s look into the help file and you will see that “OBJ_ARROW_BUY” will create an upwards arrow and “OBJ_ARROW_SELL” will create an arrow downwards. This zero means that the arrow will be printed right on the chart, this would be an Indicator that is printed below the chart, and zero would be used for the main chart so that’s what we use here.
“TimeCurrent” will deliver the current server time, actually it’s the time for the candle prices and we want to place our arrow below the low for candle 0 (zero) in our price information array (PriceInformation).
Of course we also need to calculate that for the highest price so if the high for candle 0 (zero) in our price information array (PriceInformation) is bigger than the highest price for the last 100 candles we also create an object but this time we are going to use “OBJ_ARROW_SELL” and the arrow will be drawn above the highest price of candle 0 (zero) in our price information array (PriceInformation).
In the last step we use the “Comment” function to output the “NumberOfCandles:”, the “HighestCandleNumber:”, the “HighestPrice:”, the LowestCandleNumber:” and the “LowestPrice:”.
Okay. That’s it. Please click on the “Compile” button or press F7. The compilation worked without any errors here, we have two major warnings because we could check if the conversion from string to number actually produce the result, but in this simple example we don’t need that so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the “SimpleArrowIndicator.ex5” file we have just created, mark the visualization option here and start a test.
…and here is our little Expert Advisor at work, it already started to produce arrows, this has been the lowest price for the last 100 candles, this is the new one and now you know how to create an Arrow Indicator or Expert Advisor for Metatrader 5 and you have coded it yourself with a few lines of MQL5 code.