Automatically plot (and save) list of xts objects

2019-07-12 01:56发布

I have a long list of xts objects and I'd like to automatically plot and save them to a folder (because it takes a v long time to do manually with >500 plots). The tricky part seems to be to apply functions to lists of xts objects. For example, getting a generalized naming method for the list elements based on its date (each xts object in the list is a unique day), and plotting each object and saving them to a file path with its own name.

The data can be found in a previous post here. Each xts element is a unique day in the list, with a price and volume column. I'm using the packages xts, TTR, and quantmod with the latest R (2.15).

I've tried this code to name the list elements, based on a great post on r-bloggers:

names(sample.data.uniquePOS) <- paste0("sample.data.uniquePOS", lapply(sample.data.uniquePOS, function(x) .indexday(sample.data.uniquePOS)))                                                    

This is supposed to name them by their day index (e.g. if 1st January 2012 then it would be "2012-01-01" as the element name). Unfortunately it doesn't work, producing a list of length sample.data.uniquePOS but each element is named sample.data.uniquePOSnumeric(0). I think the problem is the .indexday being applied to a list, when it should be to an xts object, but I'm not sure how to get around it.

The next step is to produce the file path to save the plots to, and then to do the plots:

mypath <- file.path("C:", "Documents and Settings", 
                              paste("Date_", names(sample.data.uniquePOS), ".jpg", sep = ""))

jpg(file=mypath)
mytitle = paste("my title is", names(sample.data.uniquePOS))
candleChart(sample.data.uniquePOS[[1]]:sample.data.uniquePOS[[length(sample.data.uniquePOS)]])
dev.off()
}

This has the same problem of candleChart needing to be applied to an xts object instead of a list:

Error in try.xts(x, error = "chartSeries requires an xtsible object") : 
  chartSeries requires an xtsible object

I'd really appreciate some help with this!

0条回答
登录 后发表回答