4

I'm working with minute data of NASDAQ, it has the index "2015-07-13 12:05:00 EST". I adjusted the system time with Sys.setenv(TZ = 'EST').

I want to program a simple buy/hold/sell strategy, therefore I create a vector of flat positions as a foundation.

pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ))

Then I want to apply a constraint, that in a certain time window, positions are bound to be flat, which in my case means equal to 1.

pos_flat["T13:41/T14:00"] <- 1

And this returns the error:

"Error in as.POSIXlt.POSIXct(.POSIXct(.index(x)), tz = indexTZ(x)) :invalid 'tz' value".

I also get this error doing other calculations, I just used this example because it is easy and shows the problem.

As extra information:

> Sys.timezone 
function (location = TRUE) 
{
    tz <- Sys.getenv("TZ", names = FALSE)
    if (nzchar(tz)) 
        return(tz)
    if (location) 
        return(.Internal(tzone_name()))
    z <- as.POSIXlt(Sys.time())
    zz <- attr(z, "tzone")
    if (length(zz) == 3L) 
        zz[2L + z$isdst]
    else zz[1L]
}
<bytecode: 0x03648ff4>
<environment: namespace:base>

I don't understand the problem with the tz value... Any ideas?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
dracthamac
  • 43
  • 1
  • 4
  • Specifying time zones in the form of three letter like 'EST' is ambiguous. Australia has an EST - Eastern Standard Time as does north America. Better to use country/city. `library(lubridate) ymd_hms('2000-01-01 12:11:10', tz = 'australia/melbourne')` – Tony Ladson Jan 14 '17 at 12:46

3 Answers3

5

The source of your "invalid 'tz' value" error is because, for whatever reason, R doesn't accept tz = df$var. If you set tz = 'America/New_York' or some other character value, then it will work.

Better answer (instead of using force_tz below) for converting UTC times to various timezones based on location. It is also simpler and better than looping through or using a nested ifelse. I subset and change tz based on a timezone column (which my data already has, if not you can create it). Just make sure you account for all timezones in your data

(unique(df$timezone))   
df$datetime2[df$timezone == 'America/New_York'] <- format(df$datetime, tz="America/New_York")[df$timezone == 'America/New_York']
df$datetime2[df$timezone == 'America/Chicago'] <- format(df$datetime, tz="America/Chicago")[df$timezone == 'America/Chicago']
df$datetime2[df$timezone == 'America/Denver'] <- format(df$datetime, tz="America/Denver")[df$timezone == 'America/Denver']
df$datetime2[df$timezone == 'America/Los_Angeles'] <- format(df$datetime, tz="America/Los_Angeles")[df$timezone == 'America/Los_Angeles']

Previous solution: Converting to Local Time in R - Vector of Timezones

require(lubridate)
require(dplyr)

df = data.frame(timestring = c("2015-12-12 13:34:56", "2015-12-14 16:23:32"), localzone = c("America/Los_Angeles", "America/New_York"), stringsAsFactors = F)

df$moment = as.POSIXct(df$timestring, format="%Y-%m-%d %H:%M:%S", tz="UTC")

df = df %>% rowwise() %>% mutate(localtime = force_tz(moment, localzone))

df
Sam
  • 468
  • 8
  • 9
3

You are getting errors because "EST" is not a valid timezone specification. It's an abbreviation that's often used when printing and displaying timezones.

The index is printed as "2015-07-13 12:05:00 EST" because "EST" probably represents Eastern Standard Time in the United States. If you want to set the TZ environment variable to that timezone, you should use Sys.setenv() with Country/City notation:

Sys.setenv(TZ = "America/New_York")

You can also set the timezone in the xts constructor:

pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ), tzone = "America/New_York")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
-1

Your error occurs because of a misinterpretation of the time object. You need to have UNIX timestamps in order to use something like

pos_flat["T13:41/T14:00"] <- 1

Try a conversion of your indices by doing something like this:

index(NASDAQ) <- as.POSIXct(strptime(index(NASDAQ), "%Y-%m-%d %H:%M:%S"))

As you want to use EST, you have to change your environment variables (if you are not living in EST timezone). So all in all, this should work:

Sys.setenv(TZ = 'EST')
#load stuff
#...
index(NASDAQ) <- as.POSIXct(strptime(index(NASDAQ), "%Y-%m-%d %H:%M:%S"))
pos_flat <- xts(rep(0, nrow(NASDAQ)), index(NASDAQ))
pos_flat["T13:41/T14:00"] <- 1

For further information, have a look at the POSIXct and POSIXlt structures in R.

Best regards

mutilis
  • 563
  • 3
  • 18
  • 3
    This answer is simply wrong. The xts index is already POSIXct. There is no need to convert it to character and call `as.POSIXct`. Further, as I and others have said, "EST" is not a valid timezone. – Joshua Ulrich Jan 14 '17 at 16:52
  • It's not a valid way to specify a timezone. Does "EST" mean Eastern Standard Time in the US or Australia, or is it the Canadian timezone? As it says in `?timezone`, "preferred way to refer to a time zone is by a location (typically of a city), e.g., 'Europe/London', 'America/Los_Angeles', 'Pacific/Easter'." – Joshua Ulrich Jan 14 '17 at 22:50
  • So you are telling ne that Pages like http://www.nasdaq.com/quotes/premarket.aspx are using wrong timezones? It is pretty clear which of these EST timezones is used. However, look again at the error of the OP and my solution. You may disregard the timezone. Additionally, seems like it works for OP as well. – mutilis Jan 15 '17 at 08:45
  • 2
    @dracthamac how can you accept this answer when it is incorrect for reasons clearly outlined? – FXQuantTrader Jan 17 '17 at 20:47
  • changing the index value/format worked for me, thanks! – AbeeCrombie Jul 13 '18 at 16:21