When I run function CTI(Close ,n=10)
, it show Caused by error:!
cti_10 must be size 5550 or 1, not 5541.
. How to fix it ? Thanks!
library(tidyverse)
library(TTR)
data(ttrc)
ttrc %>% mutate( cti_10 = CTI(Close ,n=10))
This is because you're passing a numeric vector to CTI()
, which can't be converted to an xts object inside CTI()
. CTI()
calls rollapply()
, which doesn't pad the result with leading NA. That's why it has fewer observations than the input. I'll patch TTR so leading NA are added.
In the meantime, you can use this as a work-around. Make sure you have xts version 0.13.1 for as.xts()
to automatically find the Date column in ttrc
.
library(TTR)
data(ttrc)
ttrc$cti_10 <- CTI(xts::as.xts(ttrc)$Close, n = 10)
EDIT: This is fixed on GitHub now and will be included in the next TTR release. You can install the development version with:
remotes::install_github("joshuaulrich/TTR")