MQL5 Tutorial – Simple Triple Moving Average Robot


This is the Trix Indicator.
It is an Oscillator, that means that most of the time it is used with an indicator to predict if a trend is going to continue.
If the value of the Trix Oscillator is above zero that could be a sign for long trend, and if it’s below zero, that could mean that we have a short trend.
According to Wikipedia, the Trix Oscillator was developed in the 1980s by a guy called Jack Hutson and a rising or falling line is an uptrend or downtrend.
And Trix shows the slope of that line so it’s positive for a steady uptrend, negative for a downtrend and a crossing through zero is a trend change.
And if we zoom in, we see that the crossing here actually was the start of a big short trend.
Now how can we create an Expert Advisor that is going to use the Trix Indicator?
To do that, please click on the little button here or press the F4 key.
That would show the Meta Editor and here you want to click on File, New, Expert Advisor from template, Continue.
I will call it SimpleTrix, Continue, Continue and Finish.
And now you can remove everything above the Ontick function and we are going to delete the comment lines.
And now we want to create an Array for several prices. It will be double Array with the name My Price Array.
And now we want to define the properties for the iTrixEA.
For our iTrix Definition we will use iTrix, that is a command that comes with MQL 5, we will use it for the current currency pair on the chart and the selected period on the chart and it will calculate a value based on the last fourteen candles based on the close price.
Because that is the default value when you click on properties.
Here we have the period of fourteen candles and here is the Close Price.
Now that we have made our definition, we want to sort the Price Array from the current candle downwards that is done by using Array Set as Series for our Price Array we have created here.
Now we want to fill it with data by using CopyBuffer according to the iTrix Definition we have created here.
Our indicator has just one line, that’s the zero here and we want to fill it with values from the current candle, that’s candle zero, for three candles, that’s the three here and we want to store the results in our Price Array.
And now we want to get the value of the current candle. I will use a float variable here, I call it iTrix value and it will hold the value of candle zero.
That’s the current candle of our Price Array.
So let’s create a chart output depending on the value and if our calculated iTrix value is greater than zero, we want to use the comment function to show the text iTrix is long and the iTrix value directly on our chart.
In the other case the iTrix value is below zero, we use comment to show the text iTrix is short and the iTrix value.
Okay that’s it, please click on compile or press the F7 key on your keyboard, that should work without any errors.
We have one warning here, it says we could have a possible loss of data and that’s because we have used the float type here.
But that’s okay for this simple example. So if your compilation process was successful, please click on the little button here or press F4 to go back to MetaTrader.
And in Meta Trader you want to click on View, Strategy Tester or press Control and R.
And in the Strategy Tester you want to select the file Simple iTrix.ex5, mark the visualization option here and click on start.
And here we are, our chart says iTrix is long and we have a little long trend here and when it crosses the line here it says iTrix is short.
Okay now you know how to create an Expert Advisor that is using the iTrix Indicator to show you a a long or a short trend and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 - Simple iTrix Tripple Moving Average Robot” SimpleITriX.txt – Downloaded 39 times – 786 B

Learn how to create your own MQL5 trading robot for $1
Sign Up Now