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

ALMA calculation does not prepend with NA's #29

Closed marksimmonds closed 4 years ago

marksimmonds commented 8 years ago

Hi there, when calculating the ALMA moving average, the resulting vector is shorter than the original data by 'n' where n is the length of the moving average..... Checking this against other moving average types, it seems to be that the ALMA resultant vector does not pad the start of the results with an NA.

HTH

joshuaulrich commented 8 years ago

Thanks for the report. Looks like this is due to the difference in rollapply.default and rollapply.xts when it comes to prepending the result with NA. I assume xts objects were used when ALMA was originally written, and NA are prepended in that case.

require(TTR)
require(xts)
data(ttrc)
x <- xts(ttrc[,-1], ttrc[,1]
str(ALMA(ttrc[,"Close"]))
#  num [1:5542] 3.2 3.24 3.28 3.29 3.28 ...
str(ALMA(x[,"Close"]))
# An ‘xts’ object on 1985-01-02/2006-12-29 containing:
#   Data: num [1:5550, 1] NA NA NA NA NA ...
#  - attr(*, "dimnames")=List of 2
#   ..$ : NULL
#   ..$ : chr "Close"
#   Indexed by objects of class: [Date] TZ: UTC
#   xts Attributes:  
#  NULL

The fix might be as easy as setting fill = NA in the call to rollapply.