Search code examples
rxtsquantmod

to.period returns error attempt to set index 4/4 in SET_STRING_ELT in R


I am a newbie with R and have been following a tutorial from YouTube by Chris Reeves
I am using xts with to.period to create OHLC from tick/second data.

My code

Last = AAPL.Last
Bid = AAPL.Bid
Ask = AAPL.Ask

colnames(Last) = c("TimeStamp","Price","Size")
colnames(Bid) = c("TimeStamp","Price","Size")
colnames(Ask) = c("TimeStamp","Price","Size")

Last$TimeStamp = strptime(Last$TimeStamp, "%Y%m%d %H%M%S")
Bid$TimeStamp = strptime(Bid$TimeStamp, "%Y%m%d %H%M%S")
Ask$TimeStamp = strptime(Ask$TimeStamp, "%Y%m%d %H%M%S")

require("xts")
xtsLast = as.xts(Last$Price,order.by=Last$TimeStamp,frequency=NULL)
xtsAsk = as.xts(Ask$Price,order.by=Ask$TimeStamp,frequency=NULL)
xtsBid = as.xts(Bid$Price,order.by=Bid$TimeStamp,frequency=NULL)

require("quantmod")
chartSeries(xtsLast)

bars = to.period(xtsLast,
                 period = "seconds",
                 k=60,
                 indexAt = "startof",
                 name = NULL,
                 OHLC = TRUE):
require("quantmod")
chartSeries(bars)

The error that is returned is:

Error in to.period(xtsLast, period = "seconds", k = 60, indexAt = "startof", : attempt to set index 4/4 in SET_STRING_ELT

I have googled an answer but I am unable to get a complete answer so hence asking it here. I apologize if this a very basic question.

I am working Windows 7 using RStudio with Rx64 3.1.1.

Thanks, H


Solution

  • This works if you don't set name = NULL.

    require(quantmod)
    base_url <- "http://www.reevesresearch.com/codes/TickDataAAPL/"
    Last <- read.table(paste0(base_url, "AAPL.Last.txt"), sep=";")
    colnames(Last) <- c("TimeStamp","Price","Size")
    Last$TimeStamp <- strptime(Last$TimeStamp, "%Y%m%d %H%M%S")
    xtsLast <- xts(Last$Price, order.by=Last$TimeStamp)
    bars <- to.minutes(xtsLast, k=1, indexAt="startof")