Previous Bollinger Band Breakout and close higher

I have coded a Bollinger Band breakout strategy with an Index filter using Amibroker as:

SetOption("MaxOpenPositions", 20);
SetPositionSize(5, spsPercentOfEquity);

Index = Foreign("$XAO", "C", True);
IndexMA = MA(Index, 75);

BollyTop = BBandTop(C, 100, 3);
BollyBot = BBandBot(C, 100, 1);

Buy = C >= BollyTop AND Index >= IndexMA;
Sell = C <= BollyBot;

I want to modify it to generate a buy if there was a bollinger band breakout in the previous 7 days and today's close is higher. Any suggestions?

Hi and welcome to the forum.

On this forum it is expected that you should be willing to do some searching, work to become reasonably familiar with AFL functions and have a go at coding something yourself. Your post suggests you are already on this path.

To see if there has been any previous signals you can just Sum() them over a period and look for a total above zero. Another possibility here is to use BarsSince(). Try both and use what makes sense to you.

Then perhaps use ValueWhen(), it will give the value of Close (or whatever) for the most recent of the breakout signals. To cover off what I see as a possible meaning (but its unspecified) where you want the highest of all the signals over the 7 days then look though the function reference for a way to get the highest of an array over a defined period.

Always try to cover all possible interpretations of your question.

Let us know how you go.

Thanks for the direction. I think this is close but it's not making a difference in the back testing results.

SetOption("MaxOpenPositions", 20);
SetPositionSize(5, spsPercentOfEquity);
 
Index = Foreign("$XAO", "C", True);
IndexMA = MA(Index, 75);
 
BollyTop = BBandTop(C, 100, 3);
BollyBot = BBandBot(C, 100, 1);
 
numBars = BarsSince(C >= BollyTop AND Index >= IndexMA);
closingPrice = ValueWhen(C >= BollyTop AND Index >= IndexMA, Close, 1);
 
Buy = numBars <= 7 && C >= closingPrice;
Sell = C <= BollyBot;
1 Like

Ok, this is where the rubber meets the road.
You are going to have to start using debugging to find situations in your data that are supposed to give signals but do not, and vice versa. Only you know what it is you want in detail.
Please familiarise yourself with these methods - debugging.