joshuaulrich / TTR

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

WMA does not return xts object #96

Closed joshuaulrich closed 3 years ago

joshuaulrich commented 4 years ago

Thanks to Cory Fletcher for reporting this issue via email.

There's a bug in WMA() when an xts object is passed in for x that begins with NA values (with default wts=1:n parameter). The return value will be a vector rather than an xts object as it should be, because of the call to x <- na.omit(x). This means that x and ma now have different lengths, and the final call to reclass(ma,x) fails to produce an xts object.

Minimal, reproducible example

x <- xts(x = c(NA,1:3), order.by = Sys.Date()+1:4)
wma <- WMA(x, 2)
str(wma)  # vector, but should be xts