xts

timeBasedSeq function repeats some of the days it creates!

狂风中的少年 提交于 2019-12-01 19:21:20
I am using The timeBasedSeq function from xts package to use as index in a time series/zoo object but it repeats some of the days it creates! Which than causes problems with zoo because the "index entries in ‘order.by’ are not unique". For example: timeBasedSeq("19860601/19861231") will create ..."1986-10-23" "1986-10-24" "1986-10-25" "1986-10-26" "1986-10-26" "1986-10-27" "1986-10-28" "1986-10-29""... So for some reason it repeats the 26th, how can I avoid that? 42- The bug is in xts. That function uses seq.POSIXct , and the same behavior can be produced by: seq(as.POSIXct("1986-10-01"), as

Change NAs to interpolated flat bars

此生再无相见时 提交于 2019-12-01 11:46:24
If I have some OHLC data, with some NA rows, is there already a function in one of the R packages that will interpolate data? na.locf has two cons: It is flat, then a sudden jump Used naively, it will repeat the whole of the last bar, with its high/lows UPDATE: na.approx is superior to na.locf in the first respect, thanks Dirk, but I'm still searching for an existing function that is bar-aware . I.e. that will interpolate from the close of previous bar to the open of the next non-NA bar, and will create flat bars. Bonus points if it sets volume to zero! (So, I suspect the answer to my question

R merge daily data with tick data

别说谁变了你拦得住时间么 提交于 2019-12-01 11:22:24
Thanks for the pointer to na.locf (Darren), updated example and results below: I have tick data, which I have rolled into daily data, in order to calc daily volatility. Now that I have created the daily volatility, I would like to merge the daily data with the tick data again. However, I suspect the merge remains "empty" due to the index differences of the daily and tick data. How would one merge the daily data with tick data? Example: AGL.xts <- xts(AGL_Frame[,-1], order.by=AGL_Frame[,1]) AGL.xts Close 2012-01-19 16:46:11 32376 2012-01-19 16:46:32 32377 2012-01-19 16:46:32 32376 2012-01-19 16

Nth day of month in a xts object

别说谁变了你拦得住时间么 提交于 2019-12-01 10:40:05
prices is an xts object period.ends = endpoints(prices, 'months',k=1) with this line i find position of the last day of the month in a time series. How to find position of the first day of month (not always 1 of the month)? And the second day? Thank you startpoints <- function (x, on = "months", k = 1) { head(endpoints(x, on, k) + 1, -1) } period.starts = startpoints(prices, 'months', k=1) See also: xts:::startof and xts::firstof 来源: https://stackoverflow.com/questions/21858552/nth-day-of-month-in-a-xts-object

xts comparison to another xts object does not work

試著忘記壹切 提交于 2019-12-01 10:38:27
this = structure(c(-0.012, -0.028, -0.044, -0.033, -0.039, -0.042), .Dim = c(3L, 2L), .Dimnames = list(NULL, c("one", "two")), index = structure( c(1313643600, 1313730000, 1313816400), tzone = "", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "", class = c("xts", "zoo")) m1=last(this$one) - last(this$two) m2=first(last(this$one,n=2)) - first(last(this$two,n=2)) m1 > 0 #returns a TRUE OR FALSE m1 > m2 #breaks I know I can use coredata to extract and then compare. I wasn't sure if this is bug. It didn't seem consistent that comparisons work and even math operators work just fine on xts

rolling computations in xts by month

时间秒杀一切 提交于 2019-12-01 08:52:32
I am familiar with the zoo function rollapply which allows you to do rolling computations on zoo or xts objects and you can specify the rolling increment via the by parameter. I am specifically interested in applying a function every month but using all of the past daily data in the computation. For example say my data set looks like this: dte, val 1/01/2001, 10 1/02/2001, 11 ... 1/31/2001, 2 2/01/2001, 54 2/02/2001, 34 ... 2/30/2001, 29 I would like to select the end of each month and apply a function that uses all the daily data. This doesn't seem like it would work with rollapply since the

Why is this xts frequency always 1?

旧时模样 提交于 2019-12-01 08:27:20
I'm creating an xts object with a weekly (7 day) frequency to use in forecasting. However, even when using the frequency=7 argument in the xts call, the resulting xts object has a frequency of 1. Here's an example with random data: > values <- rnorm(364, 10) > days <- seq.Date(from=as.Date("2014-01-01"), to=as.Date("2014-12-30"), by='days') > x <- xts(values, order.by=days, frequency=7) > frequency(x) [1] 1 I have also tried, after using the above code, frequency(x) <- 7 . However, this changes the class of x to only zooreg and zoo , losing the xts class and messing with the time stamp formats

R merge daily data with tick data

雨燕双飞 提交于 2019-12-01 07:42:12
问题 Thanks for the pointer to na.locf (Darren), updated example and results below: I have tick data, which I have rolled into daily data, in order to calc daily volatility. Now that I have created the daily volatility, I would like to merge the daily data with the tick data again. However, I suspect the merge remains "empty" due to the index differences of the daily and tick data. How would one merge the daily data with tick data? Example: AGL.xts <- xts(AGL_Frame[,-1], order.by=AGL_Frame[,1])

Decompose xts hourly time series

。_饼干妹妹 提交于 2019-12-01 07:32:40
I want to decompose hourly time series with decompose , ets , or stl or whatever function. Here is an example code and its output: require(xts) require(forecast) time_index1 <- seq(from = as.POSIXct("2012-05-15 07:00"), to = as.POSIXct("2012-05-17 18:00"), by="hour") head(time_index1 <- format(time_index1, format="%Y-%m-%d %H:%M:%S", tz="UTC", usetz=TRUE) # [1] "2012-05-15 05:00:00 UTC" "2012-05-15 06:00:00 UTC" # [3] "2012-05-15 07:00:00 UTC" "2012-05-15 08:00:00 UTC" # [5] "2012-05-15 09:00:00 UTC" "2012-05-15 10:00:00 UTC" head(time_index <- as.POSIXct(time_index1)) # [1] "2012-05-15 05:00

XTS apply function to time of day subset?

感情迁移 提交于 2019-12-01 07:24:52
问题 How can I apply a summary function to a time of day subset? For example with: r['T16:00/T17:00']$Value How can I apply something like function (x) quantile(x, c(.90)) for Value over each day's sample hour? 回答1: You can use apply.daily to apply a function to each day's data after you've done the time-of-day subset. rt <- r['T16:00/T17:00','Value'] rd <- apply.daily(rt, function(x) xts(t(quantile(x,0.9)), end(x))) You can see I needed to do a few backflips to ensure the object returned from