In this video, we are going to create an Expert Advisor for this standard deviation indicator, so let’s find out how to do that with MQL4. To get started, please click on a 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 file simple standard deviation EA. Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here.

We start by creating a string variable for the signal, but we do not assign any values here because we are going to calculate that later.

Now let’s create a deviation array for two hundred candles. This is a double array so it can hold floating type values. And now we are going to use For loop to go through all the values for as long as our counter is above zero. And inside of that loop we are going to assign a deviation value. That can be calculated by using the included function iSTDDEV. For the current symbol on the chart and the currently selected period on that chart, we want to calculate for twenty candles without a shift value.

We are using Mode Underscore SMA. That stands for simple moving average, and the result should be calculated based on the close price, for the current candle number. And now that we have done the calculation, we are going to assign the calculated deviation value to the counter number of our deviation array. That’s it for the For loop. Now let’s continue and calculate the current value. That is done by using the same function for the current symbol on the chart and the currently selected period on that chart.

We also calculate it for twenty candles, without any shift value, for the simple moving average and the close price. But in this case we are doing it for Candle zero. Afterwards we can use array maximum. If you mark that and press F1 you will see that will give us a maximal element in an array. In our case, it’s the number for the highest candle. Array Maximum needs a few parameters. The first one is the array. We want to search the whole array starting from Candle zero.

Let’s repeat that for the lowest candle number. This time we are going to use Array Minimum, that will give us the minimum value. Now we can check if the lowest candle equals Candle zero. That’s the current candle. And if that is true, we assign the word buy to our signal. In the other case, if the highest candle is the current Candle zero, that would be a Sell signal. And that’s when we assign the word sell to our signal.

Highest candle means the highest value for our indicator in this case. But often this is also the highest candle on the chart. Now let’s continue. If we have a buy signal and the return value for OrdersTotal is zero, that would mean we have no open positions.

And we have a buy signal, and that’s when we use Ordersend and by 10 Microlot.

Of course, we also need to do that when we have a sell signal. We will check if we have no open positions and if we have Sell signal. And if that is true, we use Ordersend. And sell 10 Microlot. Finally, I would like to add a comment statement for a chart output. That will output the current value, the number of the highest and the lowest candle for our indicator and the signal on our chart.

If you have no idea what all the code here does or if this was too fast for you, maybe you want to watch one of the other videos and the basic video series. Or maybe even the Premium Course on our website might be interesting for you. That’s also where you can find a link to download source codes like this one. In case you don’t want to type everything, but for now, please click on the Compile button or F7 on your keyboard.

That should work without any errors. And if that is the case, you can click on the little button here or press F4 to go back to Metatrader. And in Metatrader you want to pick an empty chart, please click on insert, indicators, trend, standard deviation. We will go with the default values here. That is for 20 candles, the close price, simple moving average and shift value of zero. So let’s click on OK, here is the indicator. Now let’s right click into the Chart, select Template, Save template and save it as Tester.tpl, because that is the template that you will see in the next strategy test. You can override the current value.

So let’s confirm that. And now we want to click on View, Strategy tester or Control and R. Please pick the new file simple standard deviation dot EX4, mark the option for the visual mode here and start your test. Here we are. The Expert Advisor is running. I will speed it up a little bit and as soon as the highest candle is the current one, you will see the first trade. That’s also true if it is the lowest one. So our little Expert Advisor works as expected.

And in this little video, you have learned how to create an Expert Advisor for the standard deviation indicator and you have coded it yourself with a few lines of MQL4 code.