The Bollinger Bandit uses one standard deviation above the 50-day moving average as a potential long entry and one standard deviation below the 50-day moving average as a potential short entry.
When a position is initiated, the protective stop is set at the 50-day moving average. Every day that we are in a position, we decrement the number of days for our moving average calculation by one. The longer that we are in a trade, the easier it is to exit the market with a profit. We keep decrementing the number of days in our moving average calculation until we reach 10. From that point on, we do not decrement.
There is one more element to our exit technique: the moving average must be below the upper band if we are long and above the lower band if we are short.
Previously, we stated that the upper band and lower band were potential buy/sell entries. Potential is the key word. One more test must be passed before we initiate a position; the close of today must be greater than the close of 30 days ago for a long position and the close of today must be less than the close of 30 days ago for a short position. This additional requirement is a trend filter. We only want to go long in an uptrend and short in a downtrend.
bollingerLengths=50
liqLength=50
rocCalcLength=30
avg = average[bollingerLengths]
upBand = avg+std[bollingerlengths]*1.25
dnBand = avg-std[bollingerlengths]*1.25
atr = AverageTrueRange[14](close)
rocCalc = Close - Close[rocCalcLength-1]// {remember to subtract 1}
if(lastsignal<=0 and rocCalc > 0) and close<upBand then
drawarrowup(barindex,low-atr) coloured(0,255,0)
lastsignal=1
endif
if(lastsignal>=0 and rocCalc < 0) and close>dnBand then
drawarrowdown(barindex,high+atr) coloured(255,0,0)
lastsignal=-1
endif
if lastsignal=0 then
liqDays = liqLength
else
liqDays = liqDays - 1
liqDays = Max(liqDays,10)
endif
return