xts

time zones in POSIXct and xts, converting from GMT in R

心已入冬 提交于 2020-01-12 21:25:10
问题 I have a bunch of 1 minute returns in an xts object with the index being POSIXct and time zone being GMT. The returns are on NYSE so I would like to convert to the eastern time zone but I would like to take care of the daylight savings time properly. What is the best way of doing this? I am a bit confused between the EST timezone and the EDT timezone. I would like my times to convert properly to the NY time in winter and summer. 回答1: Use indexTZ<- and the America/New_York timezone > tail(SPY)

Adding Points, Legends and Text to plots using xts objects

岁酱吖の 提交于 2020-01-12 07:49:04
问题 I am starting on a bit of analysis on pairs of stocks (pairs trading) and here is the function I wrote for producing a graph (pairs.report - listed below). I need to plot three different lines in a single plot. The function I have listed does what I want it to do, but it will take a bit of work if I want a fine customisation in the x-axis (the time line). As it is, it prints just the years (for 10 years of data) or the months (for 6 months of data) in the x-axis, with no formatting for ticks.

R use apply function on xts zoo class

狂风中的少年 提交于 2020-01-11 13:37:09
问题 I am new in R and I try to use apply function on the xts zoo class, however it shows error. I have a formula: ((2*Close-High-Low)/(High-Low)) * Volume Input: y <- getSymbols("0005.HK", auto.assign = FALSE, src = "yahoo") Error: y$II <- apply(y,2,function(x) (2Cl(x) - Hi(x) - Lo(x)) / ((Hi(x) - Lo(x)) * Vo(stk))) Error: unexpected symbol in "apply(y,2,function(x) (2Cl" and then I tried another one: Error: y$II <- apply(y,2,function(x) (2(x[,4]) - x[,2] - x[,3]) / (x[,2] - x[,3]) * x[,5]) Error

How to extract dates from apply.monthly function

倖福魔咒の 提交于 2020-01-11 11:57:27
问题 If I have a set of daily data, I want to get the minimum value for each month, and the date on which that value occurred. If I use the apply.monthly function, it gives me the minimum value, but the corresponding date is the end of every month, not the date when it actual occurred. How can I get the correct date? library(xts) #create sample data dates<-seq(from=as.Date("1970-01-01"),to=as.Date("1970-12-31"),by=1) x<-sample(1:365,365) ts<-xts(x,dates) #apply function monthly.mins<-apply.monthly

ggplot2: highlight chart area

时光毁灭记忆、已成空白 提交于 2020-01-10 03:58:30
问题 I am working with some time series data and would like to highlight chart area whenever certain conditions become true. For example: require(ggplot2) require(quantmod) initDate <- "1993-01-31" endDate <- "2012-08-10" symbols <- c("SPY") getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct")) spy<-SPY$SPY.Adjusted spy$sma<-SMA(spy$SPY.Adjusted,200) spy<-spy[-(1:199),] spy<-as.data.frame(spy) ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_line()+geom_line(aes(x

sum only the negative values in a row from an xts-object

风流意气都作罢 提交于 2020-01-06 07:29:11
问题 I have an xts-object and I want to sum only the negative values in the first row. The code sum(test[test<0]) gives me the error Error in `[.xts`(test, test < 0) : 'i' or 'j' out of range but sum(test[1],na.rm=TRUE) works, but then I have the sum of all the values, not just the negative ones: [1] -0.9786889 Without giving an example of data yet, does anybody know why this simple code doesnt work? The dimension of the xts-object is > dim(test) is 216 39 . 回答1: There is a chance that this is

Put returns into quantiles for many time series

点点圈 提交于 2020-01-05 21:11:12
问题 I have a xts object of monthly returns (one column is a time series for one instrument). I want to know the quantile for each return, each month. I have my own set of instruments prices from a local database but I can reproduce with getSymbols . I used quantile on stock returns to get the boundaries of my quantile. Then I tried to use cut to divide my returns into quantile but I am stuck there. Ideally I should have a time series of monthly quantile for each instrument. require(quantmod)

How to preserve dates from xts time series data after forecasting

╄→尐↘猪︶ㄣ 提交于 2020-01-04 05:40:15
问题 Please consider this small dataset: library(xts) library(ggplot2) library(forecast) data <- data.frame(idDate = c("12-12-2012", "13-12-2012", "14-12-2012", "16-12-2012", "19-12-2012"), score= c(110, 120, 130, 200, 180)) date <- as.Date(as.character(data$idDate), "%d-%m-%Y") score <- as.numeric(data$score) myxts <- xts(score, date) autoplot(myxts) So far the date (Index) along the x axis is preserved but as soon as I call forecast, the date along my x axis gets converted to integer. see below:

plot xts Error in if (on == “years”) { : missing value where TRUE/FALSE needed

纵饮孤独 提交于 2020-01-03 16:45:23
问题 I am trying to plot an xts object but I get an error about years.. The xts object just has a numerical value and a POSIXct index. Below is the code that shows the xts and the error when trying plot. Any ideas on what needs to be done to a xts object to properly plot? xTest<-as.xts(35, Sys.time()) xTest ## [,1] ## 2013-04-07 18:19:19.37238 35 class(xTest) ## [1] "xts" "zoo" class(index(xTest)) ## [1] "POSIXct" "POSIXt" plot(xTest) ## Error in if (on == "years") { : missing value where TRUE

Aggregate (count) occurences of values over arbitrary timeframe

点点圈 提交于 2020-01-02 06:55:10
问题 I have a CSV file with timestamps and certain event-types which happened at this time. What I want is count the number of occurences of certain event-types in 6-minutes intervals. The input-data looks like: date,type "Sep 22, 2011 12:54:53.081240000","2" "Sep 22, 2011 12:54:53.083493000","2" "Sep 22, 2011 12:54:53.084025000","2" "Sep 22, 2011 12:54:53.086493000","2" I load and cure the data with this piece of code: > raw_data <- read.csv('input.csv') > cured_dates <- c(strptime(raw_data$date,