Today we are talking about the advanced envelopes entry.
Look at the two Moving Averages. A signal will be generated as soon as the candles between the two lines are crossing one of the two lines, if the candles are between these lines we don’t have a signal.
Now we shall learn how to code an entry module for the Forex Trading Framework which is able to trade based on the signal that was generated by the Envelopes Indicator.
Let’s begin by clicking on the Metaeditor button here or press F4.
Once it opens you must see the Metaeditor window. Here you see my main module and this is the Forex Trading Framework, and this one here is the demo version of October 2017.
All the modules which are needed to handle each open trade will be imported in this part here.
Next part is where you can set your own trading risk, now it is set to 10 which I consider a very high-risk rate. By default, this rate is set to be 1 but you can select a risk ratio suitable to your account size. The next part is where we are going to import the module called: “CheckEntry” based on the values of the Envelopes Indicator. Maybe you are familiar with how the “OnTick” function works, in this code here it will look for entry signal when calling the “CheckEntry” function we shall code next.
After this the “OnTick” (or check for tick) function will call the trade management which is part of the Forex Trading Framework, then pass the values of the signal and the trading risk we have defined above.
Now, please click on “File /New/ Expert Advisor (template)” from template. Click Next to create the Check Entry module. We give it a name: CheckEntry_Envelopes.mq5. Click Next, Next again and Finish.
Here we have the file and we check the entry by using a string function.
So, we need to create a string first for the signal. We name the variable to be: signal as well.
We do not add any value because we need to calculate it then assign to the function.
We do this be using MqlRates, this array will store the price information value which is on the chart then with ArraySetAsSeries we shall sort the price information array in descending order (that is from the current candle till the oldest candle).
Next, we need to copy the price data into the array. That is done by CopyRates for the current symbol on the chart and the currently sat period on the chart.
We first start the calculation from the current candle (called: candle 0) for 3 candles and the result we store it in the price information array we coded above.
For next we need to create another array for the EA data.
We need an array for each band, so we name them one by one. We call one: UpperBandArray and the other one: LowerBandArray. The two arrays are also calculated in descending order starting with the current candle. For both of the arrays we calculate the values by the use of “ArraySetAsSeries”.
By the use of the built-in function called iEnvelopes we define the Envelopes EA for the current symbol and the current period. We are using the default indicator setting (14 candles) which you see here if you hold the mouse button above one of the lines. The next parameter for the shift we leave it zero for we don’t need it.
Now we need to use MODE_SMA and PRICE_CLOSE for the calculation of the values then the last parameters here is the deviation.
You can check the deviation value if you click on “Insert /Indicators /Trend /Envelopes”.
Moving to the next step we shall use “CopyByffer” for coping the price info into the array.
First, we use buffer zero for the values of the upper band array (UpperBandArray) for 3 candles starting from the current candle, (candle zero) then we continue with buffer 1 for the lower band array (LowerBandArray) for 3 candles also and from the current candle.
It’s time to calculate the EA for candle zero. We get the value for both the upper and the lower band arrays for candle zero.
Let’s determine a buy signal which is when the last candle is closing below the lower band and we have sell signal when the last candle is closing above the upper band. This means that candle 1 has a close price value (in the price information array) which is greater than the upper band value.
We don’t have any signal if the last candle closed between the upper and the lower bands.
So, we have three situations; that is a buy signal, a sell signal or no signal at all.
For next we return the signal to the main module. We do that by the use of the “return” command.
After this we need to save the files making sure we have included the check entry envelopes module in the framework. Then we recompile the code.
No errors being shown here so let’s go back to Metatrader. To start the test, I pick the Forex Trading Framework and a currency pair then press start in the strategy tester.
Here runs the test and now let’s speed it up. There is a lot to calculate so it takes a lot of time, but you already see the first results here.
That’s it. This is the way to create an Expert Advisor which can calculate for you entry signals by the use of the Envelopes Indicator which can then be traded in the Forex Trading Framework.
Can I maximize the results here? Okay, now you have coded it all by yourself in MQL5!