Hello All

I am trying to create the following strategy:

1.) If the underlying security price closes below the lower Bollinger Band for five consecutive days, the oversold/capitulation condition is active.

2.) When the lower Bollinger Band count = 5, the spread between the upper and lower Bollinger Band must be less than 25%.

3.) If condition1 and condition2 and the price of the underlying security close' above the Bollinger Band midline, go long.

I have coded this to the following:

``````
BollTopPrds = Param("BollTopPrds ",20,5,200,5);

BollingerTopWidth = Optimize("BandWidth",2, 0.5, 5, 0.25);

BollBottWdth = Optimize("BollBottWdth",2, 0.5, 2, 0.1);

BollBottPrds = BollTopPrds;

/* Preparation for Entry and Exit */

upperBand = BBandTop(C, BollTopPrds, BollingerTopWidth);

lowerBand = BBandBot(C, BollBottPrds, BollBottWdth);

Bmidline = MA(C,BollTopPrds);

//Entry Signals

Buycon1 = Ref(C,-1) < lowerBand AND Ref(C,-2) < lowerBand AND Ref(C,-3) < lowerBand AND Ref(C,-4) < lowerBand AND Ref(C,-5) < lowerBand;;

``````

This works but is not correct, as the third condition for price exceeding middle line, only triggers if it is the very next bar, that would have to be an explosive move.

My question is what is the best method to code these multi-bar triggers, so that we can say if Buycon1 AND Buycon2 trigger, and in the next X bars BuyCon3 triggers, then trigger overall Buy condition?

I expect it can be done with BarsSince but cant seem to get the right format,

Thanks Max

2 Likes

Magic, didnt no "Hold" exists. Thank you

1 Like

@topmarx ,
You could also take a look at Sum to simplify your 5-day counter:

``````/*Entry Signals
Buycon1 = Ref(C,-1) < lowerBand AND Ref(C,-2) < lowerBand AND Ref(C,-3) < lowerBand AND Ref(C,-4) < lowerBand AND Ref(C,-5) < lowerBand;
*/

// Try this
Sum(Ref(C,-1) < lowerBand, 5) == 5;
``````

FYI, those two upper lines are not equal (in addition you are missing assignment in 2nd one).

``````Buycon1 = Sum(Ref(C,-1) < lowerBand, 5) == 5;
``````

is equal to this one

``````Buycon1 = Ref(C,-1) < lowerBand AND
Ref(C,-2) < Ref(lowerBand,-1) AND
Ref(C,-3) < Ref(lowerBand,-2) AND
Ref(C,-4) < Ref(lowerBand,-3) AND
Ref(C,-5) < Ref(lowerBand,-4);
``````

But following one (your first one) is different than the upper one

``````Buycon1 = Ref(C,-1) < lowerBand AND
Ref(C,-2) < lowerBand AND
Ref(C,-3) < lowerBand AND
Ref(C,-4) < lowerBand AND
Ref(C,-5) < lowerBand;
``````

AFAICS, this one would be shortened version to that latter one

``````Buycon1 = Ref(LLV(C,5),-1) < lowerBand;
``````
2 Likes

@fxshrat ,
I realized I left off the assignment after it was too late to edit.
However, I did not think through the aspect of the lowerBand value incrementing with the bar when using Sum(). Thank you for the thorough explanation.
Your use of LLV is elegant and instructive (in addition to being correct!).

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.