In this video we are going to find out how to create an Expert Advisor that is able to trade buy and sell trades for this Indicator; it’s called the Envelopes Indicator, we have just opened a new position everything is traded automatically and it will also output buy and sell signals directly on the chart.
So how are we going to code it in MQL5?
To do that please click the 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/ Expert Advisor (template)” from template, I will call this one: “SimpleEnvelopeStandaloneExpertAdvisor”, click on “Continue”, “Continue” and “Finish”, let’s delete everything above the “OnTick” function and remove the two comment lines here.
We start by including the file: “Trade.mqh” that comes with MQL5 and afterwards we create an instance from “CTrade” that is called: “trade” and we are going to use it to open new positions later on.
Inside of the “OnTick” function we need to calculate the Ask price and the Bid price that can be done by using: “SymbolInfoDouble” for the current symbol on the chart, with SYMBOL_ASK” we will get the Ask price and with “SYMBOL_BID” we will get the Bid price.
If you click on a currency pair you will see a red and a blue line, the red is the Ask price and the blue is the Bid price. There are some currency pairs with 3 digits behind the dot and others have 5 digits, so we use “NormalizeDouble” and “_Digits” to automatically calculate the right number of digits for the current currency pair.
Now we create a string called: “signal”, we will assign the signal later on, right now it’s empty, let’s create an array for the prices (PriceInformation), we use “MqlRates”, if you mark “MqlRates” and press F1 you will learn that it stores the information about prices, volumes and spread and the array will be called: “PriceInformation”.
We sort the array downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill the price information array (PriceInformation) for the current symbol and the currently selected period on the chart. We start with the current candle 0 (zero) and we will fill it with price information for 3 candles.
The Envelopes Indicator has 2 bands, the blue one and the red one, so we also need two arrays for the upper band array (UpperBandArray) and the lower band array (LowerBandArray), these will also be sorted by using “ArraySetAsSeries” and now we can create our envelopes definition (EnvelopesDefinition) by using the “iEnvelopes” function that is included with MQL5.
This one also needs a few parameters: the first one is for the current symbol on the chart the second one for the period we are using, it will be calculated based on 14 candles and if you click on: “Insert/ Indicators/ Trend/ Envelopes” you will see the 14 candle period here, the shift value is 0 (zero), the deviation is 0.100 and the calculation will be based on the close prices and that’s exactly what we use here for the rest of the parameters.
Now we use “CopyBuffer” to fill our upper and our lower band array according to the envelopes definition (EnvelopesDefinition) that we have created here, the first one will be filled for the buffer 0 (zero), the second one for the buffer 1, we start with candle 0 (zero) and copy the data for 3 candles.
Let’s calculate the current value for the array values (myUpperBandValue and myLowerBandValue), when you hover with your mouse you will see that there is a value that is constantly changing and that’s what we need so we look at candle 0 (zero) in the upper band array (UpperBandArray) and the lower band array (LowerBandArray) and we use “NormalizeDouble” and a 6 to get only 6 digits behind the dot – that’s what “NormalizeDouble” does, its rounding floating point numbers to a specified accuracy – for the first parameter we say what we want to get rounded, the second parameter is for the number of digits we need and if the last candle closed below the lower band so if the close price for candle 1 in the price information (PriceInformation) is below the value for the lower band (myLowerBandValue) that would be a buy signal so we assign the word: “buy” to our signal. Otherwise if the close price for candle 1 in our price information array (PriceInformation) is above the upper band value (myUpperbandValue) that would be a sell signal and now we assign the word: “sell” to our signal. And when the signal equals sell and the function: “PositionsTotal” is below 1 – that expression will tell us that we have no open positions – and if that is the case we use “trade.Sell” to sell 10 micro lot. In the other case if the signal equals buy and “PositionsTotal” is below 1 then we use “trade.Buy” buy to buy 10 micro lot.
Finally we create a chart output by using the “Comment” function that will output the words: “The current signal is:” and the calculated signal.
Okay, that’s it! Please click on the “Compile” button or press F7, now you should see that you have no errors and in that case you can click on the little button here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, we pick our new file: “SimpleEnvelopeStandaloneEA.ex5”, please enable the visualization here and start your test.
…and here is our little Expert Advisor at work, we have already opened the first position and now you know how to automate the Envelopes Indicator and how to create an Expert Advisor that is automatically going to trade it and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE STANDALONE ENVELOPES EXPERT ADVISOR”SimpleEnvelopesStandaloneExpertAdvisor.txt – Downloaded 429 times – 2.04 KB