MQL5 TUTORIAL BASICS – 52 SIMPLE EXTERNAL CANDLE NUMBER EA

video
play-sharp-fill

 

  • Introduction to Creating a Simple Moving Average Expert Advisor (00:00 – 00:12) Introduction to creating an expert advisor in MQL5 that calculates the number of candles used for a simple moving average, with an example using twenty candles.
  • Starting in MetaEditor and Creating a New Expert Advisor File (00:12 – 00:38) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple External Candle Number EA.”
  • Setting Up the Code Structure and Including Necessary Files (00:38 – 01:08) Deleting unnecessary code and including the ‘trade.mqh’ file for simplified trading functions.
  • Creating an External Input for the Number of Candles (01:08 – 01:23) Creating an external input variable for the number of candles that can be changed without modifying the source code.
  • Calculating Ask and Bid Prices (01:23 – 01:50) Using ‘SymbolInfoDouble’ to calculate the ask and bid prices for the current symbol on the chart.
  • Creating a Price Array and Filling It with Data (01:50 – 02:16) Creating a price array named ‘price info’ and filling it with data for three candles using ‘CopyRates’.
  • Creating a String Variable for the Signal (02:30 – 02:46) Creating a string variable called ‘signal’ to store the trading signal.
  • Defining the Simple Moving Average and Filling the Array with Values (02:46 – 04:01) Defining the simple moving average for the current symbol and period, and filling the moving average array with values.
  • Setting Conditions for Buy and Sell Signals (04:13 – 05:00) Defining conditions for buy and sell signals based on the simple moving average values.
  • Executing Trades Based on Signals (05:00 – 05:31) Using ‘trade.sell’ and ‘trade.buy’ to execute trades based on the signals, ensuring no open positions exist.
  • Outputting the Signal and Number of Candles (05:31 – 05:44) Using the ‘comment’ function to output the current signal and the number of candles used for the moving average.
  • Compilation and Testing in MetaTrader (05:58 – 07:24) Compiling the code and instructions on testing the expert advisor in MetaTrader using the strategy tester.
  • Demonstration of the Expert Advisor with Different Candle Numbers (07:24 – 07:42) Demonstrating the expert advisor with different numbers of candles for the moving average, showing how it affects the signals.

 

In this video we are going to create an expert advisor that is able to calculate the number of candles that are used for a simple moving average, in this case, we are using twenty candles, we can change the number of candles that are used so let’s find out how to do that with mql5.
To get started please click on the little button 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 file simple external candle number ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, this is part of mql5 and the file provides a few simplified trading functions that we are going to use to open positions.
Afterwards, we want to create an external input, this is the input modifier and it will create an external variable for the number of candles that can be changed without changing the source code.
Inside of the on tick function we first need to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask or symbol underscore bid and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot that depends on each currency pair.
Now let’s use mql rates to create a price array that will be called price info, we use array set as series to sort the price info array from the current candle downwards and we use copy rates to fill the price array for the current symbol and the currently selected period starting from candle zero for three candles and store the values in the price info array.
Actually, we could use underscore symbol and underscore period because when you mark underscore symbol and press F1 you will see that both functions do the same and that’s also true for underscore period.
Now we need to create a string variable called signal, this one has no value so far because we are going to calculate the value later and to do that we need a second array, this one is called my moving average array, we will use it for the simple moving average values and to calculate the moving average we use the included function ima for the current symbol on the chart and the currently selected period on that chart. We use the number of candles that we have defined here, the default value is one, this one here stands for a shift value but we don’t need the shift value, you could use it to set a value for a horizontal shift to move the moving average to the right or to the left but we don’t want to do that.
We want to calculate a simple moving average so we use mode underscore sma here, ema would be an exponential moving average for example and the values should be calculated based on the close price.
Now let’s also sort this array by using array set as series and afterwards we can fill it with price data that is done by using copy buffer according to the moving average definition that we have created here.
We do that for buffer zero – that is the red line here – we want to start with the current candle zero and we want to fill it with data for three candles.
Now it’s time to check for the entry conditions so if the close price for candle one in the price info array is bigger than the value for the moving average array for candle one and if it was below the simple moving average before we consider that to be a buy signal so we assign the value buy to our signal.
In the other case if the close price for candle one is now below the moving average value and if it was above the moving average before we consider that to be a sell signal so now we assign the word sell to our signal and if the signal equals sell and the return value for positions total is below one – that would mean that we don’t have any open positions – and now we use trade dot sell to sell ten micro lot.
In the other case if the signal equals buy and positions total is below one we would use trade dot buy and buy ten micro lot.
Finally, we use the comment statement to create an output, we want to see the text the signal is now followed by the calculated signal and in a new line I would like to see the number of candles followed by the value that we have defined and that’s about it.
Well if you don’t know what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first or maybe the premium course on our website might be interesting for you, for now, please click on the compile button or press F7 on your keyboard…
I have messed something up here, it looks like I have forgotten to create an instance of the class ctrade, so let’s go to the part of the source code where that needs to be done, recompile the code and this time it looks better, we don’t have any errors here and in that case, you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, let’s pick the new file simple external candle number ea dot ex5, please enable the visible mode here and start a test.
Here we are, currently, the moving average has a value of one, you can see that in the settings of the moving average so let’s stop the test, click on inputs and change the value to thirty, now start a new test and this time the moving average is calculated based on thirty candles.
Let’s do a final test for eight candles, click on start and this time the moving average is much closer so we see more signals and in this little video you have learned how to create an expert advisor that is able to automatically calculate a simple moving average based on an external variable and you have coded it yourself with a few lines of mql5 code.