MQL4 TUTORIAL – SIMPLE WPR EXPERT ADVISOR

video
play-sharp-fill

In this video we are going to create an Expert Advisor to output buy or sell signals on your chart based on the WPR Indicator.
This is the Williams’ Percent Range Indicator, it’s based on the last 100 candles and whenever the blue line here crosses one of the dotted lines, that will produce a buy or a sell signal, when it’s below it’s a buy signal, and when it’s above the upper dotted line here, that would be a sell signal.
Now how can we create an Expert Advisor that is able to output buy or sell signals for the Williams’ Percent Range Indicator directly on your Forex chart?
To do that please click the little button here or press F4 on your keyboard and now you should see the Metaeditor window, and here you want to click on “File/ New/ Expert Advisor (template)” from template. Continue.
I will call this one: Simple WPR (SimpleWPR) for Williams’ Percent Range. Click on continue, continue and finish. Now you can delete everything that is above the “OnTick” function here and please also delete the two comment lines.
We will start with a variable for the signal – this is a string variable called signal – and so far, we will not assign any value because we are going to calculate the signal later.
Now let’s calculate the WPR value, we are doing it by using the “iWPR” function that comes with MQL4. It needs the current symbol on your chart, the selected time frame like 1 minute or 60-minute charts, this is the period for the calculation, we will use 100 candles here because when you right-click on the indicator and select properties, you will see that 100 candles are the period for the calculation in the default value. And the last parameter is for a shift value, we could use that to move the Indicator to the left or to the right, but we don’t need to do that.
We use “_Symbol” for the symbol on the chart, _Period for the selected period on the chart, this parameter is for our 100 candles and because we are not going to use any shift value the last parameter is zero.
When the WPR value is below minus 80 – that would be the case when the blue line is below the dotted line here, that’s the minus 80 mark – we want to create a buy signal, and here is how that is done with MQL4: if WPR value is below minus 80 we want to create a buy signal, so we assign the word: “buy” to our string variable with the name signal.
In the other case if the WPR value is above minus 20 – that’s whenever the blue line here crosses the upper dotted line – we want to create a sell signal.
So, if WPR value is bigger than minus 20 we create a sell signal by assigning the word: “sell” to our signal variable.
In the last step we are going to create a screen output by using the “comment” function and that will output the word WPR value followed by the calculated value.
This will produce a new line and in the second line we want to have the text signal followed by our calculated signal. Please don’t forget the closing bracket and when you are done you can click on the compile button here or press F7 on your keyboard.
If you don’t get any errors and no warnings here, you can click on a little button here or press F4 to go back to Metatrader.
In Metatrader we are going to use a little trick, just click on “Insert /Indicators /Oscillators /Williams’ Percent Range” and click on OK.
Now your chart should look like this, and you want to right-click, select “Template /Save template” and save it as tester.tpl because this is the template the strategy tester is going to use for your back test.
If you are ready, please click on select “View/ Strategy Tester” or press CTRL and R.
Now you should see the Strategy Tester panel here and you want to select the file: “SimpleWPR.ex4”, mark the visualization mode here and start your test.
This is our Expert Advisor at work, it generates buy or sell signals on our chart and you have created it yourself with a few lines of MQL4 code.

Download “MQL4 Tutorial - Simple WPR Expert Advisor” SimpleWilliamsPercentRange.txt – Downloaded 1853 times – 655.00 B