In this video, we want to check out this Oscillator here. It is called the Stochastic oscillator, and it is very popular. So let’s find out how we can create an entry signal for this one with MQL5.


In this video, we’re going to make an entry signal for this little oscillator that an expert advisor can use. It’s called the “Stochastic oscillator,” and a lot of people use it. So let’s find out how MQL5 can help us do that.


The Stochastic oscillator is shown in a separate window below the candle chart.


It has two lines and a scale.


Most of the time, oscillators are used to filter out signals, but they can also be used to make entry signals, which is what we’ll do here.


To use it, we make a separate MQ5 file and put it in the same folder as the other Platin System files.


The file is called CheckEntry Stochastic.mq5, and it has only one function called CheckEntry.


It will figure out what our entry signal is and send that information back to the main module.


We want to make a string variable for the calculated signal, but we won’t give it a value yet because that will be worked out later.


Now we need two double-type arrays to hold the different prices.


The first one is called the K-Array and the second one is called the D-Array.


We sort both arrays from the current candle downwards by using Array set as Series.


MQL5 comes with a function called iStochastic which we can use to create a definition for our oscillator.


We need to pass a few parameters. The first parameter is for the symbol on the chart. The second parameter is for the period on the chart.


Parameters 3, 4 and 5 are the standard values for the Stochastic oscillator to define the so-called K-period, the D-period and the slowing. You will also see these values when you drag the Stochastic indicator onto one of your charts.


In the last two parameters, we define that we want to use MODE_SMA which stands for the simple moving average mode and we want to calculate the result based on the low and high prices.


With CopyBuffer we fill both arrays with data and we do that for buffer 0 and buffer 1. In both cases from the current candle 0 and for 3 candles.


Let’s continue and calculate the value for the current candle by looking at the value of candle 0 in both arrays.


Afterwards we do the same for the candle before by looking at the values for candle 1 in both arrays.


To calculate the buy signal, we check for a crossover.


So if the K-value and the D-value for candle 0 are below 20, we want to know if the K-value is now above the D-value and if it was below for the candle before.


That would be a buy signal and that is when we assign the word buy to our signal.


Otherwise, if the K-value and the D-value for candle 0 are above 80 we want to know if the K-value is now below the D-value and if it was higher for the candle before.


That would be a sell signal and that is when we assign the word sell to our signal.


At the end of the function we use the return command to return the signal to the main module.


Please remember to save the file.


You don’t have to compile it. Just save it and go back to the main module, where the OnTick function is.


There, you must add the new file to the include section. This can be done with the help of the include statement.


Please use two slashes to outcomment any other entry signals and press F7 or click the Compile button to compile both the main module and the new entry file you just made.


Okay, if everything worked out as it should, you should now have a working version for the Stochastic Oscillator. Please note that this one will not create so many signals, because it will only create signals when we see a cross over here. That’s it for now. Thank you for watching, and I will see you in the next video.