[R-SIG-Finance] Interesting behaviour in BBands
Joshua Ulrich
josh.m.ulrich at gmail.com
Mon Feb 20 14:52:58 CET 2012
Hi Stergios,
On Sat, Feb 18, 2012 at 5:52 PM, Stergios Marinopoulos
<stergios_marinopoulos at yahoo.com> wrote:
> There's an interesting behavior in BBands when dn, mavg, and up are the same
> value. Here's an example:
>
'dn' and 'up' are functions of 'mavg' and the rolling standard
deviation of the series. So the real problem is that the standard
deviation is zero for some observations.
>
> library(quantmod);
>
> getSymbols("SPY", from="1993-01-31", to="1996-01-01" ) ;
> bb <- BBands(Cl(SPY), n=3, sd=0.3, maType=EMA) ;
>
>
> bb[bb$pctB==Inf,]
> dn mavg up pctB
> 1993-04-16 44.92297 44.92297 44.92297 Inf
> 1995-02-09 48.25296 48.25296 48.25296 Inf
>
>
> And here's the graphic display for the fist Inf occurrence:
>
>
> chartSeries(SPY["1993-04-10::1993-04-20",])
> addTA(bb,on=1)
>
>
> Notice pctB is Inf when the dn & up are coincident with mavg. I don't think
> Inf is the right interpretation here, as the mavg line is not Inf % between
> the up and down. Would zero be a better interpretation here?
>
First, _thank_you_ for the reproducible example. That makes it much
easier for me to investigate and help.
This is a direct result of how Bollinger Bands and pctB are calculated:
sdev <- runSD(HLC, n, sample = FALSE)
up <- mavg + sd * sdev
dn <- mavg - sd * sdev
pctB <- (HLC - dn)/(up - dn)
Where HLC=Cl(SPY) in your example. I'm not aware of any special
methods to handle cases when the past 'n' days have the same price and
cause the standard deviation to be zero. Note that this wouldn't be
an issue if you actually provide HLC data to the function, instead of
only close prices.
>
> Thanks for your thoughts.
>
> --
> Stergios Marinopoulos
>
Best,
--
Joshua Ulrich | FOSS Trading: www.fosstrading.com
R/Finance 2012: Applied Finance with R
www.RinFinance.com
More information about the R-SIG-Finance
mailing list