What you see here on the chart is the RSI indicator, it is a very popular indicator and whenever the blue line here crosses one of the dotted lines that will create a signal.
Let’s speed it up a little bit!
Now you see that the blue line is between the two dotted lines most of the time, but when it’s above the upper line that is considered to be a sell signal.
In the other case if it’s below the lower dotted line that would be a buy signal and now we want to find out how we can create an Expert Advisor that is going to output buy or sell signals directly on our chart.
To do that please click the little button here or press F4 on your keyboard!
Now you should see the Metaeditor window here.
Here you want to click on “File/ New/ Expert Advisor (template)” from template. Continue. I will call this one: Simple RSI (SimpleRSI). Continue, continue and finish. Now you can delete everything above the “OnTick” function and the two comment lines here.
The first thing we need is to create a string variable for the signal – a string variable is for text – we will also call it signal, but we will not assign any text here, because that is what we are going to calculate later on.
Now we want to calculate the RSI value that can be done by using the “iRSI” function that comes with MQL4.
It needs some parameters. The first one is the symbol on the chart, the second one is the selected period on the chart; like one minute or one hour, the third parameter is the number of the candles that are used for the calculation.
If you right click the indicator and select RSI properties you will see that the period is 14 candles in the default settings and the indicator is calculated based on the close price. So, that’s what we are going to use for the next parameter.
The last parameter here is for a shift but we are not going to use it because we don’t want to move the indicator to the right or to the left.
And now that we have calculated the RSI value we want to find out if the value is below 20 – that would be the case when the blue line here is below the lower dotted line.
In MQL4 we can use the “if” statement to find out if the RSI value is below 20 and in that case, we would have a buy signal, so we assign the word “buy” to our signal.
In the other case, if the RSI value is above 80; so, if RSI value is greater than 80 we consider that to be a sell signal and we assign the word “sell” to our signal.
Let’s create a screen output! To do that we use the built-in comment function to output the text: RSI value (RSIValue) followed by the calculated value we have created here. This backslash n (“\n”) produces a new line and in that second line we output the text: “signal” followed by our signal.
That’s actually it.
Now click on “Compile” or press F7 and that should work without any errors or warnings, and in that case please click here on the little button or press F4 on your keyboard to go back to Metatrader.
In Metatrader we use a little trick; please click on “Insert /Indicators /Oscillators /Relative Strength Index” and click on OK.
Now the RSI window should appear on your chart and now you want to right click in the chart window, select “Template /Save Template” and save it as tester.tpl because that is the template that the strategy tester is going to use for your next back test. After you have done that please click on “View / Strategy Tester” or press CTRL + R and in the strategy tester you want to select the file: SimpleRSI.ex4, enable the visual mode here and start your test.
Now you should see… Oh, something is going wrong here! The signal is always present.
Obviously I have missed something, and yes, this is only a comment! Let’s add the “if” statement, so if RSI value is above 80 that would produce a sell signal.
Let’s recompile the code!
Go back to Metatrader. Stop the running test and start a new one.
This looks much better! We don’t see a signal as long as the blue line is between the dotted lines here, and whenever it crosses the lines the signal will appear right here on the chart.
Now you know how to create an Expert Advisor in MQL4 that can output buy or sell signals directly on your chart and you have coded it yourself with a few lines of MQL4 code.
Download “MQL4 Tutorial - Simple RSI Expert Advisor”SimpleRSI.txt – Downloaded 5787 times – 562.00 B