テクニカル分析 ボリンジャーバンド(Bollinger Bands)

Equation
Y_{t}=(\sum^{n-1}_{s=0}X_{t-s})/n
\{X_{t}\}は高値、安値および終値の算術平均値.

\hat{\sigma}_{t}=\sqrt{(\sum^{n-1}_{s=0}(X_{s}-\bar{X}_{t})^{2})/n},\;\bar{X}_{t}=(\sum^{n-1}_{s=0}x_{s})/n

Y_{t}\pm\hat{\sigma}_{t},\;Y_{t}\pm2\hat{\sigma}_{t}

R
サンプルデータ:2013年3月27日から2014年3月27日までの日経平均株価
nikkeihlcbbnikkeihlc

> library(TTR)

> nikkei.high <- dataset$High
> nikkei.low <- dataset$Low
> nikkei.close <- dataset$Close

> plot(nikkei.high,type="l",ylim=c(12000,17000),main="Nikkei.2013/3/27-2014/3/27",ylab="JPY")
> par(new=T)
> plot(nikkei.low,type="l",ylim=c(12000,17000),main="",ylab="",xlab="",col=2)
> par(new=T)
> plot(nikkei.close,type="l",ylim=c(12000,17000),main="",ylab="",xlab="",col=4)

> nikkei.bb <- BBands(dataset[,c("High","Low","Close")],n=25,maType="SMA",sd=2)

> summary(nikkei.bb)
       dn             mavg             up             pctB        
 Min.   :11983   Min.   :13140   Min.   :14023   Min.   :-0.2088  
 1st Qu.:13035   1st Qu.:13953   1st Qu.:14895   1st Qu.: 0.2785  
 Median :13720   Median :14345   Median :15238   Median : 0.5581  
 Mean   :13582   Mean   :14448   Mean   :15314   Mean   : 0.5546  
 3rd Qu.:14093   3rd Qu.:14810   3rd Qu.:15847   3rd Qu.: 0.8601  
 Max.   :15243   Max.   :15773   Max.   :16658   Max.   : 1.2523  
 NA's   :24      NA's   :24      NA's   :24      NA's   :24       

> plot(nikkei.bb[,1],type="l",main="Nikkei.Bollinger Bands",ylim=c(12000,17000),ylab="JPY")
> par(new=T)
> plot(nikkei.bb[,2],type="l",main="",ylim=c(12000,17000),ylab="",xlab="",col=2)
> par(new=T)
> plot(nikkei.bb[,3],type="l",main="",ylim=c(12000,17000),ylab="",xlab="",col=4)
> par(new=T)
> plot(dataset$Close,,type="l",main="",ylim=c(12000,17000),ylab="",xlab="",col=3)
 
> attributes(nikkei.bb)
$dim
[1] 246   4

$dimnames
$dimnames[[1]]
NULL

$dimnames[[2]]
[1] "dn"   "mavg" "up"   "pctB"

参考文献
熊谷悦生、舟尾暢男(2008).『Rで学ぶデータマイニング Ⅱシミュレーション編』.オーム社.248pp.

アプリケーション
R Core Team (2013). R: A language and environment for statistical computing.
R Foundation for Statistical Computing, Vienna, Austria.
URL http://www.R-project.org/.