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 sel...