xts

R obtaining rownames date using quantmod

对着背影说爱祢 提交于 2019-11-28 12:07:21
Using quantmod and collecting data from Yahoo. I am trying to get the dates that are in rownames. However I am just getting NULL. library("quantmod") sp500 <- new.env() getSymbols("^GSPC", env = sp500, src = "yahoo", from = as.Date("2008-01-04"), to = Sys.Date()) GSPC <- get("GSPC", envir = sp500) date1 <- rownames(GSPC) date1 > NULL I would be grateful for your help into getting the rowname dates into a vector. You need to use the index function. The xts object isn't the same as a normal data.frame , and has its own way of handling dimension names. # Return all dates index(GSPC) Your code is

xts tick data rolling subset

南楼画角 提交于 2019-11-28 11:44:13
I have created an xts object from historical tick data, sourced from a SQL database. I would like to create subsets of the tick data, for example: Show daily ticks between 10am and 2:30pm. This would allow me to create specific data sets for specific trade ideas, based on the time of day. The format of my index is as follows: > index(merged[3567,]) [1] "2011-08-01 13:17:59 SAST" Could an expert in xts please advise me how I would go about creating these subsets? Any advice would be greatly appreciated. You can do it with time-of-day subsetting: merged["T10:00/T14:30"] You can use difftime to

Using rollmean when there are missing values (NA)

安稳与你 提交于 2019-11-28 11:18:36
I have a data set which has a couple of NA in it. I take a rolling mean and expect that when there is no NA in the window, the rolling mean should produce a number as opposed to NA , however, rollmeanr in zoo does not seem to do this. Example: require(zoo) z = zoo(cbind(a=0:10, b=c(NA,10:1), c=sample(1:11,11)), 1:11) rollmeanr(z, k=3, fill=NA) a b c 1 NA NA NA 2 NA NA NA 3 1 NA 3.333333 4 2 NA 4.666667 5 3 NA 4.000000 6 4 NA 6.333333 7 5 NA 7.000000 8 6 NA 9.333333 9 7 NA 8.333333 10 8 NA 8.666667 11 9 NA 5.666667 rollapply(z, width=3, FUN=mean, by=1, by.column=TRUE, fill=NA, align="right") a

Pull nth Day of Month in XTS in R

走远了吗. 提交于 2019-11-28 10:25:11
My questions is closely related to the one asked here: Pull Return from first business day of the month from XTS object using R . Instead of extracting the first day of each month, I want to extract, say the 10th data point of each month. How can I do this? Using the same example data from the question you've linked to, you can do some basic subsetting. Here's the sample data: library(xts) data(sample_matrix) x <- as.xts(sample_matrix) Here's the subsetting: x[format(index(x), "%d") == "10"] # Open High Low Close # 2007-01-10 49.91228 50.13053 49.91228 49.97246 # 2007-02-10 50.68923 50.72696

Get the month from the week of the year

大城市里の小女人 提交于 2019-11-28 09:36:12
问题 Let's say we have this: ex <- c('2012-41') This represent the week 41 from the year 2012. How would I get the month from this? Since a week can be between two months, I will be interested to get the month when that week started (here October). Not duplicate to How to extract Month from date in R (do not have a standard date format like %Y-%m-%d). 回答1: The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate

Remove duplicate rows from xts object

泪湿孤枕 提交于 2019-11-28 09:30:52
I am having trouble deleting duplicated rows in an xts object. I have a R script that will download tick financial data of a currency and convert it to an xts object of OHLC format. The script also pulls new data every 15 minutes. The new data is downloaded from the first trade of today to the last recorded trade of today. The old previous data downloaded was stored in .Rdata format and called. Then the new data is added to the old data and it overwrites the old data in .Rdata format. Here is an example of what my data looks like: .Open .High .Low .Close .Volume .Adjusted 2012-01-07 00:00:11 6

Speedup conversion of 2 million rows of date strings to POSIX.ct

梦想的初衷 提交于 2019-11-28 09:13:32
I have a csv which includes about 2 million rows of date strings in the format: 2012/11/13 21:10:00 Lets call that csv$Date.and.Time I want to convert these dates (and their accompanying data) to xts as fast as possible I have written a script which performs the conversion just fine (see below), but it's terribly slow and I'd like to speed this up as much as possible. Here is my current methodology. Does anyone have any suggestions on how to make this faster? dt <- as.POSIXct(csv$Date.and.Time,tz="UTC") idx <- format(dt,tz=z,usetz=TRUE) So the script converts these date strings to POSIX.ct .

Access zoo or xts index

匆匆过客 提交于 2019-11-28 08:59:41
I am using zoo objects, buy my question also applies to xts objects. It looks to me like it is a one column vector with an index. In my case the index is the vector of dates and the one column vector my data. All is good except that I would like to access the dates (from the index). For example I have the following result: ObjZoo <- structure(c(10, 20), .Dim = c(2L, 1L), index = c(14788, 14789), class = "zoo", .Dimnames = list(NULL, "Data")) unclass(ObjZoo) # Data # [1,] 10 # [2,] 20 # attr(,"index") # [1] 14788 14789 I want to get 14789 in a variable or a vector, but I'm not sure how to

Rolling Sum by Another Variable in R

╄→гoц情女王★ 提交于 2019-11-28 08:40:43
I want to get the rolling 7-day sum by ID. Suppose my data looks like this: data<-as.data.frame(matrix(NA,42,3)) data$V1<-seq(as.Date("2014-05-01"),as.Date("2014-09-01"),by=3) data$V2<-rep(1:6,7) data$V3<-rep(c(1,2),21) colnames(data)<-c("Date","USD","ID") Date USD ID 1 2014-05-01 1 1 2 2014-05-04 2 2 3 2014-05-07 3 1 4 2014-05-10 4 2 5 2014-05-13 5 1 6 2014-05-16 6 2 7 2014-05-19 1 1 8 2014-05-22 2 2 9 2014-05-25 3 1 10 2014-05-28 4 2 How can I add a new column that will contain the rolling 7-day sum by ID? If your data is big, you might want to check out this solution which uses data.table .

Rolling regression xts object in R

微笑、不失礼 提交于 2019-11-28 06:21:18
问题 I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. I have an xts object, prices: > tail(prices) DBC EEM EFA GLD HYG IEF IWM IYR MDY TLT 2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26 2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95 2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79 2012-11-07 27.34 41.44 53.26 166.49 91.85 108