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

How to calculate week/month MACD? #51

Closed Shicheng-Guo closed 7 years ago

Shicheng-Guo commented 7 years ago

I hope to calculate weekly or monthly MACD, therefore, I need a function to transfer daily data to weekly or monthly data and then to calculate MACD. However, it seem I am not familiar with ‘xts’ object. and Hope Joshua could give some help.

daily2week<-function(Object){ Object1<-split(na.omit(Object),rep(1:round(nrow(na.omit(Object))/7),each = 7)) Open=unlist(lapply(Object1,function(x) unlist(x[1,1]))) High=unlist(lapply(Object1,function(x) max(unlist(x[,2])))) Low=unlist(lapply(Object1,function(x) max(unlist(x[,2])))) Close=unlist(lapply(Object1,function(x) max(unlist(x[,2])))) Volume=unlist(lapply(Object1,function(x) max(unlist(x[,2])))) output<-data.frame(Open,High,Low,Close,Volume) return(output) }

Thanks.

Shicheng-Guo commented 7 years ago

Or How to transfer data.frame to ‘xts’ object? any function to do that?

joshuaulrich commented 7 years ago

I'm closing this because it's not reporting an issue or feature request. This issue tracker is not the place to ask for help or discuss general R programming advice. Instead, ask your question on Stack Overflow or the R-SIG-Finance mailing list (you must subscribe to post).

Look at to.weekly() and to.monthly() in xts to aggregate data to a lower frequency.