简体   繁体   中英

ADX function result incorrect in r

When I use the ADX function, I'm not getting the correct answer. For example, the ADX(14) value from 10/4/2017 is 12.87. The code below gives me 9.53. Any ideas why this is off?

require(quantmod)
tickers<-c('SPY')
getSymbols(tickers, from="2017-08-24")
ADX(HLC(SPY))
                DIp      DIn         DX      ADX
2017-08-24       NA       NA         NA       NA
...
2017-09-14 21.60949 13.54557 22.9381443       NA
2017-09-15 20.47286 20.68483  0.5150181       NA
2017-09-18 22.77659 19.99196  6.5109140       NA
2017-09-19 22.36879 19.63402  6.5109140       NA
2017-09-20 21.26106 21.31324  0.1225536       NA
2017-09-21 20.51171 20.56204  0.1225536       NA
2017-09-22 19.97997 20.75146  1.8940939       NA
2017-09-25 18.72051 23.47425 11.2661824       NA
2017-09-26 18.64682 22.54754  9.4690476       NA
2017-09-27 20.81017 20.92800  0.2822906       NA
2017-09-28 20.03528 20.14872  0.2822906       NA
2017-09-29 23.03483 19.02773  9.5265361       NA
2017-10-02 26.60939 18.03780 19.1984916       NA
2017-10-03 28.57002 17.44596 24.1743580 8.058099
2017-10-04 30.09667 16.66099 28.7347243 9.535001

Did you try using ema or wma? You can do that by specifying maType = EMA

ADX(HLC(SPY), n = 14, maType = EMA)

I just bumped into the same problem. In my case it was the pricing source used. When using quantmod/src="yahoo", there's NO problem :

library(xts)
library(quantmod)
library(TTR)

days_prior <- 100
n <- 0 # n-day prior closing

symbol <- c("SPY") # input ticker
Asset <- getSymbols (symbol, src="yahoo", from = Sys.Date()-days_prior, auto.assign = FALSE) # src : "google", "yahoo", "oanda"
Asset <- Asset[,2:4]
ADX_index <- ADX(Asset, n = 14)
ADX_quantmod <- cbind(Asset, ADX_index)
View(ADX_quantmod[nrow(ADX_quantmod),])

However, when using Quandl as a pricing source, the TTR:ADX uses the Adjusted Prices and causes large deviations. The solution is to force the HLC function to use the Unadjusted Prices instead:

library(xts)
library(TTR)
library(Quandl)

source <- "EOD/"
ticker <- "SPY"
symbol <- paste(source, ticker, sep = "")

Asset <- Quandl(symbol, type = "xts", start_date = Sys.Date() - days_prior, end_date = Sys.Date()-n)
Asset <- HLC(Asset)[,c(1,3,5)] # use the unadjusted prices!
ADX_index <- ADX(Asset, n = 14)
ADX_quandl <- cbind(Asset, ADX_index)
View(ADX_quandl[nrow(ADX_quandl),])

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM