In this video we are going to create a standalone Expert Advisor that is able to trade the Relative Vigor Index, it’s the little Indicator here, we have an open trade right now.
Now we want to find out how to create this EA for Metatrader 5.
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” from template (template), “Continue” (Next). I will call this file: “SimpleRVIStandaloneEA”, click on “Continue” (Next), “Continue” (Next) and “Finish”.
Now you can remove everything above the “OnTick” function and the two comment lines here.
First we need to include the trade library by including the file: “Trade.mqh”. Afterwards we create an instance for the CTrade class that will be called: “trade”.
In the “OnTick” function we create a string variable for the signal that will also be called: “signal” but we don’t assign a value here because we are going to calculate that later on.
First we need to get the Ask price and the Bid price. That is done by using “SymbolInfoDouble” for the current symbol on the chart. 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 number of digits after the decimal point.
You see there are currency pairs with 5 digits and other currency pairs with 3 digits behind the dot and now that we have calculated Ask and Bid price we want to create an array for the price data.
The first one will be named: “myPriceArray0”, the second will be named: “myPriceArray1”.
Now let’s define the properties for the RVI Expert Advisor by using the built in function: ”iRVI” that comes with MQL5 for the current symbol and currently selected period on the chart for 10 candles.
If you take a look at the Indicator here you will also see the 10 and when you click on: “Insert /Indicators /Oscillators /Relative Vigor Index” you will see that the default period here is 10 candles, so that’s what we use here.
Now we sort the array0 (myPriceArray0) and the array1 (myPriceArray1) from the current candle downward, that is done by using “ArraySetAsSeries” for our two price arrays.
With “CopyBuffer” we fill our price array according to the RVI definition (RVIDefinition) that we have created here for buffer 0 or buffer 1 from the current candle for 3 candles and store the result in our two arrays here.
After we have done that we are actually able to calculate the current value for our price array by looking at candle 0. The RVI Indicator uses 3 digits behind the dot, so we once gain use “NormalizeDouble” and a 3 to get that right.
And because we need 2 lines, let’s also do that for the 2nd array.
Let’s create a buy signal! We want to buy if the RVI value 0 (RVIValue0) is below the RVI value 1 (RVIValue1) and if both values are below the zero line – the zero line is the dotted line here – and in that case we assign the word: “buy” to our signal.
Otherwise if the RVI value 0 (RVIValue0) is greater than the RVI value 1 (RVIValue1) and if both values are above the zero line we assign the word: “sell” to our signal.
Whenever our signal equals sell and “PositionsTotal” is below 1 – so we have no open position – we want to sell 10 micro lot.
In the other case if the signal equals buy and we have no open positions we want to buy 10 micro lot.
In the last step we use the “Comment” function to output the word: “Signal:” followed by our calculated signal directly on our chart.
Okay. When you are done you can click on the “Compile” button or press F7 on your keyboard; that should work without any errors here and in that case you can click on a button here or press F4 to go back to Metatrader.
In Metatrader we click on “View/ Strategy Tester” or press CTRL and R, and here we select the “SimpleRVIStandaloneEA.ex5” file, enable the visualization mode here and click on “Start”.
…and here is our Expert Advisor at work and it already made a trade.
If this all was too fast for you or if you want to learn more you can visit my website to download the source code and you will find this and other videos.
You could also take part in the courses and in this little video you learned how to calculate the Relative Vigor Index and you have created a standalone Expert Advisor that is actually going to buy and sell ad and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE RVI STANDALONE EXPERT ADVISOR”SimpleRVIStandAloneEA.txt – Downloaded 293 times – 2.03 KB