In this video, we are going to create an expert advisor that is able to change a lot of colors for different objects right on the chart, so let’s find out how to do that with mql4.
To get started please click on a 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.
In this simple example, we are going to change the color whenever the close price for the candle before is below the close price for the last candle and if that is true we want to start by setting the color for the bull candles so that’s the color of the body inside of the candles and we use chartsetinteger for the current chart, the property we want to select is chart underscore color underscore candle underscore bull and I would like to set it to green, we have a lot of different color options. All these colors here are available. Well, you might wonder why we use chartsetinteger instead of something like chartsetcolor.
Actually, we could use a number like a twenty-two here and when we now compile the code this wouldn’t give us any errors but actually clr green is better to read, so this was the body color for the bull candles.
Let’s repeat that for the bear candles. Everything is the same except for the property value here.
I would also like to set the outer color and this is the property that is called chart color chart up, let’s also pick green here and we are going to repeat that for the bear candles, that is the property chart color chart down.
One more of those, this one is called chart mode and now I can define what type of candle I would like to see on the chart because you can either pick candles, bars or a line chart. We are going to use chart underscore bars here and if you mark that and press F1 you will see that each of those candle types can be picked.
Now we are going to set the grid to visible that is done by setting the chart show grid property to true.
Let’s also change the foreground color with chart color foreground, we want to set it to yellow and of course, you can also set the background color that is chart color background and I’ll set it to black.
Now let’s repeat the whole thing because when the close price for candle two is bigger than the close price for candle one I would like to see other colors.
I just will copy and paste it here, basically, it’s all the same, of course, you can not only set properties you also can get properties.
For example the max price and the min price on the chart that is done by using chart get double for the current chart, we are going to use chart price max and this last parameter here is for the current sub window, for example, if you would like to insert an oscillator here this would be sub window zero and this would be sub window one and we are going to chose the main window and get the max price and the min price, so let’s add a chart output here by using the comment statement.
This will output the text and the values for those properties 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 the basic video series first or maybe even the premium course on our website might be interesting for you.
This was a suggestion from 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 and if this is the case you can now 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 or press control and r, please pick the new file, simple color changer dot ex4, mark the option for the visual mode here and start your test.
And here is our little expert advisor at work. Based on the price we see different colors on the chart, so our little expert advisor works as expected and in this little video you have learned how to create an expert advisor in mql4 to change the colors on the chart and you have coded it yourself with a few lines of mql4 code.