joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
330 stars 103 forks source link

runMAD should check for NA if user specifies center argument #25

Open joshuaulrich opened 8 years ago

joshuaulrich commented 8 years ago

It's possible for a user to specify a center argument that has more NA than the number of NA in x + n - 1. This can even happen internally, for example with CCI:

R> require(TTR)
R> data(ttrc)
R> cci <- CCI(ttrc$Close, maType="DEMA")
Error in runMAD(HLC, n, center = mavg, stat = "mean") : 
  NA/NaN/Inf in foreign function call (arg 2)

This is because DEMA has more NA than n, due to double-smoothing.

R> dema <- DEMA(ttrc$Close, n=10)
R> runMAD(ttrc$Close, n=10, center=dema)
Error in runMAD(ttrc$Close, n = 10, center = dema) : 
  NA/NaN/Inf in foreign function call (arg 2)
R> sum(is.na(dema))
[1] 18

One possible solution is to set NAs to max(sum(is.na(x)), sum(is.na(center))) when center is user-specified. Note that we should also check for non-leading NA in center, as we do forx`.