In this video we are going to create a standalone version for the Stochastic Oscillator, this is a very simple Oscillator; whenever the two lines crossed below the lower line here that would be a buy signal and whenever the two lines cross above that would be a sell signal.
This Expert Advisor is actually going to trade and we will see the signal here on the chart.
Now how can we create an Expert Advisor for the Stochastic Indicator?
To do that please click on a little button here or press F4 and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this version: “SimpleStochasticStandaloneEA”, click on “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here.
First we want to include the file: “Trade.mqh” and we want to create an instance of the class: “CTrade”, let’s call that one: “trade” and inside the “OnTick” function we create a string for the signal that is called: “signal” and we don’t assign a value now because we need to calculate that later on.
To really trade something we need to calculate the Ask and the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart and we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we get the right number of digits – this currency pair has 3 digits and other currency pairs may have 5 digits – and that’s a way to calculate the right value.
Now we need 2 arrays; the first one is called K array (KArray) the second one D array (DArray) and when you click on: “Insert/ Indicators/ Oscillators/ Stochastic Oscillator”, you would see the same period values here: 5 for the K period and 3 for the D period, so we use “ArraySetAsSeries” to sort the “KArray” and the “DArray” from the current candle downwards and now we can call the “iStochastic” function that is included with MQL5 for the current symbol and the current period, here are our values: 5, 3 and another 3; that’s K period, D period and slowing. We use ”MODE_SMA” and “STO_LOWHIGH” – that’s the method here – SMA stands for Simple Moving Average and “Price field” is “Low/High”.
Let’s fill the array with “CopyBuffer”, the first one is for the first buffer that’s buffer 0 (zero) – that’s for the “KArray”, the second one is for buffer 1 – that’s for the “DArray”. We start at candle 0 (zero) and fill it for 3 candles, and that makes it possible to calculate the current K value (KValue) by looking at the value of candle 0 (zero) of the “KArray” and the current D value by looking at the candle 0 (zero) of the “DArray” and because we need to find out if we had a crossover we also do the same and calculate the same values for candle 1.
The buy signal would be true if both values are below 20 – so that’s the case when both lines are below the dotted line here – so “KValue0” would be less than 20 and “DValue0” would also be less than 20 and if the “KValue” has crossed the “DValue” from below – so now “KValue0” would be bigger than “DValue0” but “KValue1” was below “DValue1” – that would be a buy signal so we assign the word: ”buy” to our signal.
We also have the sell signal, that’s if both values are above 80, that would be the case when both lines are above the upper dotted line here and if the “KValue” has crossed the “DValue” from above – so “KValue0” is below “DValue0” but “KValue1” was above “DValue1” – that would be a sell signal so we assign the word: “sell” to our signal.
And if the signal equals sell and we have no open position – so “PositionsTotal” is less than 1 – we use “trade.Sell” to sell 10 micro lot in the other case if the signal equals buy and we have no open positions we use “trade.Buy” to buy 10 micro lot.
Finally we are going to create a chart output by using the “Comment” function that will output the words: “The current signal is:” and the calculated “signal”.
Okay, don’t forget the last bracket here, click on “Compile” or press F7 and now you should see no errors and no warnings here and in that case you can click here on the little button or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press “CTRL and R”, let’s pick the file: “SimpleStochasticStandaloneEA.ex5”, mark the visualization option here and start a test.
…and here we see our little Expert Advisor, it is actually opening trades like this one and now you know how to automate an Expert Advisor that is able to trade the Stochastic Oscillator and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE STANDALONE STOCHASTIC EXPERT ADVISOR”SimpleStochasticStandaloneEA.txt – Downloaded 439 times – 2.02 KB