joshuaulrich / TTR

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

TTR SMA feature request parameter for NA at the end #40

Closed siims-biz closed 7 years ago

siims-biz commented 7 years ago

In the documentation I did not find a parameter to move the NA to the end. Is there one around?

> kurs <- c(1,2,3,4,5,6)
> sma3<-SMA(kurs,n=3)
> sma3
[1] NA NA  2  3  4  5
> 

Default output NA NA 2 3 4 5 With additional parameter the output changes to 2 3 4 5 NA NA

joshuaulrich commented 7 years ago

You didn't find such a parameter in the documentation because one doesn't exist. For technical indicators, it is generally a very bad idea to align the output to anything other than the last observation in the period. Anything else has a very real potential to create look-ahead bias.

If you really want to do this, you should use zoo::rollmean.

R> zoo::rollmean(kurs, 3, fill = NA, align = "left")
[1]  2  3  4  5 NA NA

I'm closing this because I'm not comfortable adding it as a feature, and alternatives alrady exist.