1. Introduction to the Stochastic Oscillator Expert Advisor (00:00 – 00:13) Introduction to creating an expert advisor for trading with the stochastic oscillator in MQL5.
  2. Starting in MetaEditor and Creating a New Expert Advisor File (00:13 – 00:38) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Stochastic EA.”
  3. Setting Up the Code Structure and Including Necessary Files (00:38 – 01:02) Deleting unnecessary code and including the ‘trade.mqh’ file for trading functions, and creating an instance of the class ‘ctrade’.
  4. Creating Variables for Signal, Ask, and Bid Prices (01:02 – 01:48) Creating a string variable for the signal and calculating the ask and bid prices using ‘SymbolInfoDouble’.
  5. Creating Arrays for the Stochastic Oscillator (01:48 – 02:16) Creating two arrays for the K and D lines of the stochastic oscillator and sorting them from the current candle downwards.
  6. Defining the Stochastic Oscillator and Filling Arrays with Values (02:16 – 03:27) Defining the stochastic oscillator with default values and filling the K and D arrays with values using ‘CopyBuffer’.
  7. Calculating Current and Previous Values for K and D Lines (03:27 – 04:04) Calculating the current and previous values for the K and D lines of the stochastic oscillator.
  8. Setting Conditions for Buy and Sell Signals (04:04 – 05:12) Defining conditions for buy and sell signals based on the values of the K and D lines and their crossover points.
  9. Executing Trades Based on Signals (05:12 – 05:39) Using ‘trade.sell’ and ‘’ to execute trades based on the calculated signals, ensuring no open positions exist.
  10. Outputting the Signal and Finalizing the Code (05:39 – 05:53) Using the ‘comment’ function to output the current signal on the chart.
  11. Compilation and Testing in MetaTrader (05:53 – 06:25) Instructions on compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
  12. Demonstration of the Expert Advisor in Action (06:25 – 06:58) Demonstrating the expert advisor in MetaTrader, showing trades being executed based on the stochastic oscillator signals.


In this video we are going to create an expert advisor to trade this oscillator here, this is the stochastic oscillator so let’s find out how to do that with mql5.
To get started please click on a 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 stochastic 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 for the file trade dot mqh, this one comes with mql5 and we will use it to open buy or sell positions.
Therefore we start by creating an instance of the class ctrade that will do it for us, we will call it trade.
Inside of the on tick function, we start by creating a string variable called signal. We don’t assign a value here that is going to be calculated later on.
As we are going to trade in both directions we need the ask price and the bid price, we can calculate both with the function symbol info double for the current symbol on the chart we use symbol underscore ask – all in capital letters – to get the ask price and with normalize double and underscore digits we automatically can calculate if the currency pair has five or three digits behind the dot.
To get the bid price we do the same but this time we use symbol underscore bid.
Once that is finished we create two arrays, one is the k array, the other one is the d array because the stochastic oscillator has two lines here we need two arrays, we use array set as series for both arrays to sort them from the current candle downwards and with the integrated function istochastic we can create our definition for the expert advisor.
The first parameter is for the current currency pair on the chart, the second one is for the selected period on that chart and these three values here also show up here in the oscillator. When we click on properties we will see that the default value for the k period is five, the value for slowing is three and the d period is also three so that’s what we use here.
The last two parameters, mode underscore sma and sto underscore low high stand for the simple moving average method and for the price field so let’s continue to fill the arrays that is done by using copy buffer according to the stochastic definition we have created here.
For the k array we use buffer zero, and for the d array buffer one, and we copy the values from candle zero to candle three and store it in the k array or the d array.
Now we can get the current value for the k array by looking into the k array, especially at the value in candle zero. To get the d value for the current candle we do the same for the d array and to get the values for the candle before we do the same for candle one, so now we know the current and the last values for both arrays.
And we would have a buy signal if those values are below twenty – that’s the dotted line here – and if k value zero is below twenty and d value zero is below twenty we will check our next condition and that is, we want to know if the current k value zero is bigger than the d value zero and if the last k value was below the d value we have a cross over and if that is the case we assign the word buy to our signal.
Otherwise, if the current k value and the current d value are bigger than eighty – and that is the case when the two lines are above the upper dotted line here – that’s when we check if the current k value is below the current d value and if k value one was above d value one for the candle before we have a sell signal so now we assign the word sell to our signal.
Now if our signal equals sell and if the return value for positions total is below one – or in other words, we don’t have any open position – we use trade dot sell to sell ten micro lot.
Otherwise, if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot.
Finally, we use the comment statement to output the text, the current signal is and the calculated signal directly on our chart.
That’s about it.
If you don’t know what all the code here does or if this was too fast for you, you probably want to watch one of the other videos in this basic series first or maybe you are even interested to buy the premium course, you can find that one on our website.
For now, let’s click on the compile button or press F7, we don’t have any errors here and if that is the case we can click on the 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 ex5, mark the option for the visual mode here and start a test.
Here is our expert advisor, it is running and we had a cross over right now so now we should see the first trade on our chart. The expert advisor is working like expected and in this little video you have learned how to create an expert advisor for the stochastic oscillator and you have coded it yourself with just a few lines of mql5 code.