MQL5 TUTORIAL BASICS – 53 SIMPLE FIBONACCI OBJECT

video

In this video we are going to create such a Fibonacci object, this was a request from one of our course members, so let’s find out how to do that with mql5.
To start 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 simple Fibonacci object, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
Inside of the on tick function we want to create a few variables for the highest and the lowest candle, these are integer variables because it’s only the number of the highest and lowest candle.
We also create two arrays for the highest and the lowest prices and with array set as series we are going to sort the arrays from the current candle downwards, let’s also do that with the second array and now we can fill the arrays, we are using copy high to fill the high array with the highest prices of those candles, I would like to calculate the values based on one hundred candles.
And for the lowest values in the candles, we use copy low, both for the current symbol on the chart and the currently selected period, we start with candle zero, that’s the current candle and copy the values for one hundred candles in each of the arrays.
Now we can use array maximum to calculate the value for the highest candle in our high array and we use array minimum to calculate the lowest candle in our low array but we also need some price values, so let’s use mql rates to create a price information array, this one also needs to be sorted with array set as series.
Now we copy all the price data for the whole chart by using copy rates for the current symbol on the chart and the currently selected period for all the bars on the chart into our price information array, we use object delete to remove formal Fibonacci objects, we don’t have any objects at the very first time but afterwards, we delete the old object before we draw a new one and now we use object.create to actually create the Fibonacci object on our chart.
For the current symbol on the chart, the name will be Fibonacci, the type of the object is obj underscore fibo, if you mark it and press F1 you will see that we have lots of different object types, this is the one that we are going to use, we want to use the main window to draw the object, the left border will be candle number one hundred, the right border is candle zero, the upper border is where the highest candle price is and the lower border will be drawn on the lowest low of these one hundred candles.
That’s about it.
If you don’t know 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, you shouldn’t get any errors and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file simple Fibonacci object dot ex5, mark the visual mode here and start a test.
Here we are! Our Fibonacci object is drawn on the chart, now we want to stop the test, let’s calculate some values, this can be done by using object get. In our case, we use object get integer to get the time property for time values and we use object get double to get the object property for the price, if you mark this one and press F1 you will see that each object has lots of properties that can be different for each type of object, you can set a property by using object set, for example, the color or something like that or you can get the property by using object get and now we create a chart output with the comment statement that will output the time stamps and the price levels directly on our chart.
To calculate the price level fifty I have simply added the price level one hundred and the price level zero value and multiplied it by zero point five. Now let’s recompile the code, go back to Metatrader, start another test and here is the object and here are our values, let’s hit the pause button, with a click on the middle mouse button I can check if the calculated values are correct. This is the price level one hundred value, it’s one point four four nine two, that’s exactly what we have calculated here.
So our little expert advisor is working as expected and in this little video you have learned how to code a Fibonacci object and you have coded it yourself with a few lines of mql5 code.