how to parse tick data with quantmod? - r

I want to generate daily candle bars using quantmod, but what I got is GBPJPY.csv tick data, like
Symbol,Time,Bid,Ask
GBP/JPY,2013-12-29 17:01:06.000,173.319,173.544
GBP/JPY,2013-12-29 17:01:07.000,173.319,173.459
GBP/JPY,2013-12-29 17:01:08.000,173.319,173.459
GBP/JPY,2013-12-29 17:01:08.000,173.319,173.544
I want to know how to parse it and generate daily candle bar.

First step is to load the ticks into an xts object; second step is to make bars from that xts object.
I will assume your csv file only contains GBP/JPY data (i.e. the first column is redundant). If not, you just need to do a bit more work to use the first column. (Or, better, pre-process your data using unix tools to give you one file per symbol.)
You also didn't say if you want both bid and ask bars, or just one, or mid. The below code assumes bid, as it is simpler.
d=try(read.table(source_fname,sep=',',header=T,stringsAsFactors=F),silent=T)
if(inherits(d,"try-error")){
print(d);quit() #Or some kind of error-handling
}
timestamps = as.POSIXct(d$Time)
bids = xts(d$Bid,timestamps)
Because we only have one column, making daily bars is easy:
to.daily(bids)
It gives:
bids.Open bids.High bids.Low bids.Close
2013-12-29 173.319 173.319 173.319 173.319
If you need to do something more complicated (e.g. make OHLC for both bid and ask), this answer shows how to use period.apply() to apply a custom function to any sized block of ticks: https://stackoverflow.com/a/19896834/841830 (that question also shows how you could use read.zoo() to read the ticks; it is a wrapper around the read.table() function).

Related

How to plot only within a range defined by x number of bars?

I understand that I can contingently plot on the chart using range of date and time values.
However, I would like to be able to plot using a specific number of candlesticks.
For example, I would like to be able to say something like
// Within the most recent 10 bars
// If close[0] > close[1]
// plotshape()
I have tried implementing numerous variations using barstate.isrealtime or barstate.islast but I keep running into limitations.
One major problem is that, although bar_index[#] works by indexing backwards from the most recent bar, the value of bar_index[10] is not 10 but some number in the thousands (depending on the timeframe of the chart — for me its Daily = 2,616 candles, 1hr = 6,217 candles, 15m = 5,222, etc.). In other words, it counts the number of bars from the oldest bar available.
Since referencing of the bars (starting from most recent) and the index values (starting from the oldest) are conflicting--due to counting from opposite ends--I am not sure how to specify: plotshape() for the most recent 10 bars.
I am also running into trouble due to the fact that bar_index[0] occurs every single iteration of the chart's timeframe--so I am getting caught in recursive calculations when trying to do bar_index[0]-bar_index[10].
It seems that what I need is something like bar_index.islast[10]
The reason that I would like to call a plotshape() based on the number of specified candles versus since x date/time (or within date range (x,y)), is because I want my indicator to function properly regardless of which timeframe my chart is displaying:
If I am showing a monthly chart, I want to plot across the last 10 monthly bars; If I am showing a daily chart, I want to plot across the last 10 daily bars; etc.
If I am forced to use a date range, then this functionality breaks down, since I will be shown increasingly more bars for smaller timeframes.
Caveat**
I am able to kinda make this work by specifying the number of bars from the oldest candlestick by stating something like:
bar_index > 2600 ? color=color.black : na
However, given the fact that every single time frame displays a different number of bars, this is not a workable solution for me.
Thanks for any advice.
UPDATE
I have been hunting around and and found that the functionality I desire is already built into the show_last argument of the various plot()functoins.
Ill leave my question posted, in case it helps someone else.
UPDATE I have been hunting around and and found that the functionality I desire is already built into the show_last = int argument of the various plot() functions.
I'll leave my question posted, in case it helps someone else.

hide missing dates from x-axis ggplot2

I am trying to make a chart in ggplot2 (barplot). There are several days in between data points in my data set. I'd like to only graph the dates where variables are and omit the empty dates.
I have been searching for an hour trying to find an answer to this. I found some replies here indicating that the fix is this:
scale_x_date(format = '%d%b', major='days')
However, those arguments inside of scale_x_date don't seem to work anymore. Does anyone have a quick and easy solution?
I would be remiss if I didn't mention the ggplot2 docs which are a fantastic resource, packed full of well-documented examples. They are often the best place to start when you don't know how to do something with ggplot.
The "translation" for the code you quoted above in the current framework is:
scale_x_date(breaks='days', labels=date_format("%d%b"))
which will require the package scales. Unless you have fairly few dates, you probably also would want an adjustment like
theme(axis.text.x = element_text(angle=45))
If you only want to label the dates for which you have data, try
scale_x_date(breaks=df$date, labels = date_format("%m/%d"))
where df$date is the column of your dataframe containing the dates, and of course you can use your preferred date format string.

R/GGPLOT one Choropleth Map from each column in a Data frame

I have a data frame containing 48 hourly observations from each state. State names are in column 1 and columns 2:49 contain the observations for each hour. Each column is being used to generate a Choropleth map of the US for that hour. I have been able to generate a single map with the code as follows where the data frame is named "qso_per_hour".
map_hours<-colnames(qso_per_hour[2:49]) #drop state column, get just hour column names
this_map<-map_hours[18] # hour 18 as an example
#
ggplot(qso_per_hour, aes_string(map_id="states", fill=this_map))+
geom_map(map=states_map, colour="black") # other formatting code trimmed
What I want to do then is generate all 48 hourly maps, each on its own page, and save them off to a graphics file. As a start I am just trying to get them displayed on screen. When I put this code into a simple for loop it no longer draws the map.
map_hours<-colnames(qso_per_hour[2:49]) #drop state column, get just hour column names
for (this_map in map_hours) {
ggplot(qso_per_hour, aes_string(map_id="states", fill=this_map))+
geom_map(map=states_map, colour="black") # other formatting code trimmed
}
When run, an R Graphic:Device window opens but the map is no longer drawn. The window remains gray. I have spent hours searching for an answer to this. It must be something simple. Anyone have any ideas? In debugging I have also put a readline statement at the end of the loop to make it wait for me to say it is ok to go to draw the next map, but that does not solve the issue.
Thanks in advance for any ideas.
Upon further investigation I discovered the problem I was having was an instance of "ggplot does not work in a for loop" which has been addressed several times before. Sorry I don't have the links. The solution is to put the ggplot function within a print() function. Then it works just fine while included in a for loop.

Overlay of multiple time series in Quantmod in R

I want to create a chart using chartSeries with two or more time series on the SAME chart (overlaying each other). Bing/search doc have gotten me nowhere. Seems like should be fairly easy, I tried addTA, but didn't work. I tried the following code.
chartSeries(c(PSEC, ARCC), type = "line")
That resulted into this: (nevermind the fonts, the hell is the green blob?)
The green blob is what you asked R to plot. c(PSEC, ARCC) is the same as rbind(PSEC, ARCC), which creates a single OHLCVA object with two observations per index value (one for PSEC, one for ARCC). You said you tried addTA, but it "didn't work". Saying something "doesn't work" does not help people help you, especially when you don't even show the code that "doesn't work".
Here's a chartSeries call that uses addTA, and a corresponding chart_Series version. Note that chart_Series automatically handles the y-axis range for you.
> chartSeries(Cl(PSEC), TA="addTA(Cl(ARCC), on=1)")
> chart_Series(Cl(PSEC), TA="add_TA(Cl(ARCC), on=1)")
Your data, if it is not daily, could easily look like a green blob.
For example, if that's minute-by-minute prices, I'd expect it to look like this upon visual inspection.

adding a vertical line to a chartSeries graphic

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?
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

Resources