[R-SIG-Finance] Interpolation of RBloomberg historical intraday bar data to force time periodicity

PBrakenhielm patrik at brakenhielm.se
Fri Dec 23 10:56:11 CET 2011


Hi,

First of all, I want to thank all the contributors at this forum, without
you, I would not have been able to do anything in R.

Now, for my issue.

Using RBloomberg, I fetch historical intraday bar data using the below code.
Currently I have chosen close per minute as I do not wish to lose to much
data.

library(RBloomberg)
conn <- blpConnect()
bid <- bar(conn, instrument, "BEST_BID", start, end, "1")
ask <- bar(conn, instrument, "BEST_ASK", start, end, "1")

Which I then merge. I have tried merging the data set so that I do not lose
periods where I only have data for bid or ask, but that has not been
successful.

bidask <- merge(bid, ask, by="time")

In order to remove non-trading hours and weekends I create time as a
numerical variable and remove unsuitable weekdays.

Date2 <- substr(bidask$time, 1,10)
Date3 <- substr(bidask$time, 12,16)
Date3b <- paste(Date2, Date3)
Date4 <- as.POSIXlt(Date3b)

Time = Date4 - trunc(Date4, "days")

dataset2 <- cbind(bidask, Time)
dataset3 <- cbind(dataset2, Date2)

Weekday <- as.POSIXlt(Date2)$wday
dataset4 <- cbind(dataset3, Weekday)
dataset5 <- subset(dataset4, Weekday > 0)
dataset6 <- subset(dataset5, Weekday < 6)
dataset7 <- subset(dataset6, Time > 8.00)
dataset8 <- subset(dataset7, Time < 16.41)

Finally, I calculate the average daily spread and aggregate it on a daily
basis.

spread <- (dataset8$close.y-dataset8$close.x) /
((dataset8$close.y+dataset8$close.x)/2)*10000
dataset9 <- cbind(dataset8,spread)

final <- aggregate(x=dataset9$spread, by=list(dataset9$Date2), FUN=mean)


I was wondering if it is possible to create a sequence of time so that I can
get observations for every minute during trading hours. I was thinking about
the possibility to get a NA which I can replace using zoo's na.locf to carry
over last value. But it would be even better if I could get it down to
per-second.

Secondly, I am having trouble with the hours, definitely due to timezones.
And thirdly, I have not yet looked at trading calendars.

I would be very grateful if all my troubles were solved, or some of them. 

- Patrik

--
View this message in context: http://r.789695.n4.nabble.com/Interpolation-of-RBloomberg-historical-intraday-bar-data-to-force-time-periodicity-tp4228004p4228004.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list