[R-SIG-Finance] R Bloomberg for intraday prices
algotr8der
algotr8der at gmail.com
Thu Apr 5 21:29:15 CEST 2012
Thanks Jeff and Brain!
I'm still having issues... I did the following:
My starting object is SMH, which was created by result of a query of the bar
function in the RBloomberg package. My machine's timezone is EDT.
> Sys.timezone()
[1] "EDT"
> head(SMH)
time open high low close
2011-09-19T13:30:00.000 2011-09-19T13:30:00.000 30.52 30.550 30.460 30.49
2011-09-19T13:31:00.000 2011-09-19T13:31:00.000 30.48 30.520 30.470 30.50
2011-09-19T13:32:00.000 2011-09-19T13:32:00.000 30.49 30.590 30.480 30.56
2011-09-19T13:33:00.000 2011-09-19T13:33:00.000 30.56 30.610 30.530 30.53
2011-09-19T13:34:00.000 2011-09-19T13:34:00.000 30.53 30.545 30.525 30.53
2011-09-19T13:35:00.000 2011-09-19T13:35:00.000 30.53 30.540 30.495 30.53
numEvents volume
2011-09-19T13:30:00.000 319 105000
2011-09-19T13:31:00.000 198 63700
2011-09-19T13:32:00.000 260 98800
2011-09-19T13:33:00.000 106 20900
2011-09-19T13:34:00.000 89 24900
2011-09-19T13:35:00.000 648 159000
1) strptime(SMH[,1], format="%Y-%m-%dT%H:%M:%S") -> date_time
2) rownames(SMH) <- date_time
3) now the row label is in the format YYYY-mm-dd H:M:S
> head(SMH)
open high low close numEvents volume
2011-09-19 13:30:00 30.52 30.550 30.460 30.49 319 105000
2011-09-19 13:31:00 30.48 30.520 30.470 30.50 198 63700
2011-09-19 13:32:00 30.49 30.590 30.480 30.56 260 98800
2011-09-19 13:33:00 30.56 30.610 30.530 30.53 106 20900
2011-09-19 13:34:00 30.53 30.545 30.525 30.53 89 24900
2011-09-19 13:35:00 30.53 30.540 30.495 30.53 648 159000
> str(SMH)
'data.frame': 48694 obs. of 6 variables:
$ open : num 30.5 30.5 30.5 30.6 30.5 ...
$ high : num 30.6 30.5 30.6 30.6 30.5 ...
$ low : num 30.5 30.5 30.5 30.5 30.5 ...
$ close : num 30.5 30.5 30.6 30.5 30.5 ...
$ numEvents: num 319 198 260 106 89 648 190 171 221 255 ...
$ volume : num 105000 63700 98800 20900 24900 ...
4) Create an XTS object:
as.xts(as.data.frame(SMH)) -> SMH.xts.1
Notice that the tzone = null. Also notice that the time has NOT changed
> str(SMH.xts.1)
An ‘xts’ object from 2011-09-19 13:30:00 to 2012-03-30 20:15:00 containing:
Data: num [1:48694, 1:6] 30.5 30.5 30.5 30.6 30.5 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "open" "high" "low" "close" ...
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
List of 2
$ tclass: chr [1:2] "POSIXct" "POSIXt"
$ tzone : chr ""
> head(SMH.xts.1)
open high low close numEvents volume
2011-09-19 13:30:00 30.52 30.550 30.460 30.49 319 105000
2011-09-19 13:31:00 30.48 30.520 30.470 30.50 198 63700
2011-09-19 13:32:00 30.49 30.590 30.480 30.56 260 98800
2011-09-19 13:33:00 30.56 30.610 30.530 30.53 106 20900
2011-09-19 13:34:00 30.53 30.545 30.525 30.53 89 24900
2011-09-19 13:35:00 30.53 30.540 30.495 30.53 648 159000
5) create another XTS but specify the tzone in the creation of the xts
object since as per RBloomberg documentation the time specified in the bar
function (used to extract the data in the first place) is in UTC:
--snippet from RBloomberg documentation ---
Period start and end times must be expressed as strings in UTC time.
bar <- function(conn, security, field, start_date_time, end_date_time,
interval)
--end snippet-------
as.xts(as.data.frame(SMH), tzone="UTC") -> SMH.xts.2
> head(SMH.xts.2)
open high low close numEvents volume
2011-09-19 17:30:00 30.52 30.550 30.460 30.49 319 105000
2011-09-19 17:31:00 30.48 30.520 30.470 30.50 198 63700
2011-09-19 17:32:00 30.49 30.590 30.480 30.56 260 98800
2011-09-19 17:33:00 30.56 30.610 30.530 30.53 106 20900
2011-09-19 17:34:00 30.53 30.545 30.525 30.53 89 24900
2011-09-19 17:35:00 30.53 30.540 30.495 30.53 648 159000
Warning message:
timezone of object (UTC) is different than current timezone ().
Notice that the time changes..... the PROBLEM I have - at least this is
what I believe the problem to be - is that the time specified in the
original object is already UTC. This is why I want to force the tzone field
to UTC in my call to as.xts. But by doing so it actually performs a
conversion, which I believe is from EDT to UTC since my machine's timezone
is EDT.
The first data point in the original object occurred at 13:30. By forcing
UTC, the new time changes to 17:30 because it thinks I want to convert from
EDT to UTC when in fact I just want to tell R that the object is UTC time so
I can convert it to EDT regular trading hours.
Any thoughts on how I can achieve this without changing the timezone on my
machine? Greatly appreciate your help.
--
View this message in context: http://r.789695.n4.nabble.com/R-Bloomberg-for-intraday-prices-tp4261687p4535793.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list