[R] Re: [R-sig-finance] dates and times on Windows for fMetrics

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Mon Dec 13 00:21:42 CET 2004


# Here is the solution:


require(fBasics)

# Be sure that R is running in time zone GMT.
# Set your Windows environment variable to "GMT"
# Your PC Windows clock can still run in any other time zone!
# My clock is now running in Zurich in Europe.


Date    = c("2003-10-09", "2003-10-10", "2003-10-13", "2003-10-14")
 Open   = c(1.27, 1.25, 1.27, 1.29)
 High   = c(1.28, 1.28, 1.29, 1.29)
 Low    = c(1.25, 1.25, 1.27, 1.27)
 Close  = c(1.25, 1.27, 1.28, 1.27)
 Volume = c(152810, 111338, 243843, 180211)
Data    = data.frame(Open, High, Low, Close, Volume)


# In which time zone are your data recorded?
zone = "Australia/Sydney"
# At what local time have your data been recorded?
# Say, 16:00:00 local time "Australia/Sydney" when the exchange closes?
Time = "16:00:00"
# At which Financial Center you like to use your Data?
FinCenter = "Australia/Sydney"


# Make a timeSeries Object:
sydney.ts = timeSeries(
   data = Data,
   charvec = paste(Date, Time),
   units = c("Open", "High", "Low", "Close", "Volume"),
   zone = "Australia/Sydney",
   FinCenter = "Australia/Sydney")

sydney.ts
# You should get:
#                     Open High  Low Close Volume
# 2003-10-09 16:00:00 1.27 1.28 1.25  1.25 152810
# 2003-10-10 16:00:00 1.25 1.28 1.25  1.27 111338
# 2003-10-13 16:00:00 1.27 1.29 1.27  1.28 243843
# 2003-10-14 16:00:00 1.29 1.29 1.27  1.27 180211


# Now, you are living at your "FinCenter" in Adelaide,
# but the data were recorded in the time "zone" of Sydney:

adelaide.ts = timeSeries(
   data = Data,
   charvec = paste(Date, Time),
   units = c("Open", "High", "Low", "Close", "Volume"),
   zone = "Australia/Sydney",
   FinCenter = "Australia/Adelaide")
adelaide.ts

# Or, you are living in Melbourne:

melbourne.ts = timeSeries(
   data = Data,
   charvec = paste(Date, Time),
   units = c("Open", "High", "Low", "Close", "Volume"),
   zone = "Australia/Sydney",
   FinCenter = "Australia/Melbourne") 
melbourne.ts


# Why does it fail for Perth?
# Have a look on the tail of the DST rules for Sydney:
tail(Sydney())
# You get:
#                  Sydney offSet
# 123 2028-03-25 16:00:00  36000
# 124 2028-10-28 16:00:00  39600
# 125 2029-03-24 16:00:00  36000
# 126 2029-10-27 16:00:00  39600
# 127 2030-03-30 16:00:00  36000
# 128 2030-10-26 16:00:00  39600

# Now for Perth:
tail(Perth())
# You get:
#                  Perth offSet
# 8  1974-10-26 18:00:00  32400
# 9  1975-03-01 18:00:00  28800
# 10 1983-10-29 18:00:00  32400
# 11 1984-03-03 18:00:00  28800
# 12 1991-11-16 18:00:00  32400
# 13 1992-02-29 18:00:00  28800

# OOPS ...
# The DST rules are missing after 1992.
# A quick repair:
# Let's assume that the DST dates are the same as for Sydney:
# and the offset 2 hours (120 Minutes) earlier:

rm(Perth)
PERTH <<- Perth
Perth = function() {
    Perth = paste(substring(as.character(Sydney()[52:128,1]), 1, 10), 
"18:00:00")
    offSet = Sydney()[52:128,2] - 2*60*60
    rbind(PERTH(), data.frame(Perth, offSet))
}

# Try the complete Perth():
Perth()


perth.ts = timeSeries(
   data = Data,
   charvec = paste(Date, Time),
   units = c("Open", "High", "Low", "Close", "Volume"),
   zone = "Australia/Sydney",
   FinCenter = "Australia/Perth") 
perth.ts

# You get:
#                     Open High  Low Close Volume
# 2003-10-09 14:00:00 1.27 1.28 1.25  1.25 152810
# 2003-10-10 14:00:00 1.25 1.28 1.25  1.27 111338
# 2003-10-13 14:00:00 1.27 1.29 1.27  1.28 243843
# 2003-10-14 14:00:00 1.29 1.29 1.27  1.27 180211

 

# Is that right, there are 2 hours difference from Perth to Sydney?



# Note there are some other FinCenters which are not up to date.
# The list will be checked and updated with the next version of Rmetrics.


# Regards
# Diethelm Wuertz







Tom Mulholland wrote:

> First things first
>
> R       "R version 2.0.1, 2004-11-15"
> OS.type "windows"
> GUI     "Rgui"
>
> I thought that I had the time and date stuff nearly under control. I 
> don't get the ubiquitous "GMT" warning although I'm not sure that the 
> way I have done it is correct. I use a batch file to invoke R
>
> set TZ=GMT
> rgui.exe
>
> I have a dataset that I use called tempdata
>
> > str(tempdata)
> `data.frame':   300 obs. of  7 variables:
>  $ date     : chr  "2003/10/09" "2003/10/10" "2003/10/13" "2003/10/14" 
> ...
>  $ Open     : num  1.27 1.25 1.27 1.29 1.27 1.28 1.32 1.35 1.35 1.34 ...
>  $ High     : num  1.28 1.28 1.29 1.29 1.29 1.31 1.35 1.37 1.37 1.34 ...
>  $ Low      : num  1.25 1.25 1.27 1.27 1.27 1.28 1.31 1.32 1.33 1.32 ...
>  $ Close    : num  1.25 1.27 1.28 1.27 1.28 1.31 1.35 1.35 1.34 1.33 ...
>  $ Volume   : int  152810 111338 243843 180211 159147 386021 270289 
> 690343 574630 314740 ...
>  $ dateposix:`POSIXct', format: chr  "2003-10-09" "2003-10-10" 
> "2003-10-13" "2003-10-14" ...
>
> I use the POSIXct in my own home-made plots. In playing with Fmetrics 
> I naturaly wanted to create a time series
>
> This works
> ts = timeSeries(tempdata[,2:6], charvec = tempdata[,1],format = 
> "%Y/%m/%d",FinCenter = "Australia/Sydney")
>
> Although if I set myFinCenter to "Australia/Perth" it fails. (See 
> below for structure)
>
> while
> ts = timeSeries(tempdata[,2:6], charvec = tempdata[,1],format = 
> "%Y/%m/%d",FinCenter = "Australia/Perth") fails with
>
> Error in if (timeTest == 0) iso.format = "%Y-%m-%d" :
>         missing value where TRUE/FALSE needed
>
> Ive looked at the function but I'm missing something.
>
> Any ideas would be much appreciated
>
>
> > str(ts)
> Formal class 'timeSeries' [package "fBasics"] with 7 slots
>   ..@ Data         : num [1:300, 1:5] 1.27 1.25 1.27 1.29 1.27 1.28 
> 1.32 1.35 1.35 1.34 ...
>   .. ..- attr(*, "dimnames")=List of 2
>   .. .. ..$ : chr [1:300] "2003-10-09 10:00:00" "2003-10-10 10:00:00" 
> "2003-10-13 10:00:00" "2003-10-14 10:00:00" ...
>   .. .. ..$ : chr [1:5] "TS.1" "TS.2" "TS.3" "TS.4" ...
>   ..@ positions    : chr [1:300] "2003-10-09 10:00:00" "2003-10-10 
> 10:00:00" "2003-10-13 10:00:00" "2003-10-14 10:00:00" ...
>   ..@ format       : chr "%Y-%m-%d %H:%M:%S"
>   ..@ FinCenter    : chr "Australia/Sydney"
>   ..@ units        : chr [1:5] "TS.1" "TS.2" "TS.3" "TS.4" ...
>   ..@ title        : chr "Time Series Object"
>   ..@ documentation: chr "Created at Australia/Sydney 2004-12-12 
> 19:52:35"
>
> _______________________________________________
> R-sig-finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>




More information about the R-help mailing list