MQL5 TUTORIAL BASICS – 49 SIMPLE WILLIAMS PERCENT RANGE EA

video
play-sharp-fill

 

  1. Introduction to the Williams Percent Range Oscillator Expert Advisor (00:00 – 00:13) Introduction to trading with the Williams Percent Range oscillator and creating buy and sell signals to open trades in MQL5.
  2. Starting in MetaEditor and Creating a New Expert Advisor File (00:13 – 00:28) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Williams Percent Range EA.”
  3. Setting Up the Code Structure and Including Necessary Files (00:28 – 00:56) Deleting unnecessary code above the ‘on tick’ function, removing comment lines, and including the ‘trade.mqh’ file for trading functions.
  4. Creating an Instance of the Class CTrade (00:56 – 01:12) Creating an instance of the class ‘ctrade’ for opening and closing positions.
  5. Calculating Ask or Bid Price Using SymbolInfoDouble (01:12 – 01:40) Using ‘SymbolInfoDouble’ for the current symbol to calculate the ask or bid price, and normalizing the number of digits behind the dot.
  6. Creating a String Variable for the Signal (01:40 – 01:54) Creating a string variable named ‘signal’ for the trading signal, to be calculated later.
  7. Creating and Defining the Williams Percent Range Array (01:54 – 02:27) Creating a ‘wpr array’ and defining it using the ‘iwpr’ function for the current symbol and period, calculating the result for fourteen candles.
  8. Sorting the Array and Filling with SAR Values (02:27 – 03:02) Sorting the array from the current candle downwards and filling it with values for three candles using ‘copy buffer’.
  9. Calculating the Current WPR Value (03:02 – 03:17) Calculating the current Williams Percent Range value for candle zero and normalizing it to two digits behind the dot.
  10. Defining Conditions for Buy and Sell Signals (03:17 – 04:09) Setting conditions for buy signals when the WPR value is below minus eighty but above minus one hundred, and sell signals when the WPR value is above minus twenty but below zero.
  11. Executing Trades Based on Signals (04:09 – 04:37) Using ‘trade.sell’ and ‘trade.buy’ to execute trades based on the calculated signals, ensuring no open positions exist.
  12. Outputting the Signal and Finalizing the Code (04:37 – 04:51) Using the ‘comment’ function to output the current signal on the chart.
  13. Compilation and Testing in MetaTrader (04:51 – 05:17) Instructions on compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
  14. Demonstration of the Expert Advisor in Action (05:17 – 05:59) Demonstrating the expert advisor in MetaTrader, showing the opening of a sell trade, confirming that the expert advisor works as expected.

 

 

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.