dimanche 12 février 2017

Saving printed values in for() function as variable?

Vote count: 0

I'm really new to R and I'm still trying to wrap my head around for() functions. My sample dput is linked here (I apologize for the length; I tried to shorten it as much as possible). I'm working on a small project, and I have the following function:

new.trend <- function(MergedData)
{
  ret <- as.list(rep(NA, length(MergedData))) 
  ma.sig <- ma.crossover(MergedData)
  pricebreak <- price.channel(MergedData)
  sig <- intersect(which((ma.sig[1,])==1), which(!pricebreak[1,]==0))
  for (i in sig) { #Calculates output variables based on active signals
    x <- MergedData[[i]]
    x <- xts(x[,-1], order.by=x[,1])
    dev20 <- (x[,4]-SMA(x[,4], n=20))/x[,4]*100
    dev50 <- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
    RSI <- RSI(x[,4], n=14)
    ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L)))
  }
  na.omit(print(ret))
}
print(new.trend(MergedData))

The issue/problem

return(ret) is returning this:

> new.trend(MergedData)
[[1]]
             EUR.LAST EUR.LAST.1      EMA
2017-02-09 -0.6968559  0.3526983 44.68176

[[2]]
[1] NA

[[3]]
             GBP.LAST GBP.LAST.1      EMA
2017-02-09 -0.1920461   1.027927 52.27664

[[4]]
            CHF.OPEN CHF.OPEN.1      EMA
2017-02-09 0.5066387 -0.7241689 52.56533

[[5]]
[1] NA

[[6]]
[1] NA

[[7]]
[1] NA

[[8]]
[1] NA

[[9]]
[1] NA

[[10]]
[1] NA

[[11]]
[1] NA

[[12]]
[1] NA

[[13]]
           PLN.CLOSE PLN.CLOSE.1      EMA
2017-02-09 0.2824105   -1.569392 48.24069

[[14]]
[1] NA

[[15]]
           TRY.CLOSE TRY.CLOSE.1      EMA
2017-02-09 -2.315328  -0.2501765 42.52731

[[16]]
             ZAR.CLOSE ZAR.CLOSE.1      EMA
2017-02-09 -0.09598239   -1.492148 46.06286

[[17]]
[1] NA

[[18]]
            CLP.CLOSE CLP.CLOSE.1      EMA
2017-02-09 -0.2433194   -2.112368 40.93616

[[19]]
[1] NA

[[20]]
           MXN.CLOSE MXN.CLOSE.1      EMA
2017-02-09 -2.460443   -3.490762 34.67792

[[21]]
            PEN.CLOSE PEN.CLOSE.1      EMA
2017-02-09 -0.4138617   -1.974541 37.84737

[[22]]
             CNY.CLOSE CNY.CLOSE.1      EMA
2017-02-09 -0.08749199  -0.5004658 44.39283

[[23]]
            IDR.CLOSE IDR.CLOSE.1      EMA
2017-02-09 -0.4064827   -0.631571 35.91677

[[24]]
           INR.CLOSE INR.CLOSE.1      EMA
2017-02-09 -1.291429   -1.594705 21.83156

[[25]]
            KRW.CLOSE KRW.CLOSE.1      EMA
2017-02-09 -0.8529425   -2.840274 34.61214

[[26]]
           MYR.CLOSE MYR.CLOSE.1      EMA
2017-02-09 0.1407816  -0.4020273 49.80231

[[27]]
           SGD.CLOSE SGD.CLOSE.1      EMA
2017-02-09  0.123548  -0.7103133 49.73621

[[28]]
           PHP.CLOSE PHP.CLOSE.1      EMA
2017-02-09 0.1355443    0.236601 55.61772

[[29]]
           THB.CLOSE THB.CLOSE.1      EMA
2017-02-09 -0.518655   -1.396926 23.51997

But I want to retrieve what is printed in the for() function, such as this:

> for (i in sig) { #Calculates output variables based on active signals
+     x <- MergedData[[i]]
+     x <- xts(x[,-1], order.by=x[,1])
+     dev20 <- (x[,4]-SMA(x[,4], n=20))/x[,4]*100
+     dev50 <- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
+     RSI <- RSI(x[,4], n=14)
+     print(ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L))))
+ }- (x[,4]-SMA(x[,4], n=50))/x[,4]*100
        RSI <- RSI(x[,4], n=14)
        print(ret[[i]]<- na.omit(merge(tail(dev20, n=1L), tail(dev50, n=1L), tail(RSI, n=1L))))
      }

             EUR.LAST EUR.LAST.1      EMA
2017-02-09 -0.6968559  0.3526983 44.68176
             GBP.LAST GBP.LAST.1      EMA
2017-02-09 -0.1920461   1.027927 52.27664
            CHF.OPEN CHF.OPEN.1      EMA
2017-02-09 0.5066387 -0.7241689 52.56533
           PLN.CLOSE PLN.CLOSE.1      EMA
2017-02-09 0.2824105   -1.569392 48.24069
           TRY.CLOSE TRY.CLOSE.1      EMA
2017-02-09 -2.315328  -0.2501765 42.52731
             ZAR.CLOSE ZAR.CLOSE.1      EMA
2017-02-09 -0.09598239   -1.492148 46.06286
            CLP.CLOSE CLP.CLOSE.1      EMA
2017-02-09 -0.2433194   -2.112368 40.93616
           MXN.CLOSE MXN.CLOSE.1      EMA
2017-02-09 -2.460443   -3.490762 34.67792
            PEN.CLOSE PEN.CLOSE.1      EMA
2017-02-09 -0.4138617   -1.974541 37.84737
             CNY.CLOSE CNY.CLOSE.1      EMA
2017-02-09 -0.08749199  -0.5004658 44.39283
            IDR.CLOSE IDR.CLOSE.1      EMA
2017-02-09 -0.4064827   -0.631571 35.91677
           INR.CLOSE INR.CLOSE.1      EMA
2017-02-09 -1.291429   -1.594705 21.83156
            KRW.CLOSE KRW.CLOSE.1      EMA
2017-02-09 -0.8529425   -2.840274 34.61214
           MYR.CLOSE MYR.CLOSE.1      EMA
2017-02-09 0.1407816  -0.4020273 49.80231
           SGD.CLOSE SGD.CLOSE.1      EMA
2017-02-09  0.123548  -0.7103133 49.73621
           PHP.CLOSE PHP.CLOSE.1      EMA
2017-02-09 0.1355443    0.236601 55.61772
           THB.CLOSE THB.CLOSE.1      EMA
2017-02-09 -0.518655   -1.396926 23.51997

Tips, suggestions and pointers would be much appreciated! I understand that a for() function may not be ideal, but it's the only way I know how to be able to cycle through only the MergedData values that correspond to the indices in sig.

asked 25 secs ago

Let's block ads! (Why?)



Saving printed values in for() function as variable?

Aucun commentaire:

Enregistrer un commentaire