MQL5 TUTORIAL BASICS – 49 SIMPLE WILLIAMS PERCENT RANGE EA

video

In this expert advisor, we are going to trade this oscillator, it’s the Williams Percent Range oscillator, we are going to create buy and sell signals to open trades, so let’s find out how to do that with mql5.
To get started please click on the little icon 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 one simple Williams percent range ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and let’s also remove the two comment lines here.
We start with an include statement to include the file trade dot mqh this one is included in mql5 and it will give us some trade functions that we are going to use to open and close positions.
First, we need to create an instance of the class ctrade that will be called trade.
To open a position we need to know either the ask or the bid price, we can calculate that by using symbol info double for the current symbol on the chart, we either use symbol ask or symbol bid – all in capital letters – and with normalize double and underscore digits we calculate the right number of digits behind the dot because that could be three digits or five digits depending on the currency pair.
Now we create a string variable called signal for our signal, we don’t assign a value here because that is going to be calculated later on.
Let’s create an array, I will call this one wpr array and here is the definition, we use the included function iwpr, that comes with mql5 for the current symbol on the chart and the currently selected period on that chart.
And I would like to calculate the result for fourteen candles, that’s also what we see here and it’s the default value.
We sort the array from the current candle downwards by using array set as series for our wpr array and with copy buffer, we fill our wpr array according to the definition that we have created here. We do that for buffer zero – that’s the blue line here. We start with the current candle zero and we copy the prices for three candles and store them in the array.
To calculate the current value we just have to look inside our array and grab the value for candle zero, once again I use normalize double and I would like to have a value with two digits behind the dot because that’s how the oscillator is calculated here.
Now let’s define the buy signal.
We would have a buy signal if the current wpr value is below minus eighty but above minus one hundred, so that is the range here below the lower dotted line and if this is true we want our signal to have the value buy.
Otherwise, if the wpr value is bigger than minus twenty but below zero and that would be true when it’s above the upper dotted line here in this range that’s when we want to sell so now we assign the word sell to our signal.
And when our signal equals sell and positions total is below one – or in other words, we don’t have any open positions – we use trade dot sell to sell ten micro lot.
Otherwise, if the signal equals buy and we also have no open positions we use trade dot buy and buy ten micro lot.
Finally, we use comment to output the text, the signal is now followed by the calculated signal.
That’s about it.
If this was too fast for you or if you have no clue what all the code here does, maybe you want to watch one of the other videos in this basic video series or maybe even the premium course might be interesting for you, you can find that one on our website.
For now, if you are finished please click on the compile button or press F7, in my case I have no errors and if this is true for you you can click on the little button here or you can press F4 on your keyboard to go back to Metatrader.
And in Metatrader we click on view, strategy tester or press control and r, please pick the new file simple williams percent range ea dot ex5, please enable the visual mode here and start a test.
Here is our expert advisor and right now we have opened our first sell trade so the expert advisor works as we expected it and in this little video you have learned how to create an expert advisor for the Williams Percent Range oscillator and you have coded it with a few lines of mql5 code.