You might have heard of the Williams’ percent range indicator?
It can be inserted by clicking on insert, indicators, oscillators, Williams’ percent range. This is a very easy oscillator.
It has one line. It was created by Larry Williams and it has a negative scale -100 means you have the lowest low of the past n days. Thats fourteen, because if you click on properties of Williams’ percent range you will see that it is calculating for a period of fourteen candles and if it’s hitting the zero line, that means it’s the highest high of the past n days.
The idea is, to find peaks that are outside this range. So here you would like to sell and here you would like to buy.
Now how can we create an Expert Advisor for this indicator?
Well first we click on this little button or hit the F4 key to bring up the MetaEditor and there we will select file, new, expert advisor from template continue, and we will call this one simple Williams percent range.
Click on continue, continue, finish and delete everything above the OnTick function.
Now let’s remove the two comment lines here and in the first step we are going to create an array for our prices.
This one is called WPR array. Now we need to define it. This is done by using the iWPR function for the current symbol on the chart and the selected period on the chart, for example this could be the Euro against the U.S. dollar on the one minute chart.
And we need to define for how many candles we want to calculate the indicator. We would choose fourteen here, because the default indicator in Meta Trader is also defined that way.
And in the next step we want to sort the price array from the current candle downwards by using the function arraysetasseries for the array we have created here.
And now we will use the copybuffer function to fill our WPR array for the one line it has, from the current candle zero downwards for three candles.
And we do it according to the definition we have created here. So let’s calculate the value.
We will use the normalizedouble function to get the candle zero, that’s the current candle on my chart from the price array and we want to have an output with two digits behind the dot, because that will match the MetaTrader indicator that is built in.
Okay, the last thing is the output on the chart. We will use the comment function to print out the text WPR value and the calculated value from this line.
So let’s compile the source code here and we have zero errors and no warnings.
So now we can click on the button over here or press the F4 key to bring up the MetaTrader. And in the MetaTrader we click on View, strategy tester or hit the control and the R key, that will show that strategy tester panel here and here we will select the simple Williams percent range.ex5 file.
Let’s enlarge the panel a little bit more to enable the visualization mode here and start your test.
And now it’s going to calculate the value here and if it’s below this dotted line her, you won’t expect to see a price change in the other direction.
Let’s see if this is true – and here we are. And in the other case when it’s above this dotted line here, you would expect the price to fall.
It took a little while, but it actually happened. You might also notice that it gave several false signals here.
So let’s stop that test and change the value to 100, compile the code again, start another test and now it should create far less signals.
Let’s zoom in here and you will see: here is a peak and if you would have sold here, you would have made a large profit.
Let’s speed that up a little bit and it re-entered here and now it goes up and this also would have been profitable.
Okay now you know how you can create an Expert Advisor for the Williams’ percent range indicator and you’ve done it with a few lines of source code in MQL 5.