In this video, we are going to create an expert advisor that is able to change the colors for several objects on the chart, so let’s find out how to do that with mql5.
To get started please click on the little icon here or press the F4 key 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 color changer, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start by creating an array, we will use mql rates to get the price information because mql rates is a structure that stores the information about price, volumes and spread.
Afterwards, we use array set as series to sort our price information array from the current candle downwards and with copy data, we can fill our price information array with data for the current symbol on the chart, we are going to use the currently selected period on that chart and we will copy the data starting from candle zero for three candles and store it in our price information array.
Now that we have done this we are able to calculate the close price for candle one that is done by looking into the value for candle one in the price information array. We are interested in the close price, so if you cut that out and press the period button on your keyboard you will see that you can get close, high, low or open prices, we will go with the close price. Afterwards, we do the same for the close price of candle two. Everything else is similar but this time we are going to look for the close price of candle two in our price information array and if the close price for candle two is below the close price of candle one we want to change the colors on the chart that is done by using chartsetinteger. You probably would expect something like chartsetcolor, so this is a little strange. We will do that for the current chart and the property we are going to use change first is chart color candle bull, I will set it to green, so I use clr green here and if you mark that and press F1 you will see that we can use a lot of different predefined colors here and this will change the body color for the bull candles.
Let’s do the same thing here for the bear candles. Everything is pretty much the same except for the property that we are going to change, this time is chart color candle bear and now we use chartsetinteger to change the property for chart color chart up. This is for the outer color of the bull candles and I’ll also change this one to green and repeat the whole thing and do it for the bear candles by using chart color chart down.
The next one is for the chart mode and here we can pick one of three values. Chart mode will change the type, we can have candlesticks, bars or line chart, we can pick one of these values and I decided to go with a bar chart. I want to have a grid on the chart, so I use chartsetinteger to set the property for chart show grid to true.
Let’s change the foreground color for the data by using chart color foreground and I will set it to yellow here and finally, I would like to set the background color, so I use chart color background and set it to black.
In the other case when the close price for candle two is above the close price for candle one I would like to see other colors. This is basically the same stuff so I will use copy and paste here, this time we will see the color red for the candles, that’s also what I use for the chart mode, I would like to see candles on the chart, I don’t want to see the grid, so I set the property to false.
The chart color foreground should be magenta and the chart color background should be white. But you can not only set values for the chart properties it’s also possible to read values.
In our case we will read the max price and the min price, that is done by using chart get double for the current chart and the properties are chart price max and chart price min.
Finally, we want to add a comment statement to get an output on the chart that will show us the close price for candle one and candle two, the max price and the min price and that’s about it.
Well if this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you.
This was another suggestion that was made by a premium course member and if you are already a premium course member and have an idea for a video like this one please send me an email.
For now, please click on the compile button or press F7 on your keyboard, you shouldn’t get any errors here and if this is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we want to click on view, strategy tester. Please pick the file, simple color changer dot ex5 here, enable the visual mode and start a test.
Here we are! Based on the current prices we see that the chart colors are changing, so our little expert advisor works as designed and in this little video you have learned how to create an expert advisor in mql5 that is able to change the chart colors for different objects based on the current price and you have coded it yourself with a few lines of mql5 code.