In this video we are going to create an expert advisor that is able to create random symbols directly on your chart, so let’s find out how to do that with mql4.
To get started 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 arrow symbols, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
First, we want to start by calculating the ask price that can be done by using symbol info double for the current symbol on the chart, we use symbol underscore ask and with normalize double and underscore digits we make sure that we calculate the right number of digits behind dot because for some currency pairs you might need three digits while other currency pairs have five digits. And if you ask yourself why we are not going to use ask as we always do in mql4, well this is actually an example that can be used in mql4 or in mql5 so the code that we are generating will work for both versions of the Metatrader.
Now let’s continue and repeat the whole thing for the bid price, everything is similar except for the name of the variable and we are going to use symbol underscore bid this time. I would also like to calculate a medium price by adding the ask price and the bid price and when we divide the result by two we will get the medium price.
To create randomized symbols we need to use the random generator. To prepare it we use the function mathsrand if you mark that and press F1 you will learn that it sets the starting point for generating a series of pseudorandom integer values and as a parameter we pass the function gettickcount, this one will return a number of milliseconds that have passed since we have started our system and this is how we get a start value that is unique. Afterwards, we use mathrand, this time not with an s to calculate a random value within a special range. This is our range and we assign the result to our random arrow code that we are going to use to draw our symbols.
Now let’s use object create for the current symbol on the chart, we want to create an object that is called my object and we use obj underscore arrow if you mark that and press F1 you will see that we can create lots of other object types but we are going to use the arrow because we can create different shapes with that kind of object type. This is the parameter for the chart window, chart window zero stands for the main chart, if you use an oscillator you will see a second chart below the candles but we don’t need that, so let’s draw the shapes inside of chart zero and I would like to use time current because that function will return the last known server time, that’s always the time for candle zero and the last parameter here is the medium price that we have calculated here, we are going to use that as an anchor point to draw the shape.
And to get different shapes we now use objectsetinteger on the main chart zero for the object with the name my object and we want to change the object property arrowcode and set it to the random arrow code that we have calculated here.
Objectsetinteger is also used to change the object property for the width, I will use a value of twenty to get bigger objects.
And whenever the price changes I want to use objectmove to move our object and redraw it for the current time and the medium price.
Finally, we need a chart output. We will use the comment statement to output the ask price, the bid price, and the medium price and that’s about it.
Well, if you don’t understand 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 error messages and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader please click on view, strategy tester or press control and r. Now you should see the strategy tester panel and here we want to pick the new file, simple arrow symbols dot ex4, please mark the option for the visual mode here and start a test.
Here we are! Our expert advisor is drawing different shapes directly on the chart and in this little video, you have learned how to create an expert advisor that will draw shapes in Metatrader 4 or in Metatrader 5 directly on your chart and you have coded it yourself with a few lines of mql4 code.