Video Discription |
https://mql4tutorial.com
With MQL4 and Metatrader4 we create an Expert Advisor for the Stochastic Oscillator. This Stochastic EA will create buy and sell signals and actually open buy and sell positions on the chart.
In this video we are going to create an expert advisor for this oscillator here, this is the stochastic oscillator and we are using it to create buy and sell signals so let’s find out how to do that with mql4.
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 file simple stochastic ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
First, we want to create a string variable called signal but we don’t assign a value here because this what we are going to calculate later on.
The next step is to define the expert advisor properties. We will use the built-in function istochastic, we have two lines here, one is the k line the other one is the d line. And we start with the current value for the k line.
We need to pass a few parameters.
The first parameter is for the current currency pair on the chart, the second one for the currently selected time period on that chart and the next three values here is what you see if you click on insert, indicators, oscillators, stochastic oscillator.
We see we have a k period with a value of five, a d period with a value of three and the slowing value is also three and that’s exactly what we are using here.
The next parameter mode underscore sma stands for the moving average method, this one is simple for simple moving average.
Here we have another parameter, the zero value is used to define that we are using low high for the price field calculation. There is also something called close close but we will go with the default values here. So please click on okay, right-click into the chart, select template, save template and save it as tester dot tpl because this is the template that we are going to see in the next strategy test, you can overwrite the old one.
So far so good!
Let’s calculate the d value, this is pretty similar but we are using mode underscore signal in this case, that’s for the second line. We also want to calculate the same values for candle one, so now we create a variable called k one and d one.
You can copy everything from the first two lines but you need to use another name for the variables and the last parameter is different because we use a one for candle one.
If the current k and the current d value are above eighty – that is the case when both lines are above the dotted line here – and if that is the case we want to check if we had a crossover, so if the current d value is bigger than the current k value but the last d value was below the last k value that would be a sell signal so we assign sell to our signal.
So let’s actually use sell signal here!
For a buy signal, we would check if the current k value and the current d value are below twenty – and that is true if both lines are below this dotted lower line here – and this time we would check if the current d value is below the current k value and if the former d value was above the former k value.
If this expression is true we would have a crossover and now we assign the word buy to our signal.
And if our signal equals buy and the return value for orders total is zero, that would mean that we have no open orders and a buy signal and that’s when we use order send to open a buy trade for ten micro lot.
In the other case if the signal equals sell and we have no open orders we would use order send to sell ten micro lot.
Finally, we use the comment function to output the text, the current signal is, followed by the calculated signal directly on our chart.
So far so good!
Well, if you don't understand what all the code here does or if this was a little bit too fast for you, maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course might be interesting for you. You can find that one on our website.
For now, please click on the compile button or press F7. In my case, I don’t have any errors and if this is also true for you, you can click on a 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, please pick the new file simple stochastic ea dot ex4, mark the option for the visual mode here and start your test.
Here we are! This is the first crossover, the expert advisor has already opened a buy position so it works as expected and in this little video you have learned how to create an automated version of the stochastic expert advisor for Metatrader 4 and you have coded it yourself with a few lines of mql4 code. |