fork download
  1. library(FinancialInstrument)
  2. library(TTR)
  3. library(PerformanceAnalytics)
  4. symbols.close<-read.table("s.txt",header=T, row.names=c("time"))
  5. monthly.returns <- ROC(symbols.close, n = 1, type = "discrete", na.pad = TRUE)
  6. RankRB <- function(x){
  7. r <- as.xts(t(apply(-x, 1, rank, na.last = "keep")))
  8. return(r)
  9. }
  10.  
  11. MonthlyAd <- function(x){
  12. sym <- sub("\\..*$", "", names(x)[1])
  13. Ad(to.monthly(x, indexAt = 'lastof', drop.time = TRUE, name = sym))
  14. }
  15.  
  16. CAGR <- function(x, m){
  17. x <- na.omit(x)
  18. cagr <- apply(x, 2, function(x, m) prod(1 + x)^(1 / (length(x) / m)) - 1, m = m)
  19. return(cagr)
  20. }
  21.  
  22. SimpleMomentumTest <- function(xts.ret, xts.rank, n = 1, ret.fill.na = 3){
  23. lag.rank <- lag(xts.rank, k = 1, na.pad = TRUE)
  24. n2 <- nrow(lag.rank[is.na(lag.rank[,1]) == TRUE])
  25. z <- max(n2, ret.fill.na)
  26. lag.rank <- as.matrix(lag.rank)
  27. lag.rank[lag.rank > n] <- NA
  28. lag.rank[lag.rank <= n] <- 1
  29. mat.ret <- as.matrix(xts.ret) * lag.rank
  30. vec.ret <- rowMeans(mat.ret, na.rm = TRUE)
  31. vec.ret[1:z] <- NA
  32. vec.ret <- xts(x = vec.ret, order.by = index(xts.ret))
  33. f <- list(mat = mat.ret, ret = vec.ret, rank = lag.rank)
  34. return(f)
  35. }
  36.  
  37. roc.three <- ROC(x = symbols.close , n = 3, type = "discrete")
  38. rank.three <- RankRB(roc.three)
  39.  
  40. roc.six <- ROC(x = symbols.close , n = 6, type = "discrete")
  41. rank.six <- RankRB(roc.six)
  42.  
  43. roc.nine <- ROC(x = symbols.close , n = 9, type = "discrete")
  44. rank.nine <- RankRB(roc.nine)
  45.  
  46. roc.twelve <- ROC(x = symbols.close , n = 12, type = "discrete")
  47. rank.twelve <- RankRB(roc.twelve)
  48.  
  49. num.assets <- 4
  50.  
  51. case1 <- SimpleMomentumTest(xts.ret = monthly.returns, xts.rank = rank.three,
  52. n = num.assets, ret.fill.na = 15)
  53.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class, interface, or enum expected
library(FinancialInstrument)
^
Main.java:13: error: unclosed character literal
  Ad(to.monthly(x, indexAt = 'lastof', drop.time = TRUE, name = sym))
                             ^
Main.java:13: error: unclosed character literal
  Ad(to.monthly(x, indexAt = 'lastof', drop.time = TRUE, name = sym))
                                    ^
3 errors
stdout
Standard output is empty