1

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

  • You haven't provided enough information to [reproduce](http://stackoverflow.com/q/5963269/271616) the error, which makes it very difficult to help you. – Joshua Ulrich Sep 19 '14 at 11:56

1 Answers1

0

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")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Now fixed as of r859 on R-forge, so specifying `name = NULL` will work. – Joshua Ulrich Sep 22 '14 at 14:44
  • Thank you Joshua - I will remember to add more detail in future. Your advice has done the trick, however I was using to.periods and not to.minutes. Is there a problem with to.periods? – AlgoTraderNew Sep 23 '14 at 18:49
  • @AlgoTraderNew: `to.minutes` is just a wrapper around `to.period`, so there's nothing wrong with `to.period`. – Joshua Ulrich Sep 23 '14 at 18:53