MQL5 Tutorial – Simple Chart Objects
Each time a tick comes in, one of these objects will be painted here.
Usually you will see things like arrows up and down, but actually there are a lot of more possibilities for such an arrow symbol.
Everything you see here is called an arrow even if it doesn’t look like one.
Now how can we create an Expert Advisor in MQL5 that is going to draw objects like these?
To do that, please click on the little button here or press the F4 key on your keyboard. And that will bring up your MetaEditor window and here you want to select File, New, Expert Advisor from Template, Continue.
I will call it SimpleChartObjects here, Continue, Continue and finish. And now you can delete everything above the OnTick function and the two comment lines here.
And first we need an array for prices, we can create one by using MQLrates and it will have the name Price Information, now we are going to sort the array downwards from the current candle by using arraysetasseries and by using copyrates we are going to fill the price information array for the current chart and the period selected on the chart from the current candle zero for all the bars on the chart and store the values in our array.
And now we are going to create a static counter, it will be an integer with the name i, and we will use that value to actually create an arrow with the name MyArrow. Please remember in MQL 5 an arrow can look like a circle or a square or anything else.
So we use ObjectCreate for the current chart and our object will have the name MyArrow and we use OBJ_Arrow to create it.
The next parameter would be a sub window; we will use a zero here because we are going to create the arrow here in the main window.
You might have seen indicators that use a sub window below.
We will use the current time and the arrow should be placed on the highest point of the current candle in our price information array we have created here.
And with object set integer for the current chart, and the object, my arrow, we use OB J Prop_Arrow Code, and the value of the static counter to change how our object looks.
Currently we have the value 179 and as soon as that changes to 180 the arrow code changes and so does the way our arrow looks.
And for 181 our arrow would look like this; let’s select a color here, that is done by using objects set integer for the current chart for the object with the name My Arrow and we use OBJ_Prop_Color and we would like to have the color green.
If you press F1 on your keyboard you will see a lot of other colors that you can use. To make our object a little bit bigger we’re using object set integer for the current chart for the object with the name MyArrow and we use the OBJProp_Width to set the size to 20.
And now we move the object by using ObjectMove for the current symbol and for the object with the name MyArrow on the main window we used, for the current time and we move it to the highest prize of the current candle.
I would actually like to have a chart output for the current counter value so we use comment to output the text “i has the value” followed by the current value of the static integer variable i. And in the last step we are going to increase our counter by one, that is done with I++ and now the next time a tick comes in we should see another symbol on the chart.
Okay that’s it; let’s compile the code here and that worked without any errors. So please click on the little button here or press F4 on your keyboard to go back to MetaTrader. And in MetaTrader click on View, Strategy Tester or press control and R to bring up the Strategy Tester Panel here and there you want to select the file SimpleChartObjects.ex5.
Let’s start the test after you have enabled the visualisation option here and for the first 20 counter values nothing changes, but around 30 you will see changing symbols on your chart and now you actually know how to print dynamic symbols on your chart and you have done it within a few minutes by using a few lines of MQL5 code.