[R-SIG-Finance] [Q] How to omit NA values in TTR package + Jeff Cooper's 5 day MOM

Joshua Ulrich josh.m.ulrich at gmail.com
Tue Mar 3 20:11:41 CET 2015


On Tue, Mar 3, 2015 at 12:55 PM, Alex Badoi <alex.badoi1 at gmail.com> wrote:
> 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)
>
You say NA values are causing the problems, but the error says it's an
NA, NaN, or Inf.  na.omit will not remove Inf, so that's likely the
problem.  Use !is.finite to find non-finite values in your data.

>
>
> 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)
>
There is no reason for you to call try.xts.  As ?try.xts says, it (and
reclass) are meant for developers... and TTR functions already call
try.xts internally, so you don't need to.

It's also really bad practice to subset columns by number.  Use column
names, or the convenience functions in quantmod (e.g. Cl(), Op(),
etc.)

> 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]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-SIG-Finance mailing list