In this video, we are going to create an Expert Advisor that is able to create buy or sell position based on oscillator values. So let’s find out how to do that with MQL4. To get started please click on a little button here or press F4 on your keyboard. Now you should see the Metaeditor window and here you want to click on file, new file, Expert Advisor from template, continue. I will call this file simple. IOBVA. 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.
We start by creating a string variable for the signal, but we are not going to assign a value now. Afterwards, we want to create an array for 100 items. This is a double array. Now let’s use a For loop to go through all the array items and we calculate the value for the array items by using the iOBV function. This will calculate the on balance volume indicator for the current symbol on the chart and the currently selected period on that chart. We use price underscore close for the current counter value and afterwards we are going to add the calculated value to our array.
That’s it for the For loop. Now we need to calculate some values. The first one is the current value for Candle zero. That is calculated by using iOBV for the current symbol on the chart and the currently selected period on that chart. We are using price underscore close.
And we want to calculate the value for Candle zero, that’s the current candle, afterwards, we need to calculate the candle number for the highest candle in our array. And we can do that by using array maximum for the array that we have created. We want to search the whole array starting from item number zero. That’s the first item in our array. And this function will get the number for the highest candle. Therefore, we repeat the whole thing and this time we use array minimum to get the number of the lowest candle, because once we know the number of the candle, we can calculate the value by simply looking into the number of the highest candle, we will get the maximum value.
And it is very similar to get the minimum value by looking into our array for the candle number that we have calculated here. So let’s check for the signal because of the current value for Candle zero is bigger or equal, the max value that we have calculated, we would consider that to be a buy signal and that’s when we assign the word buy to our signal. Otherwise, if the current value is below or equal, the minimum value that we have calculated, that would be a sell signal.
And that’s when we assign the word sell to our signal. And when our signal equals buy and OrdersTotal is zero, that would mean we have no open positions and a buy signal and that’s when we use Ordersend to buy 10 Microlot. In the other case when the signal equals sell and we also have no open orders, we use Ordersend and OP_SELL to sell 10 Microlot. Finally, we use the comment statement. This will create a chart output and it will output the current, the max, the min and the signal value.
And that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other videos in the basic video series first or maybe even the Premium Course on our website might be interesting for you. That’s also where you can find links to source codes in case you don’t want to type everything. But for now, please click on the Compile button or F7 on your keyboard.
You shouldn’t get any errors here. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we use an empty chart and click on insert indicators, volumes, on balance volume. You can go with the default values here and click on OK, here is the oscillator. Now lets rightlick into the chart, select template, save template and we want to save it as tester.tpl.
You can override the current value because this is what we are going to see in the strategy tester. So let’s click on View, strategy tester or press Control and R. Please pick the new filedot Simple IOBVEA.EX4 mark the option for the visual mode here and start a test. Here we are. The Expert Advisor is calculating the current value and as soon as it is below the minimal value, here we see a sell trade on the chart. So our little Expert Advisor is working. And in this little video you have learned how you can create an Expert Advisor that is able to open buy and sell positions based on extreme values of an oscillator.
And you have coded it yourself with a few lines of MQL4 code.