Reputation: 151
I have a financial timeseries dataset of 1 minute OHLCV observations that I would like to plot using quantMod's chartSeries function. Here is a reproducible example:
ES = structure(list(
ES.Time = structure(c(1126042320, 1126042380, 1126042440, 1126042500,
1126042560, 1126042620, 1126042680, 1126042740,
1126042800, 1126042860, 1126042920, 1126042980,
1126043040, 1126043100, 1126043220),
class = c("POSIXct", "POSIXt"), tzone = ""),
ES.Open = c(1114.25, 1114.5, 1114.5, 1114.25, 1114.25, 1114.25, 1114.25,
1114.25, 1114.25, 1114, 1114, 1114, 1114.25, 1114, 1114.25),
ES.High = c(1114.5, 1114.75, 1114.5, 1114.5, 1114.5, 1114.25, 1114.25,
1114.25, 1114.25, 1114.25, 1114.25, 1114, 1114.25, 1114, 1114.25),
ES.Low = c(1114.25, 1114.25, 1114.25, 1114.25, 1114.25, 1114.25, 1114,
1114.25, 1114, 1114, 1114, 1114, 1114, 1114, 1114.25),
ES.Close = c(1114.25, 1114.5, 1114.5, 1114.25, 1114.5, 1114.25, 1114.25,
1114.25, 1114, 1114.25, 1114, 1114, 1114, 1114, 1114.25),
ES.Volume = c(98, 423, 131, 274, 46, 20, 218, 13, 14, 43, 15, 8, 56, 2, 10)
), class = "data.frame", row.names = c(NA,-15L))
ES = xts(ES, order.by = ES$ES.Time)
chartSeries(ES)
When I run this code I get the following error: Error in (function (cl, name, valueClass) : assignment of an object of class “character” is not valid for @‘yrange’ in an object of class “chob”; is(value, "numeric") is not TRUE
I thought maybe it was trying to plot ES.Time. So I removed that column and called as.numeric() on all the other columns just to make sure, but I'm still getting the same error.
ES = subset(ES, select=-c(ES.Time))
ES$ES.Open = as.numeric(ES$ES.Open)
ES$ES.High = as.numeric(ES$ES.High)
ES$ES.Low = as.numeric(ES$ES.Low)
ES$ES.Close = as.numeric(ES$ES.Close)
ES$ES.Volume = as.numeric(ES$ES.Volume)
Can someone please help me figure out what I need to do to get the plot working? Thanks
Upvotes: 1
Views: 188
Reputation: 269824
The problems are:
Use this code
library(quantmod)
ES <- xts(cbind(open, high, low, close, volume), as.POSIXct(datetimes))
chartSeries(ES)
or if what you are starting out with is a data.frame with character datetimes or if the data comes from a file read.zoo can read files as well.
DF <- data.frame(format(datetimes), open, high, low, close, volume)
ES <- as.xts(read.zoo(DF, tz = ""))
chartSeries(ES)
Upvotes: 1