Your request was interesting, but a bit beyond my skill level, so I asked
Roy Larsen (MSTT) to have a look -- here is what he came up with:
{BB System}
D:=Input("Deviations",0.1,10,.2);
N:=Input("Periods",2,500,62);
BT:=BBandTop(C,N,S,D);
BB:=BBandBot(C,N,S,D);
A:=L>BT; {LOW > BBtop}
B:=H<BB; {HIGH < BBbot}
BW:=BT-BB; {BBands width}
I:=Cum(IsDefined(A+B))<3; {2-bar Init signal}
N:=BarsSince(I+A)<BarsSince(I+B) {BB position latch}
AND Cum(B)>0; {inhibit latch B TRUE once}
Buy:=Ref(N*Alert(N=0,2),-1) {truncate buy signal and delay entry}
AND O>Ref(BT,-1); {check entry price is valid}
BP:=ValueWhen(1,I+Buy,O); {buy price}
{BP set by 2nd I bar to stop N/A flow-on effect on Sell, Stop and Tr variables}
Sell:=C>BP+BW*2; {profit exit}
Stop:=C<BP-BW; {stop loss exit}
Tr:=If(PREV<=0,Buy*O,If(Sell+Stop,-PREV,PREV)); {trade latch variable}
Exit:=Tr<0; {sell on CLOSE signal}
Abs(Tr); {entry price / active trade}
If you use this with the System Tester, I suggest you use the following for signals:
BUY: FmlVar("BB System","Buy");
SELL: FmlVar("BB System","Exit");