4

I have a OHLC data frame, and trying to calculate the Bollinger Bands without charting witin R.

The below works but i'm looking to create a new data frame containing BB levels.

head(stock)
                   minutes.Open minutes.High minutes.Low minutes.Close

2014-08-04 01:00:00      102.561      102.581     102.486       102.537
2014-08-04 05:00:00      102.536      102.677     102.530       102.673
2014-08-04 09:00:00      102.668      102.713     102.537       102.597
2014-08-04 13:00:00      102.591      102.656     102.578       102.578
2014-08-04 17:00:00      102.570      102.572     102.438       102.487
2014-08-04 21:00:00      102.481      102.584     102.460       102.584

chartSeries(stock)
addBBands()

Please help to point me in the right direction.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
user3773444
  • 333
  • 3
  • 12

2 Answers2

6

If you look at the source code for addBBands(), you'll see that it calls BBands. Try this:

BBands(HLC(stock))
GSee
  • 48,880
  • 13
  • 125
  • 145
  • @user3773444 Try `RSI(Cl(stock))`. It takes a univariate price series, instead of high, low, close. See `help(package="TTR")` for all the function in the TTR package (Technical Trading Rules) – GSee Aug 20 '14 at 02:56
3

The addBBands function returns a silent chobTA object which can be accessed using attr:

x=addBBands()
bands=attr(x,"TA.values")

I'd assume the dn and up columns correspond to the bands.

Joe Bender
  • 41
  • 2
  • head(bands) dn mavg up pctB 2014-08-03 23:00:00 NA NA NA NA 2014-08-04 01:00:00 NA NA NA NA 2014-08-04 03:00:00 NA NA NA NA 2014-08-04 05:00:00 NA NA NA NA 2014-08-04 07:00:00 NA NA NA NA 2014-08-04 09:00:00 NA NA NA NA > – user3773444 Aug 19 '14 at 03:30
  • 1
    Have you noticed that when the Bollinger bands are displayed on the chart, they aren't present for the early time points? The NA rows are there because addBBands() needs 19 previous time points to calculate the moving average. The 20th row is the first row with actual numbers. – Joe Bender Aug 19 '14 at 04:56