MQL4 TUTORIAL BASICS – 65 SIMPLE BACKGROUND COLOR

video
play-sharp-fill

In this video we are going to create an expert advisor that is able to change the background color depending on the buy or sell signal, so let’s find out how to do that with mql4.
To get started please click on a little button 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 background color, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here, first we create a string variable for the signal currently, we don’t assign any value here because we are going to calculate that later on.
And to do that we want to use a simple moving average and to calculate that one we use the included ima function for the current symbol on the chart and the currently selected period. We want to calculate based on the last twenty candles, we don’t use any shift values here, the mode is sma for simple moving average and the simple moving average will be calculated based on the close price.
Let’s continue with the conditions. First, we want to check if the moving average is above the bid price and if that is the case we want to assign the word buy to our signal and now we want to set the chart color for the background to green.
Otherwise, if the moving average is above the ask price that would be a sell signal so now we assign the word sell to our signal and now we want to set the chart color for the background to red.
The third case would be when the moving average is below the ask price and the moving average is above the bid price, in that case, we would have no signal so we assign the word none to our signal and this is when we want to set the chart color for the background to black.
Well, if you mark that and press F1 you will see that you can set lots of colors in our case we have used clr black here, I would like to add a comment statement on the chart that will create an output, the current signal is, followed by the calculated signal.
And now we need to code this user-defined function because it doesn’t exist so far. The name for our function is set chart color background, we use void here because we don’t have a return value and we need to pass a color I have called it background color but you can call it whatever you like and inside of our function we use chart set integer for the current symbol on the chart, we want to change the property chart color background – all in capital letters – and here we pass the background color that we have received here 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 first or maybe even the premium course on our website might be interesting for you, this was actually an idea of a premium course member and if you are already a member of the premium course and have an idea for such a video, please send me an email.
Okay, so far so good! Now let’s click on the compile button or press F7, I don’t see any errors here and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And inside of Metatrader please click on view, strategy tester or press control and r, please pick the new file, simple background color dot ex4, mark the option for the visual mode and start a test.
Here is our expert advisor at work, currently we have a sell signal, the background is red and when we get a buy signal it should change the color to green, that happened right now so our little expert advisor is working and in this little video you have learned how to create an expert advisor that is able to change the background color of the chart based on the sell or buy signals that you receive and you have coded it yourself with a few lines of mql4 code.