[R-SIG-Finance] Problems with xts and plotting charts in quantmod

Liu carloslewlew at gmail.com
Thu Feb 5 17:44:25 CET 2015


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:

> chartSeries(data.xts,subset="2015-01",type="candlesticks")
*Error in na.omit.xts(x) : unsupported type*
> head(data.xts)
                    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"

> str(data.xts)
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:

> 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:
>
>> Hello Wouter,
>> Thank you for your kind response.
>> I should have pasted the data in plain text:
>> V1                        V2        V3         V4        V5
>> 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
>>
>> first column is date and time, then comes OHLC value. There used to be
>> sixth column "Volume“ but I removed it since it's just 0.
>> I followed your help and casted it into an xts object:
>> data<-xts(data,as.POSIXct(data$V1,format="%Y%h%m %H%M%S"))
>>
>> then I ran:
>> chartSeries(data,type="candlesticks")
>> and I got error:
>> Error in na.omit.xts(x) : unsupported type
>>
>> The manual says chartSeries takes in an OHLC object, though the error
>> before said"chartSeries requires an xtsible object". Where do I go from
>> here, to convert the xts into an OHLC or something else?
>> Please help.
>>
>> Thank you very much,
>> Carlos
>>
>>
>> On Wed, Feb 4, 2015 at 8:23 PM, Wouter Thielen <wouter at morannon.org>
>> wrote:
>>
>>> Hi Carlos,
>>>
>>> I can not clearly see where the columns start or stop, but I hope that
>>> the first character (1, 2, 3, 4) is a separate column, and that the
>>> date+time is in the second column. If that is the case, try passing
>>> "index=as.POSIXct(data$V2, format='%Y%m%d %H%M%S')" to your as.xts function:
>>>
>>> as.xts(data, index=as.POSIXct(data$V2, format='%Y%m%d %H%M%S'))
>>>
>>> You may want to skip the first column as it is not part of the OHLC
>>> structure, so as.xts(data[, -1], index=...).
>>>
>>> For more reference, see http://www.stat.berkeley.edu/~s133/dates.html
>>> and
>>> http://stackoverflow.com/questions/18261160/convert-date-time-format-for-use-in-xts
>>>
>>> Hope this helps.
>>>
>>> Wouter
>>>
>>> On Thu Feb 05 2015 at 10:09:38 AM Liu <carloslewlew at gmail.com> wrote:
>>>
>>>> Hi all,
>>>> I'm trying to plot candle charts from OHLC data frame like this:
>>>>  V1V2V3V4V5
>>>> 120150101 1801001184.131184.441184.041184.13
>>>> 220150101 1802001184.121184.411184.121184.41
>>>> 320150101 1803001184.531184.541184.321184.54
>>>> 420150101 1804001184.601184.601184.431184.51
>>>> Tried to cast it to xts by using:
>>>>
>>>> data<-read.csv(file path)
>>>> chartSeries(as.xts(data),type="candlesticks")
>>>>
>>>> Then I have the error "
>>>>
>>>> Error in as.POSIXlt.character(x, tz, ...) :
>>>>   character string is not in a standard unambiguous format
>>>>
>>>> "
>>>> I am not sure what that means. Maybe it's saying that my date and time
>>>> is
>>>> non standard string? What should I do?
>>>> Please help.
>>>>
>>>> Thanks
>>>> Carlos
>>>>
>>>>         [[alternative HTML version deleted]]
>>>>
>>>> _______________________________________________
>>>> R-SIG-Finance at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>> -- Subscriber-posting only. If you want to post, subscribe first.
>>>> -- Also note that this is not the r-help list where general R questions
>>>> should go.
>>>>
>>>
>>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list