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

Wouter Thielen wouter at morannon.org
Thu Feb 5 05:30:58 CET 2015


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