inputs:avgLength(20);
inputs:atrLength(20);
inputs:NumDevs(2);
inputs:NumATRs(1.5);

vars:movAvgVal(0);
vars:BBupBand(0);
vars:BBdnband(0);
vars:KLupBand(0);
vars:KLdnBand(0);

movAvgVal = Average(close,avgLength);

BBupBand = Bollingerband(close,avgLength,+NumDevs);
BBdnBand = Bollingerband(close,avgLength,-NumDevs);

KLupBand = movAvgVal + AvgTrueRange(atrLength)*NumATRs;
KLdnBand = movAvgVal -  AvgTrueRange(atrLength)*NumATRs;

if movAvgVal < movAvgVal[1]
and BBupBand < KLupBand
and BBdnband > KLdnBand
and close cross under BBdnBand
then sellshort("BBsell") this bar on close;

if marketposition = -1
then  buytocover next bar at movAvgVal stop;