Now how can we create an Expert Advisor that is going to actually trade our signal and output our buy and sell signals directly on the chart?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, I will call this file: “SimpleIVIDYAStandaloneEA”, click on “Continue”, “Continue” and “Finish”.
Now we can remove everything that is above the “OnTick” function and let’s also delete the two comment lines here.
We start by including the file: “Trade.mqh”. Sometimes people ask me where the file is?
Well, it’s included in MQL5 and we can create an instance of the class: “CTrade” that will be called: “trade” and we are going to use it later on to open positions.
Inside of the “OnTick” function we are going to calculate the Ask price and the Bid price. We get the prices by using “SymbolInfoDouble”, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price.
With “NormalizeDouble” and “_Digits” we calculate the number of digits behind the dot.
Let’s create a string variable that will be called: “signal” and we are going to calculate the signal value later on so we don’t assign a value right now.
We also need to create a price array (PriceArray) that is done by using “MqlRates”, lets sort the array from the current candle downwards by using “ArraySetAsSeries” and with “CopyRates” we fill our array (PriceArray) with price data for the current symbol on the chart and the currently selected period, we are going to start from the current candle 0 (zero) and we need the price for 3 candles – that’s for the prices.
Now we need to create an array for the Expert Advisor data that will be called: “EAArray”, let’s also sort that one by using “ArraySetAsSeries”.
Now we actually can define the Expert Advisor by using the integrated “iVIDyA” function. If you mark that one and hit F1 you will see all the parameters for the Variable Index Dynamic Average Indicator, it has only one buffer and the parameters are: the current symbol on the chart, the currently selected period on the chart, here we have a 9, a 12 and a 0 (zero) – so let’s click on: “Insert/ Indicators/ Trend/ Variable Index Dynamic Average” and you will see that we have a “Period CMO:” that is 9, the “Period EMA:” that is 12 and the “Shift:” value of 0 (zero) and that’s exactly what we use here and the value will be calculated based on the close price (PRICE_CLOSE). So let’s use “CopyBuffer” to fill our array (EAArray) with data according to the IVIDYA definition (iVIDyADefinition) we have created here, it’s for only one buffer – the first buffer is buffer 0 (zero) – the current candle is also candle 0 (zero), we need data for 3 candles so we use a 3 here and save it in our “EAArray”.
So far so good; to get the current value we just look into our “EAArray” and we need the value for the current candle – that’s candle 0 (zero) – now we can actually calculate the signal. Whenever the current iVIDyA value (iVIDyAVALUE) is bigger than the close price of the last candle in our price array (PriceArray) that would be a sell signal and that’s when we want to assign the word: “sell” to our signal.
In the other case if the iVIDyA value (iVIDyAVALUE) is smaller than the price array 1 close price for candle 1 (PriceArray.close) that would be a buy signal and now we assign the word: ”buy” to our signal.
Whenever our signal equals sell and “PositionsTotal” is less than 1 – with other words we don’t have any open position – 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 would use “trade.Buy” to buy 10 micro lot.
In the last step we also create a chart output by using the “Comment” function, it would output the words: “The current signal is:” followed by the signal that we have calculated.
Please don’t forget the closing bracket here and when you are done you can click on the “Compile” button here or press F7. That worked without any errors here so now we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we select our new file: “SimpleIVIDYAStandaloneEA.ex5”, let’s enlarge this here, mark the visualization option and start a test.
…and here is our first trade, our Expert Advisor is working, the signal line is below the candles here so we have a buy signal, it actually opened our first position and now you know how to create a standalone version for the Variable Index Dynamic Average Indicator that actually is going to trade automated and you have created it yourself with a few lines of MQL5 code.