Reputation: 31
I can't seem to save chart_Series as pdf using the pdf() and dev.off() inside a for-loop. However, outside the for-loop if I run the code inside the hash box one at a time with incremental value of "i",I am able to produce the graphs. What am I doing wrong?
rm(list = ls(all = TRUE))
setwd("~/Documents/R")
library(tseries)
library(PerformanceAnalytics)
library(quantmod)
library(xts)
library(TTR)
library(Hmisc)
library(Quandl)
Quandl.api_key("yourKeyHere")
startDate = "2012-01-01"
symbols = c(
"ACAD",
"ACM" ,
"AL",
"ALB",
"AMD",
"AMKR",
"AVP",
"BC",
"BID",
"BKD",
"BWA" ,
"BYD" ,
"CAA" ,
"CAR" ,
"CAVM",
"CBL" ,
"CF" ,
"CHK" ,
"CIEN"
)
EOD <- function(symbols, startDate){
for (i in 1:length(symbols)){
stk <-Quandl(paste("EOD/",symbols[i],sep=""), start_date = startDate, type='xts')[,c(8,9,10,11,12)]
sp <-data.frame(Date=index(stk), round(coredata(stk), digits=3))
colnames(sp) <- c("Date","Open", "High", "Low", "Close", "Volume")
setwd("~/Documents/R/STK/csv/eq")
write.csv(sp, paste(paste0(symbols[i]),".csv", sep=""), row.names=FALSE)
}
}
EOD(symbols, startDate)
for (i in 1:length(symbols)){
setwd("~/Documents/R/STK/csv/eq")
stk1 <-getSymbols.csv(symbols[i], src="csv", from = startDate, col.names=c("Open","High","Low","Close","Volume"), auto.assign =FALSE)
colnames(stk1) <- c("Open", "High", "Low", "Close", "Volume")
##########################################################
setwd("~/Documents/R/STK/zs")
name <- paste0(symbols[i])
pdfname <- paste(name, ".pdf", sep="")
pdf(file=pdfname)
chart_Series(stk1 , subset = "2016-01-01::", name = paste0(symbols[i]))
dev.off()
##########################################################
}
######################################################
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.5
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] KFAS_1.2.8 Quandl_2.9.0 reshape2_1.4.2 Hmisc_4.0-3
[5] ggplot2_2.2.1 Formula_1.2-1 survival_2.41-3 quantstrat_0.9.1739
[9] foreach_1.4.3 blotter_0.9.1741 FinancialInstrument_1.2.0 quantmod_0.4-10
[13] TTR_0.23-1 PerformanceAnalytics_1.4.3541 xts_0.9-7 zoo_1.8-0
[17] tseries_0.10-41 timeSeries_3022.101.2 timeDate_3012.100 lattice_0.20-35
loaded via a namespace (and not attached):
[1] splines_3.3.2 colorspace_1.3-2 htmltools_0.3.6 mgcv_1.8-17 base64enc_0.1-3 rlang_0.1.1 foreign_0.8-69
[8] RColorBrewer_1.1-2 plyr_1.8.4 stringr_1.2.0 munsell_0.4.3 gtable_0.2.0 htmlwidgets_0.8 codetools_0.2-15
[15] labeling_0.3 latticeExtra_0.6-28 knitr_1.16 curl_2.6 htmlTable_1.9 Rcpp_0.12.11 acepack_1.4.1
[22] scales_0.4.1 backports_1.1.0 checkmate_1.8.2 jsonlite_1.5 gridExtra_2.2.1 digest_0.6.12 stringi_1.1.5
[29] grid_3.3.2 quadprog_1.5-5 tools_3.3.2 magrittr_1.5 lazyeval_0.2.0 tibble_1.3.3 cluster_2.0.6
[36] Matrix_1.2-10 data.table_1.10.4 httr_1.2.1 iterators_1.0.8 R6_2.2.2 rpart_4.1-11 nnet_7.3-12
[43] nlme_3.1-131
Upvotes: 1
Views: 111