Skip to content

Problems with xts and plotting charts in quantmod

5 messages · Wouter Thielen, Liu, Brian G. Peterson +1 more

#
Hi Carlos,

My explanation was incomplete sorry. You need to format the data correctly
first, and removing the first column is necessary. Also, naming the columns
Open, High, Low, Close also helps. Here is the code that should work (I
tested it):

data <- read.csv("file.csv", header=FALSE)
data.xts <- xts(data[, -1], order.by=as.POSIXct(data$V1, format="%Y%m%d
%H%M%S"))
colnames(data.xts) <- c("Open", "High", "Low", "Close")
chartSeries(data.xts, type="candlesticks")

Note, it is %Y%m%d, not %Y%h%m as in what you put in your example.

It is also good to do a head(data), head(data.xts) after each command to
see how the structure of the data and the xts object changes after each
command, to see it does what you want it to do.

Oh, and please also cc the list (reply-all) so that others can follow :)

Wouter
On Thu Feb 05 2015 at 1:12:17 PM Liu <carloslewlew at gmail.com> wrote:

            

  
  
Liu
#
Hi Wouter,
I appreciate your help and contribution. I followed the above codes and
encountered an error:
*Error in na.omit.xts(x) : unsupported type*

I found other's post on SO with my similar question but unfortunately no
solution yet:
http://stackoverflow.com/questions/28255994/error-in-na-omit-xtsx-unsupported-type-with-chartseries

Just post the output here to see if you can see the problem:
*Error in na.omit.xts(x) : unsupported type*
Date Time         Open      High      Low       Close

2009-03-15 17:00:00 "20090315 170000" " 929.60" " 929.60" " 929.60" "
929.60"
2009-03-15 18:00:00 "20090315 180000" " 925.85" " 927.00" " 925.80" "
926.05"
2009-03-15 18:01:00 "20090315 180100" " 925.80" " 927.30" " 925.80" "
925.90"
2009-03-15 18:02:00 "20090315 180200" " 925.90" " 926.45" " 925.90" "
925.90"
2009-03-15 18:03:00 "20090315 180300" " 925.85" " 926.95" " 925.85" "
926.95"
2009-03-15 18:04:00 "20090315 180400" " 927.20" " 927.20" " 925.65" "
925.80"
An ?xts? object on 2009-03-15 17:00:00/2015-01-30 16:57:00 containing:
  Data: chr [1:2064753, 1:5] "20090315 170000" "20090315 180000" "20090315
180100" "20090315 180200" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "Date Time" "Open" "High" "Low" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ:
  xts Attributes:
 NULL



Thanks,
Carlos
On Wed, Feb 4, 2015 at 11:30 PM, Wouter Thielen <wouter at morannon.org> wrote:

            

  
  
#
at least part of your problem now is that you're storing the data as 
character, rather than numeric.

in your as.xts call, you'll want to remove the first column in the 'x' 
argument,

e.g.

y <- as.xts(x[,-1], order.by=as.POSIXct(x[,1],...))
# the above is not reproducible, it is missing the format argument at least

I also see that you have spaces in your character data, so you may need 
an as.numeric in there too.

Another way to do it would be to make sure that read.csv sees the 
columns as numbers rather than character, which proably has to do with 
your choice of separator.

Regards,

Brian
On 02/05/2015 10:44 AM, Liu wrote:

  
    
#
I've been looking at this today, and I was able to get this working by using a csv file that 
I put the data into, and used the following data and code:
used a csv file:
DateTime,Open,High,Low,Close
20150101 180100,1184.13,1184.44,1184.04,1184.13
20150101 180200,1184.12,1184.41,1184.12,1184.41
20150101 180300,1184.53,1184.54,1184.32,1184.54
20150101 180400,1184.60,1184.60,1184.43,1184.51

code:

library(quantmod)
ColClasses = c("character", "numeric", "numeric", "numeric", "numeric")

ohlcdata <- read.zoo("./data-file.csv", 
???????????????????? index.column = 1, 
???????????????????? sep = ",",
???????????????????? tz='',
???????????????????? format="%Y%m%d %H%M%S", 
???????????????????? colClasses = ColClasses,
???????????????????? header=TRUE)
as.xts(ohlcdata)

chartSeries(ohlcdata,type="candlesticks")

It does produce the chart, and I hope it is what you're looking for.Let me know if you have trouble.
?Tom Clifford MBA '13
Lansing, Michigan 48912
tjclifford at yahoo.com
On Thursday, February 5, 2015 1:25 PM, Brian G. Peterson <brian at braverock.com> wrote:
at least part of your problem now is that you're storing the data as 
character, rather than numeric.

in your as.xts call, you'll want to remove the first column in the 'x' 
argument,

e.g.

y <- as.xts(x[,-1], order.by=as.POSIXct(x[,1],...))
# the above is not reproducible, it is missing the format argument at least

I also see that you have spaces in your character data, so you may need 
an as.numeric in there too.

Another way to do it would be to make sure that read.csv sees the 
columns as numbers rather than character, which proably has to do with 
your choice of separator.

Regards,

Brian
On 02/05/2015 10:44 AM, Liu wrote:

  
    
Liu
#
Thanks to everyone,
I made the chart in the afternoon. In fact the xts obj is not numeric
because the data has been imported and treated "string as character". I
transferred the OHLC data into 4 columns of numeric values and cbind them
together. That solved the problem so I could do the as.xts cast afterwards.

Much appreciation and hope this would help someone in the future too.



On Thu, Feb 5, 2015 at 6:25 PM, Tom Clifford via R-SIG-Finance <
r-sig-finance at r-project.org> wrote: