xts

XTS: split FX intraday bar data by trading days

吃可爱长大的小学妹 提交于 2019-12-04 12:48:23
I want to apply a function to 20 trading days worth of hourly FX data (as one example amongst many). I started off with rollapply(data,width=20*24,FUN=FUN,by=24) . That seemed to be working well, I could even assert I always got 480 bars passed in... until I realized that wasn't what I wanted. The start and end time of those 480 bars was drifting over the years, due to changes in daylight savings, and market holidays. So, what I want is a function that treats a day as from 22:00 to 22:00 of each day we have data for. (21:00 to 21:00 in N.Y. summertime - my data timezone is UTC, and daystart is

R quantmod::getFinancials

╄→гoц情女王★ 提交于 2019-12-04 12:37:18
I'm using the quantmod package. I've got a vector of tickers like this : c("AAPL","GOOG","IBM","GS","AMZN","GE") and I want to create a function to calculate the EBIT margin of a stock (= operating income / total revenue). So for a given stock, I use the following piece of code which only works for GE (provided a ".f" is added a the end of the ticker) : require(quantmod) getFinancials("GE",period="A") ebit.margin <- function(stock.ticker.f){ return(stock.ticker$IS$A["Operating Income",]/stock.ticker$IS$A["Total Revenue",]) } ebit.margin("GE") I would like to generalize this function in order

Splitting irregular time series into regular monthly averages - R

萝らか妹 提交于 2019-12-04 12:04:00
问题 In order to establish seasonal effects on energy use, I need to align the energy use information that I have from a billing database with monthly temperatures. I'm working with a billing dataset that has bills of varying lengths and start and end dates, and I'd like to obtain the monthly average for each account within each month. For example, I have a billing database that has the following characteristics: acct amount begin end days 1 2242 11349 2009-10-06 2009-11-04 29 2 2242 12252 2009-11

R Month by Month Percent Growth on an XTS objects

耗尽温柔 提交于 2019-12-04 10:59:29
How might I plot month to month growth for the following data: A 2008-07-01 0 2008-08-01 87 2008-09-01 257 2008-10-01 294 2008-11-01 325 2008-12-01 299 (In dput format, before Joshua hunts me down and murders me in my sleep): structure(c(0L, 87L, 257L, 294L, 325L, 299L), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "", index = structure(c(1214884800, 1217563200, 1220241600, 1222833600, 1225512000, 1228107600), tzone = "", tclass = c("POSIXt", "POSIXct")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "A"), class = c("xts", "zoo")) Define growth: first difference? Percentages? In either case

XTS Apply Family and a Multi Column XTS?

醉酒当歌 提交于 2019-12-04 09:36:36
How do I use the apply family of functions, say apply.daily to a multivariate XTS? So for example: Time,a,b ... 2012-02-11 16:21:24 4.7258 7.7258 2012-02-11 16:26:25 4.9096 12.3796 2012-02-11 16:31:25 4.7904 2.2204 ... How would I use apply.daily and mean to the entire matrix by column. So the result would be a single time stamp for the day, the mean of a for the next column, and the mean of b for the column after that. I would like to do this for arbitrary number columns (the amount of columns and names are not known -- all numeric of course). You could simply use colMeans to take the mean of

How do I extract / subset day+0 to day+1 index times from minutely data via xts in R?

折月煮酒 提交于 2019-12-04 09:32:50
I'm trying to generate cross-day, sub-settable timeseries in XTS. For example, let's say I have a minutely timeseries ( mts ) that is generated 24 hours a day over 10 years. I want to extract, say, every (08:30am on t+0 to 13:30 t+1) period for every 'day' in the timeseries. To do this from 08:30 to, say, 16:00 on the same day with xts is trivial and well addressed on StackExchange: ie mts["T08:29:59/T16:01:00"] But how do i write the equivalent where the endpoint of the timeseries to be subset is a time that occurs on the next day? Any thoughts much appreciated. This will make a list where

Faster Way of Calculating Rolling Realized Volatility in R

旧街凉风 提交于 2019-12-04 09:25:20
问题 I want to calculate the rolling 20 day realized volatility for a collection of indices. Here is the code I use to download the index prices, calculate the daily returns and the 20 day realized volatility. library(quantmod) library(PerformanceAnalytics) tickers = c("^RUT","^STOXX50E","^HSI", "^N225", "^KS11") myEnv <- new.env() getSymbols(tickers, src='yahoo', from = "2003-01-01", env = myEnv) index <- do.call(merge, c(eapply(myEnv, Ad), all=FALSE)) #Calculate daily returns for all indices and

plotting multiple xts objects in one window

僤鯓⒐⒋嵵緔 提交于 2019-12-04 08:02:38
I have found some answers to this online but for some reason am interpreting incorrectly because I cannot get it to work. My goal is to simply use the xts plotting feature (with the the way it creates the axis, gridlines,etc.) to plot multiple plots: x <- xts(data.frame(a=1:100, b=100:1),seq(from=as.Date("2010-01-01"), by="days", len=100)) > plot(x, screens=1) Warning messages: 1: In plot.xts(x, screens = 1) : only the univariate series will be plotted 2: In plot.window(...) : "screens" is not a graphical parameter 3: In plot.xy(xy, type, ...) : "screens" is not a graphical parameter 4: In

Plotting two xts objects

时光毁灭记忆、已成空白 提交于 2019-12-04 07:22:09
I'm using xtsExtra to plot two xts objects. Consider the following call to plot.xts: plot.xts(merge(a,b),screens=c(1,2)) which is used to plot the xts objects a and b in two separate panels. How do I control the spacing of the y-axes? Specifically, I'm running into the problem where the y-axis labels come too close or even overlap. Ideally, I would like to specify a minimum padding which is to be maintained between the two y-axis labels. Any help is appreciated! EDIT: A reproducible example: #install if needed #install.packages("xtsExtra", repos="http://R-Forge.R-project.org") library(xtsExtra

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

一笑奈何 提交于 2019-12-04 06:30:57
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. Use indexTZ<- and the America/New_York timezone > tail(SPY) SPY.Bid.Price SPY.Ask.Price SPY.Trade.Price SPY.Mid.Price SPY.Volume 2012-08-09 19:54:00 140.47 140.48 140