Thanks for helping me. I want to create functions of indicators. It work on TTR::SMA but not for TRR::ATR or TTR::Donchian Channel
Here is the example of what's work. (SMA)
# Create SMA function
n = 10
sma <- function(x, return_format = "tibble", n = n) {
# Convert tibble to xts
if (!is.xts(x)) {
x <- xts(x[,-1], order.by = x$Date)
}
# Get SMA
sma_xts <- SMA(x = x$Adjusted, n = n) # I want sma on Adjusted Price
# Rename
names(sma_xts) <- "SMA"
# Return in xts format if tibble is not specified
if (return_format == "tibble") {
sma <- fortify.zoo(sma_xts, names = "Date")
} else {
sma <- sma_xts
}
sma
}
However, when I want to apply this with ATR or DonchianChannel, it showned NA.
# Create DonchianChannel
n = 10
dc <- function(x, return_format = "tibble", n = n) {
# Convert tibble to xts
if (!is.xts(x)) {
x <- xts(x[,-1], order.by = x$Date)
}
# Get DonchianChannel
dc_xts <- DonchianChannel(x = c(x$High,x$Low), n = n) # <<< I think here is the problem
# Rename
names(dc_xts) <- "DC"
# Return in xts format if tibble is not specified
if (return_format == "tibble") {
dc <- fortify.zoo(dc_xts, names = "Date")
} else {
dc <- dc_xts
}
dc
}
My best guess is the input of function was wrong. Because SMA need x but DonchianChannel need HL. So I have tried many format but I still didn't get it.
SMA(x, n = 10, ...)
ATR(HLC, n = 14, maType, ...)
DonchianChannel(HL, n = 10, include.lag = FALSE)
Thank you guys for enlightening me :) I'm trying to be better.