MQL5 Tutorial – Simple Stochastic Expert Advisor

ARE YOU STILL BROKER FOOD?
EASY AUTOMATED TRADING! 
PRECODED MODULES AND STRATEGIES!
FASTER, BETTER, AUTOMATED RESULTS YOU WILL GET NOWHERE ELSE
CLICK HERE  TO START  NOW

One of our course members with the name Som requested an Expert Advisor that is able to calculate the Stochastic indicator and here it is.
This little Expert Advisor is able to generate buy or sell signals, a buy signal will be generated when we have a crossing of these two lines below the 20% mark and for a sell signal we are looking for a crossover above the 80% mark.
Now how can we create an Expert Advisor in MQL5 that is able to output the buy or sell signal directly on the chart?
To do that please click the little button here or press F4 in Meta Trader and now you should see the Meta Editor here.
Here you want to click on File, New, Expert Advisor from Template, Continue.
I will name this Expert Advisor Simple Stochastic EA, Continue, Continue and Finish.
Now you can delete everything above the OnTick function and the two comment lines here and the first thing we need is an empty string for the signal, we will call it signal and we will not assign any value here because we are going to calculate that later.
And now we need to create two arrays; one is named K Array and the other one D Array because if you click on insert indicators, oscillators, Stochastic oscillator you will see a K period here and a D period.
Now we need to sort our arrays from the current candle downwards that is done by using array set as series.
Now let’s actually define the EA, we create a Stochastic definition by using the built-in MQL5 function iStochastic for the current symbol on the chart and the currently selected period.
These three parameters five, three, three can be found if you look at the properties of the Stochastic oscillator.
The five K period, the first three is for the D period and the second three is the slowing.
It is also called final smoothing in the help file.
We want to use a simple moving average for the method and we want to calculate based on the low and high prices so we use Mode_SMA and STO_LowHigh for the last two parameters.
Basically everything we have used here can also be found in the default values of the built-in Stochastic Oscillator in MetaTrader 5.
Now let’s use copy buffer for the Stochastic definition we have created here for the K array and for the D array.
The K Array will be calculated based on buffer zero from the current candles zero for three candles and the D Array will be filled by using buffer one – also from candle zero for three candles .
Now let’s actually calculate the value for the current candle.
K value zero will be based on the K Array and we use candle zero that’s the current candle and to calculate the D values zero we will also use the D Array and also candle zero.
And now we are going to calculate the value for the last candle, we will call that K value one and D value one and we will do the same as above, but this time our calculation will be based on the value of candle one.
Now let’s define the buy signal, first we need to check if both values are below twenty, that would be the case if both lines are below the twenty per cent mark here.
If that is true we want to check if the K value has crossed the D value from below, that would be the case if the blue line crosses the red line from below.
If that is true that would be a buy signal.
In the other case for the sell signal we would check if the K value and D value are above the eighty percent mark, that would be in this area.
If the K value has crossed the D value from above, that’s the case if the current K value is below the current D value and the K value for candle one is above the D value for candle one, then we would like to create a sell signal.
And in the last step we will use the comment function to output the text ‘signal’ followed by the calculated signal directly on our chart in the upper left corner.
Okay, if you’re done please click on the compile button here or press F7 on your keyboard.
And now you should see that the compiling worked without any errors, so let’s click on the little button here or press F4 to go back to MetaTrader.
And in Meta Trader you want to click on View, Strategy Tester or press control and R, and now you want to select the file Simple StochasticEA.ex5.
Enable the visualization mode here and start your test. And as you can see it works.
Our little Expert Advisor is going to tell us when to buy and when to sell and you have coded it yourself in a few minutes with these few lines of MQL5 code.

Download “MQL5 Tutorial - Simple Stochastic EA” SimpleStochasticEA.txt – Downloaded 88 times – 1 KB

Learn how to create your own MQL5 trading robot for $1
Sign Up Now