In this video we are looking at this little Indicator, it’s the Onbalance Volume Indicator.
Investopedia (www.investopedia.com) says that the Onbalance Volume should show us that whenever the Indicator increases without a significant price change the price should follow and now we are going to find out how to automate the Onbalance Volume Indicator in MQL5.
To do that please click on a little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here we click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleIOBValue”, click on “Continue”, “Continue” and “Finish”.
Now you can delete everything above the “OnTick” function and let’s also remove the two comment lines here.
This is a very simple Indicator, we start by creating an array for the Expert Advisor (EAArray), this one will be a double array so it can hold floating type values, now we use “ArraySetAsSeries” to sort the array (EAArray) from the current candle downwards and to define the Onbalance Volume we use the included function that is called: “iOBV”, so let’s mark that and press F1, here we see that it uses the current symbol on the chart, the currently selected period on the chart and the third parameter is called: applied volume so let’s click on this link and we see that we could use the tick volume or the real volume. In our case we use “VOLUME_TICK”.
Now we use “CopyBuffer” to fill our Expert Advisor array (EAArray) according to the IOBV definition (IOBVDefinition) that we have created here for the first buffer… well, a buffer is in our case this line here, some Indicators have more than one buffer for example if I would “Insert/ Indicators/ Trend/ Bollinger Bands”, this one has three different lines, so it uses 3 buffers and the first buffers is always called buffer 0 (zero), the second buffer would be number one and so on. That’s because arrays are zero based in MQL5 so the first item in an array is always called by calling the index zero.
Let’s copy the data for 3 candles and the result will be stored in our EA array (EAArray).
To calculate the current Onbalance Value (IOBVVALUE) we now need to look at the value for candle 0 (zero) in our EA array (EAArray), remember this one is the current candle so this would be candle 0 (zero), candle 1, candle 2, and so on.
Finally we want to create a chart output that’s done with the “Comment” function and it will output the words: “The IOB Value is:” followed by the calculated value, so let’s compile that and this worked without any errors, we only have a minor warning – you see the little triangle here – I have used an int value, let’s try double recompile the code and now the warning is gone, so now that everything is finished we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester”, I select a new file: “SimpleIOBValue”, mark the visualization option here and start a test.
…and here we are, this is the value and it’s also drawn on the chart here as you can see, it has one digit behind the dot and this digit is always zero so if I use an integer here recompile the code I will get the warning again but when I restart the test you will see that the digit and the dot are gone, I prefer it this way.
…and now you know how to calculate the Onbalance Volume and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE ONBALANCE VOLUME EXPERT ADVISOR”SimpleIOBValue.txt – Downloaded 149 times – 580.00 B