MQL5 TUTORIAL – SIMPLE ITRIX STANDALONE EXPERT ADVISOR
Now how can we create an Expert Advisor that is able to automatically open trades for the signal here?
To do that please click on the little button here or press F4 in your Metatrader, 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 one: “SimpleItrixStandaloneExpertAdvisor” , click on “Continue”, “Continue” and “Finish”.
Now we can delete everything above the “OnTick” function and let’s also remove the two comment lines here.
We start by including the “Trade.mqh” file that one comes with MQL5 and it helps us to create an instance of the class “CTrade” that will be called: “trade” and we are going to open trades with it later on.
Inside the “OnTick” function we start by calculating the Ask and the Bid price, we get that by using “SymbolInfoDouble” for the current symbol and with “SYMBOL_ASK” we will get the Ask price and “SYMBOL_BID” will deliver us the Bid price.
We also use “NormalizeDouble” and “_Digits” to calculate the right number of digits behind the dot for each currency pair. With “MqlRates” we create a price info (PriceInfo) array that will hold our price data, first we use “ArraySetAsSeries” for the price info array (PriceInfo) to sort the price array from the current candle downwards and afterwards we use “CopyRates” for the current symbol and the currently selected period on the chart to fill the price array (PriceInfo) with price data. We start at candle 0 (zero) – that’s the current candle – we copy the price info for 3 candles and store the values in the price info array (PriceInfo).
Let’s create a string variable for the signal that will also get the name “signal” but we don’t assign a value here because that needs to be calculated later on, first we need to create another array. This one will hold the price data for the Indicator (myPriceArray), MQL5 comes with the built-in “iTriX” function that will help us to create the definition for our Expert Advisor here. We are also using the current symbol, the currently selected period on the chart, 14 is the number of candles, you will also see it in the brackets here and the calculation will be done for the close prices.
Once again we use “ArraySetAsSeries” to sort the price array (myPriceArray) from the current candle downwards and now we use “CopyBuffer” according to the itrix definition (iTriXDefinition) that we have created here to fill our price array (myPriceArray) with data for the buffer 0 (zero) – that’s the red line here – and we do that from the current candle 0 (zero) for 3 candles.
Now we can get the value for the current candle by looking into candle 0 (zero) of our price array (myPriceArray) and assign the value to the variable itrix value (iTriXValue) and if that “iTriXValue” is above 0 that would be a buy signal so we assign the word: “buy” to our signal.
Otherwise if the “iTriXValue” is below 0 that would be a sell signal and in that case we assign the word: “sell” to our signal.
If the signal equals sell and “PositionsTotal” is below 1 – in other words we don’t have any open positions – that’s when we want to 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 use the “Comment” function to output the sentence: “The current signal is:” followed by the calculated “signal” directly on our chart.
Okay, that’s it, please click on: “Compile” or press F7, that worked without any errors here so now I 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 CTRL and R, here we pick the file: “SimpleItrixStandaloneExpertAdvisor”, mark the visualization option and start a test.
…and here we are! We already have opened a trade here and now you know how to create a standalone version that is able to trade the Triple Exponential Average, output the buy and sell signals directly on the chart and you have coded it yourself with a few lines of MQL5 code.