0

I am new to R, I would like to calculate the below formula in the zoo xts class object.

formula: 10-day sum of [(close- open)/(high-low) *volume] / 10-day sum of volume

Input df:

df <- getSymbols("0005.HK", auto.assign = FALSE, src = "yahoo") 

First I create a new column in the df

df$oneday <- (Cl(df) - Op(df)) / ((Hi(df) - Lo(df)) * Vo(df))  

Then I tried to write a function for the above formula

df$tenday <- rollapplyr(df, 10, function (x) df$oneday/Vo(x), by.column = FALSE, fill = NA)

However, Error comes out,

Error in array(ans, c(len.a%/%d2, d.ans), if (!is.null(names(dn.ans)) ||  : length of 'dimnames' [1] not equal to array extent

Anyone can help, thank you.

Peter Chung
  • 1,010
  • 1
  • 13
  • 31

0 Answers0