4

I hope this isn't redundant as I've googled extensively and still have not found an answer. I'm plotting intraday data and want to place a vertical line at a specific point in time. It seems I have to use the function addTA but it always plots below my graph in some weird empty white space. Here's some sample code and data. Thanks for any help.

Data:

date,value
29-DEC-2010:00:02:04.000,99.75
29-DEC-2010:00:03:44.000,99.7578125
29-DEC-2010:00:05:04.000,99.7578125
29-DEC-2010:00:07:53.000,99.7421875
29-DEC-2010:00:07:57.000,99.71875
29-DEC-2010:00:09:20.000,99.7421875
29-DEC-2010:00:11:04.000,99.75
29-DEC-2010:00:12:56.000,99.7421875
29-DEC-2010:00:13:05.000,99.7421875

Code:

#set up data
data    = read.csv("foo.csv")
values  = data[,2]
time    = c(strptime(data[,1],format="%d-%b-%Y:%H:%M:%S",tz="GMT"))
dataxts = xts(values, order.by=time,tzone="GMT")

# chart data
chartSeries(dataxts)
# add vertical line - this is where I have no clue what's going on.
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT"),on=1))

What ends up happening is that I get a vertical line where I want it, 2010-12-29 00:11:00, but it sits in a new section below the graph instead of overlaid on it. Any ideas?

user1229681
  • 107
  • 2
  • 6
  • 1
    Why not to use `abline(v=7)` for example? – agstudy Feb 15 '13 at 22:29
  • This is helpful but I'd like to avoid having to find the index of the time. Is there a way that involves using something of class POSIXlt to get the vertical abline? – user1229681 Feb 16 '13 at 00:50
  • @agstudy `abline` is not really compatible with the `chartSeries` framework. It does not update the `chob`, so if you zoom or add new TAs, lines drawn with `abline` will be lost. – GSee Feb 16 '13 at 16:12
  • @GSee Oops..good point :) – agstudy Feb 16 '13 at 16:12

1 Answers1

5

You're passing on as an argument to xts, but you should be passing it to addTA.

I think you mean to be doing this:

addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT")),on=1)

That said, it still doesn't work for me with your sample data. However, if what you had works with your real data except it puts the line in a new panel, then this should work and not open a new panel.

I have an addVLine function in my qmao package that is essentially the same thing.


Edit

Aside from the typo with the parenthesis, the xts object also needs a column name in order for addTA to work (I think... at least in this case anyway). Also, you must give an x value that exists on your chart (i.e. 11:04, not 11:00)

colnames(dataxts) <- "x"
chartSeries(dataxts)
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:04",tz="GMT")),on=1, col='blue')
# or
# library(qmao)
# addVLine(index(dataxts[7]))

Edit 2

Perhaps a better approach is to use the addLines function

addLines(v=7)

Or, if you know the time, but don't know the row number, you could do this

addLines(v=which(index(dataxts) == as.POSIXlt("2010-12-29 00:11:04", tz="GMT")))

which gives

enter image description here

GSee
  • 48,880
  • 13
  • 125
  • 145
  • @agstudy, see edit. At its core, the code is the same. The main visible difference in this example is that `addVLine` uses `on=-1` by default so that the line on the original chart is not hidden behind the vertical one. `addLine` can also be called like `addVLine("days")` if you want a vertical line at the end of each day for example. – GSee Feb 16 '13 at 16:06
  • thank's for the clarification. but here you plot `2010-12-29 00:11:04` and not `2010-12-29 00:11:00` as asked in the OP. (need to convert the scale to seconds). That's said I think that "financing speaking" , the vertical line should be something like an Expiry or barrier so it must be defined in the current chart. (but you say the same think in your answer, so +1) – agstudy Feb 16 '13 at 16:17
  • @agstudy, right. I don't know how to plot a line at a point in time that does not occur in the data. (I can shade over it, but I can't plot a line at exactly 11:00 without adding that row to the data) – GSee Feb 16 '13 at 16:22
  • it is a quantmod limitation. great specialized package but the idea of creating the chob object based in the base graphics is really limiting . It was be better to use the `grid` package like `lattice` and `ggplot2` ...maybe an idea for `quantmodGrid` package.. – agstudy Feb 16 '13 at 16:30
  • @agstudy, aren't grid graphics really slow? e.g. you wouldn't want to try to plot a chart of tick data with ggplot2 – GSee Feb 16 '13 at 16:42
  • I think that ggplot2 it is slow ! but I don't think that grid is great and equivalent to base graphics.. – agstudy Feb 16 '13 at 16:49
  • I ended up using addLines(v=first(which(index(dataxts) >= as.POSIXlt(paste(date," 00:11:00",sep=""), tz="GMT")))) to place a line as close to 11am as possible without being early. Thanks for all the suggestions! – user1229681 Feb 18 '13 at 23:22