简体   繁体   中英

XTS object from Quantmod

Hi I would like to know how I can apply the same code that works for a single ticker with an XTS Object that has many tickers.

This is the code that works for 1 tickers (Close Price):

getSymbols("IYR",from="1995-01-01",to="2014-01-01")

adj<-IYR$IYR.Adjusted
rtnM<-ROC(adj)[2:length(adj)]
r05<-rtnM[rtnM<= -.05]
plot(sort(r05),type='o',main='US ETF Drops 1995-present returns <= -5%')
100*sort(r05)

I seem to be struggling with these 2 lines when applying it to my full ticker list

mb<- sub[sub<= -.05]
plot.xts(sort(mb),type='o',main='US ETF <= -5%')

I apologise if I haven't made myself clear this is a first post.

> head(sub)
           DIA.Close    EEM.Close    EFA.Close    EWZ.Close    FXI.Close    GLD.Close

    GSG.Close
2012-01-03  0.0145024539  0.030116502  0.026891372  0.039461948  0.031336368  0.025528326  0.035154052
2012-01-04  0.0027463668 -0.005642487 -0.005913678  0.003678319 -0.014839976  0.005053908  0.004672906
2012-01-05 -0.0001613424 -0.004382015 -0.014939029 -0.013610224  0.006747285  0.006804694 -0.015267472
2012-01-06 -0.0033942169 -0.012739026 -0.013740366 -0.010031539 -0.014677091 -0.003682778  0.004428052
2012-01-09  0.0010518226  0.010411338  0.003858265  0.020800615  0.021100797 -0.004462870  0.001177510
2012-01-10  0.0058055315  0.021517224  0.015086276  0.020376751  0.022299616  0.013581474  0.007620201

> mb<- sub[sub<= -.05]
Error in `[.xts`(sub, sub <= -0.05) : 'i' or 'j' out of range

Assuming you want to plot together for each ticker the sorted values that are less than -5%,

As stated above by @GSee, the first two lines

adj<-IYR$IYR.Adjusted
rtnM<-ROC(adj)[2:length(adj)]

could be combined to remove the NA's of the first line this way:

#
rtnM <- na.omit(ROC(IYR[,6]))  # remove all NA's 

Using your xts object sub that holds per column the ROC's,

# get a list per column, with all the fields that are <= -0.05
answer.ls <- lapply(1:ncol(sub), function(i) sub[sub[,i] <= -0.05, i])

If you work directly with a list, then your function would be even simpler:

# import libraries
library(xts)
library(quantmod)
library(RColorBrewer)

mySymbols <- c("IYR", "DIA", "EEM", "EFA", "EWZ", "FXI", "GLD", "GSG")

# get symbols
getSymbols(mySymbols,
           from = "1995-01-01",
           to = "2014-01-01")

# apply ROC to the last column of each of them, 
# get() will allow to access the object using the character name
sub.ls <- lapply(mySymbols,
                 function(symbol) na.omit(ROC(get(symbol)[,6])))

# check dimensions (all are different)
lapply(sub.ls, dim)

answer.ls <- lapply(sub.ls,
                    function(sub.ticker) sub.ticker[sub.ticker[,1] <= -0.05, 1])

# each ticker has a different dimension
lapply(answer.ls, dim)

Then just plot it:

# plot each list
require(RColorBrewer)

# create colors palette
pal <- brewer.pal(length(answer.ls), "Set3")

# plot all series 
plot(sort(coredata(answer.ls[[1]])),
     type   = "o",
     col    = pal[1])
for(i in 2:length(answer.ls)){
   lines(sort(coredata(answer.ls[[i]])),
         col = pal[i])
}
legend("bottomright",
       legend  = mySymbols,
       col     = pal,
       pch     = c("o", rep("-", length(answer.ls) - 1)),
       lwd     = 4)

Don't forget coredata() before sorting the XTS objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM