CODING A BOLLINGER BAND RE-ENTRY EXPERT ADVISOR STEP BY STEP CONTINUED:
Now I need to go back up to
if (!IsNewBar()) return; and begin to capture some values from the candle that has just closed, starting with the candle’s closing price. So, here I am calling the
iCLose function, passing the
Symbol and the
Period, and I am looking at bar number
1, the bar that has just closed.
And then I also want the high and the low values, because if I do execute a trade, I will be trading buy just above the high of the bar that is closed, and I will be trading sell just before the low of the bar that was closed. So accordingly, I am calling the
iHigh and the
iLow functions.
Again, I am passing in the chart symbol and period, and I am looking at bar number
1. So, this will give me the high, low, and close values of the bar that has just closed.
Next, I want to get the value of the upper line of the Bollinger band...
So, I have declared a type
double and named it
upper1, because this is going to be the upper bar that has just closed. There is an inbuilt technical indicator function called
iBands that takes arguments of the
Symbol and the time frame (
Period). I also input the number of periods for the bands (
InpBandsPeriods) as well as the number of deviations (
InpBandsDeviations). There is also a shift which is more applicable if you are viewing this on screen, which I set to zero (
0) because I do not need it. On the next line I input the applied price as well (
InpBandsAppliedPrice), after which I called
MODE_UPPER to get the upper band. And finally, I am saying I want this for bar number
1, which of course, is the bar that just closed.
I also want the lower band (
lower1). Again, this is type
double and is almost exactly the same, except that here I am changing
MODE_UPPER to
MODE_LOWER, and that will give me the lower Bollinger band.
And then because I will be making a comparison with the bar before this in order to see if the asset has first closed outside the Bollinger bands and then closed back inside the Bollinger bands, I also want to calculate the close for the bar number 2. This is the same as before, but I am just using 2 as the input for bar number.
double close2 = iClose(Symbol(), Period(), 2)
Again, I want the upper and lower bands for bar number 2, and will again have mode upper and mode lower. But, at the very end of these lines of code, I am passing bar number 2 rather than bar number 1.
And now I need a test for trading: If bar number 2 closed above the upper band number two, AND bar number 1 closed below the upper band number 1, then I am looking at a re-entry from above, and that means that I am executing sell...
if (close2>upper2 && close1<upper1)
So then, I am going to call my open order function, passing in order type sell stop. And the price that I am going to be executing the sell stop at is low number 1, which is the low of the bar just closed. And I am also passing in this difference between the upper and the lower, because I want to calculate the take profit and stop loss based on number of bands.
OpenOrder(ORDER_TYPE_SELL_STOP, low1, (upper1-lower1))
This open order is not another custom function, so I am going to go down and look at that now before I go any further...
This is the beginning of the open order function. It is a type integer because it will return the ticket number for the order that is opened.
First argument is the
order type that I want to execute. Then I have the
price that I want to open the order at, and the
channel width, which on line #78 you will see I passed in upper1 minus lower1...
...so, it is the total channel width.
Then I want to calculate the size of one deviation, because my take profit and stop loss are based on one deviation.
So, single deviation is the total channel width divided by two times the input for number of deviations. So, by default, I am saying that I am setting the bands at two deviation. That means that the upper band is two above the middle, and the lower band is two below the middle. So, that would give me a total width of four deviations.
(A single deviation is two times the band deviation.)
So then, the take profit amount is just a single deviation multiplied by the number of take profit deviations, and the stop loss size is a single deviation multiplied by the number of stop-loss deviations.
And then I am also calculating the expiry time. (Remember that these stop orders are going to expire at the end of the current candle.) So, for the expiry time I am getting the iTime ( Symbol, Period, ); which is the
open time for the
current chart symbol for the
current period for bar number
0.
And
PeriodSeconds with no arguments "()" will give me the total number of seconds in this bar. And then I am just subtracting one second (-
1). So, this is for the bar start time plus the number of seconds in this bar, minus one, which means that this expiry time is just before the next bar opens.