[R-SIG-Finance] [Q] How to omit NA values in TTR package + Jeff Cooper's 5 day MOM
Alex Badoi
alex.badoi1 at gmail.com
Tue Mar 3 19:55:42 CET 2015
Hi everyone,
I am working on a trading system based on Jeff Cooper's 5 day Momentum.
http://www.maxdown.org/e-books-list/the-5-day-momentum-method-by-jeff-cooper_4fa8l.html
I am new to R and still familiarising myself with a lot of the functions
but i am slowly piecing it together. The code bellow contains all the
calculations necessary for the above strategy.
I have the following problems:
1. NA Values are making my code useless as it stops half way through. I
need to run this on a few hundred stocks. It works perfectly with no NA
values. I tried to use na.omit and na.rm=T.
Alternatively i am looking for a way through which i could search through
my stockData environment for vectors containing NA values and remove these
vectors entirely before running it through the following code.
Error:
Error in runSum(x, n) : NA/NaN/Inf in foreign function call (arg 1)
7 runSum(x, n)
6 runMean(x, n)
5 SMA(structure(c(NA, NA, NA, NA, NA, NA, NA, 29.75, 34.6666666666667,
22.75, 22.75, 26.25, 52.5, 45.5, 52.5, 45.5, 52.5, 52.5, 52.5,
59.5, 59.5, 59.5, 35, 35, 35, 35, 59, 59, 59, 59, 59, 66, 59,
66, 117, 117, 117, 117, 131, 117, 131, 131, 72.5, 43.25, 39.75, ...
4 do.call(maType, c(list(fastK), list(n = nFastD, ...)))
3 stoch(try.xts(x[, c(2, 3, 6)], error = as.matrix), nFastK = 8,
na.action = na.exclude)
2 FUN(X[[9L]], ...)
1 lapply(stockData, MOM)
Code:
stockData <- new.env()
getSymbols(ticker_names , env = stockData, src="yahoo")
"MOM" <- function(x){
# Price Adj.
price <- x[,c(6)]
# DIp DIn DX ADX
adx <- ADX(try.xts(x[,c(2,3,6)], error=as.matrix), n =14)
DIp <- adx$DIp
DIn <- adx$DIn
Adx <- adx$ADX
# dn mavg up pctB
bbands <- BBands(try.xts(x[,c(2,3,6)], error=as.matrix), n =14, sd=1.8)
down <- bbands$dn
up <- bbands$up
sma <- SMA(try.xts(x[,c(6)], error=as.matrix), n = 20)
# rsi
rsi <- RSI(try.xts(x[,c(6)], error=as.matrix), n = 14)
# fastK fastD slowD
stochastics <- stoch(try.xts(x[,c(2,3,6)], error=as.matrix), nFastK = 8)
fastK <- stochastics$fastK
# results
result <- cbind( price, DIp, DIn, Adx, down, up, sma, rsi, fastK )
lastresult <- do.call("rbind", list(last(result)))
write.table(lastresult, file = "MOM.csv", append = T, col.names = F, sep=
",")
print(last(result))
}
# Loop function on data #
lapply (stockData, MOM)
I spend the last few hours searching for a solution but with little
success. Thank you in advance if you can help.
Alex
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list