[R] Asking help about drawing and saving candle chart automatically....
박상규
birdfire94 at naver.com
Sun Oct 14 18:12:42 CEST 2012
Hello,
I have trouble with saving and showing candlestick graph.
I want to draw daily 1-minute candle chart per each day and save it
repeatedly.
The data I saved and is used in my code is as the following format:
-----------------------------------------------------
Index,Open,High,Low,Close # header
2011-11-01 9:00:00 ,248.50,248.95,248.20,248.70
...
2011-11-01 15:15:00 ,250.55,250.55,250.55,250.55
2011-11-02 9:00:00 ,245.55,246.25,245.40,245.80
...
2011-11-04 15:15:00 ,252.50,252.50,252.50,252.50
(Some daily data does not exists sometimes. Because it was holyday.)
2011-11-07 9:00:00 ,253.00,253.10,252.60,252.80
...
-----------------------------------------------------
Here is the full source that I coded for the loading csv, drawing chart,
and saving chart as jpeg file.
-----------------------------------------------------
library(xts)
library(quantmod)
rm(list=ls())
aa <- as.xts(read.zoo("E://log.txt", sep=",", FUN=as.chron, header=TRUE))
# convert start date of csv file as julian date
juldate_start <- as.integer(as.Date("2011-11-01"))
for(i in 1:5)
{
# increased date and convert it as Date class again
convdate <- as.Date(juldate_start + i)
# reformat date as recognizable string by quantmod
format(convdate, format="yyyy-mm-dd")
strdate <- as.character(convdate)
print(strdate)
# make daily range
daterange <- paste( strdate, "::", strdate, sep="" )
print(daterange)
jpegname <- paste(strdate, ".jpeg", sep="")
#### jpeg( filename=jpegname, width=1200, height=400 )
tryCatch(chartSeries(aa, subset=daterange, type='candlesticks',
theme=chartTheme('white')))
#dev.list()
#saveChart( 'jpeg' )
#### dev.off()
# wait for user input
cat("\n","Enter x","\n") # prompt
y<-scan(n=1)
}
-----------------------------------------------------
The problems are as the follows:
1. I tried to save chart as the format like '2011-11-02.jpg', '2011-11-
03.jpg', etc. whenever count variable 'i' increases.
But I failed it. I couldn't find how to change name of char file
automatically.
Could you fix my code or give me advice ?
2. The above source code showed daily chart successfully until date of 2011-
11-04.
But it failed to draw chart of 2011-11-07.
Because code tried to find data in 2011-11-05, but it does not exists in
the file 'log.txt'.
How can I handle this problem ?
Thank you in advance.
SK Park
More information about the R-help
mailing list