
In this video we are going to talk about this Indicator, it’s the Bears Power Indicator and we want to create an Expert Advisor that is able to actually produce signals that will be seen right here on the chart and in this case we use the Bears Power Indicator tool trade automated.
Usually this Indicator is used as a filter and you would use another signal and only when the signal and the filter are pointing in the right direction you would open a new position.
To code that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here we want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleBearsPowerStandaloneEA”, click on “Continue”, “Continue” and “Finish”, now you can delete everything above the “OnTick” function and the two comment lines here.
We start by including the file: “Trade.mqh”, this one comes with MQL5 and it contains some trading functions. We need to create an instance from the class: “CTrade” that will be called: ”trade” and we are going to use it to open positions later on.
Inside of the “OnTick” function we calculate the Ask price and the Bid price, that’s done by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will give us the Ask price while “SYMBOL_BID” will give us the Bid price and with “NormalizeDouble” and “_Digits” we calculate the right number of digits after the decimal point.
We also need to create a string for the signal, that string variable will also be called: ”signal” and so far we don’t have any value because that is going to be calculated later.
Let’s create an array for the price data that will be called: “PriceInformation” and we use “MqlRates” to create it because “MqlRates” is the structure that stores the information about prices, volumes and spread. We sort the array downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill the array (PriceInformation) with price data for the current symbol on the chart and the currently selected period. We start with the current candle 0 (zero) and we need price data for 3 candles – that’s enough – and store the result in our price information array (PriceInformation).
Now let’s create another array called: “myPriceArray” this one is for the Expert Advisor data, the other one was for the candle prices.
The second array also needs to be sorted downwards by using “ArraySetAsSeries”.
Now we use the “iBearsPower” function that comes with MQL5 for the current symbol and the currently selected period on the chart, the last parameter 13, is the number of candles that you also will see when you drag the Bears Power Oscillator on your chart.
Now we use “CopyBuffer” for the bears power definition (BearsPowerDefinition) that we have created here to calculate the values for buffer 0 (zero) – that’s the first buffer – from the current candle 0 (zero) for 3 candles and store the result in our price array (myPriceArray) that makes it possible to calculate the current array value by looking at candle 0 (zero) in our price array (myPriceArray) and we store a result in the bears power value (BearsPowerValue) and if the “BearsPowerValue” is above 0 we would consider that to be a buy signal so we assign the word: “buy” to our signal.
Otherwise if the “BearsPowerValue” is below 0 we consider that to be a sell signal so now we assign the word: “sell” to our signal and if the signal equals sell and we have no open positions we use “trade.Sell” to sell 10 micro lot.
Otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” and buy 10 micro lot.
Finally we create a chart output that is done by using the “Comment” function and we want to output the words: “The current signal is:” followed by the calculated “signal”.
Now let’s compile the code, we don’t have any errors and in that case we can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, you will select the “SimpleBearsPowerStandaloneEA”. Let’s also enable the visualization option here and start your test.
…and here is our little example at work, we have buy and sell signals, usually you would use the Bears Power Oscillator to filter another entry signal but now you know how to code a standalone Expert Advisor for Metatrader 5 and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 Tutorial - Simple Bears Power Standalone Expert Advisor”
SimpleBearsPowerStandaloneEA.txt – Downloaded 156 times – 1.71 KB