xts tick data rolling subset
You can do it with time-of-day subsetting:
merged["T10:00/T14:30"]
R merge daily data with tick data
Items 14.5 and 14.6 in R Cookbook demonstrate merging monthly inflation data with daily IBM data, using merge
(with all=T
or all=F
depending on purpose), na.locf
and zoo
with seq
to generate a full set of dates (to cover dates when one or the other symbol has no data).
I've used the same approach to create blank 1m bars for minutes where there were no ticks, so I think it will work for merging daily and tick data too.
Rolling frequency of irreqular tick-data xts timeseries
Looks like I found a way to accomplish it. Not the most beatiful but it seems to work:
data <- EURUSD
#using the cut method to get the frequency
freqs <- data.frame(table(cut(index(data), breaks="min")))
#getting it back into an xts and merging with the original
freqs[,1] <- as.POSIXct(as.character(freqs[,1]), format = "%Y-%m-%d %H:%M:%s")
freqxts <- xts(freqs[,-1], order.by=freqs[,1])
datawithtickspeed <- merge(data, freqxts)
Struggling to convert tick data with price and volumes to 5min in R
Borrowing the data from agstudy's answer (and therefore making 1 min bars, not 5-mins), I would use this:
dat.xts <- as.xts(dat.xts) ## otherwise you get an error align.time
## since (dat.xts is a zoo object)
bars <- period.apply(dat.xts,
endpoints(dat.xts,"secs",60),
function(xx){
ticks=coredata(xx$price)
c( first(ticks),max(ticks), min(ticks),
last(ticks), sum(xx$volume), sum(xx$price_volume) )
})
colnames(bars) <- c("Open","High","Low","Close","Volume","Price*Volume")
align.time(bars,60)
I.e. period.apply()
is given an xts object holding the ticks for a given 1 minute period. I use first
, max
, min
and last
to make the OHLC data. (I have to use coredata
otherwise those functions complain.)
Then volume and price_volume are summed for each period.
After the main loop I assign column headings, and round up the timestamps. The output is:
Open High Low Close Volume Price*Volume
2013-11-11 14:56:00 31.19 31.19 31.19 31.19 11 34309
2013-11-11 14:57:00 31.16 31.16 31.15 31.15 443 1381515
Subtracting a xts object from a subset of xts object
Next time, please provide a minimal reproducible example. For example:
> library(xts)
> data(sample_matrix)
> x <- as.xts(sample_matrix)
> x-x[,1]
Error in `-.default`(x, x[, 1]) : non-conformable arrays
> apply(x, 2, function(y) y-x[,1])
Error in array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null, :
length of 'dimnames' [1] not equal to array extent
The problem is that xts objects have a dim
attribute by default and it isn't dropped when subsetting like it is with matrix and zoo class objects. You can force dropping it by setting drop=TRUE
in your subsetting call.
> head(x-x[,1,drop=TRUE])
Open High Low Close
2007-01-02 0 0.07799532 -0.08936727 0.07799532
2007-01-03 0 0.19137980 0.00000000 0.16717014
2007-01-04 0 0.00000000 -0.15681864 -0.08859811
2007-01-05 0 0.00000000 -0.15243423 -0.03887316
2007-01-06 0 0.00000000 -0.13311797 -0.06320448
2007-01-07 0 0.08349916 -0.14025780 -0.14025780
This works because x[,1,drop=TRUE]
returns a "vector xts" (i.e. a dimensionless xts object) and the vector is recycled along x
during the -
call.
Prepend xts rows to a subset
Here's one way to do this using endpoints
and a for loop. You could still use the which.i=TRUE
suggestion in my comment, but integer subsetting is faster.
y <- x*NA # pre-allocate result
ep <- endpoints(x,"weeks") # time points where parameters change
set.seed(1.23456789)
for(i in seq_along(ep)[-(1:2)]) {
rng1 <- ep[i-1]:ep[i] # obs to calc weights
rng2 <- ep[i-2]:ep[i] # "prime" obs
wgts <- calc_weights(x[rng1])
# calc smooth_days on rng2, but only keep rng1 results
y[rng1] <- smooth_days(x[rng2], wgts)[index(x[rng1])]
}
xts subset quarterly data
Looks like a timezone issue. The xts index is always a POSIXct object, even if the index class is something else. Like a Date
classed index, the yearqtr
(and yearmon
) classed index should have the timezone set to "UTC"
.
> a <- as.xts(ts(rnorm(20), start=c(1980,1), freq=4), tzone="UTC")
> a["1983"]
[,1]
1983 Q1 1.4877302
1983 Q2 -0.4594768
1983 Q3 -0.1906189
1983 Q4 -1.1518943
Warning message:
timezone of object (UTC) is different than current timezone ().
You can safely ignore the warning. If it really bothers you, you can set your R session's timezone to "UTC"
via:
> Sys.setenv(TZ="UTC")
> a <- as.xts(ts(rnorm(20), start=c(1980,1), freq=4))
> a["1983"]
[,1]
1983 Q2 1.84636890
1983 Q3 -0.06872544
1983 Q4 -2.29822631
1984 Q1 -1.46025131
This will never work:
a[as.yearqtr("1981 Q1")/as.yearqtr("1983 Q3")] # Does not work
It looks like you're trying to do something like: a["1981 Q1/1983 Q3"]
, which isn't supported because "YYYY Qq" is not an ISO8601 format.
Faster way to subset xts
An easy solution is to split
your xts object into chunks by month and day.
# if using xts < 0.10-0 (bug work-around)
month_day <- split(junk_sma10, as.numeric(format(index(junk_sma10), "%m%d")))
# requires xts >= 0.10-0
month_day <- split(junk_sma10, format(index(junk_sma10), "%m%d"))
Now month_day
is a list, where every element is the same month and day of the month for each year. Then you can continue your analysis by using lapply
to call a function on each list element.
Related Topics
Substitute a for B and B for a in a String
R Obtaining Rownames Date Using Quantmod
Assign Colors to a Range of Values
How to Rename All Columns of a Data Frame Based on Another Data Frame in R
Shutdown Windows After Simulation
How to Convert a Character String Date to Date Class If Day Value Is Missing
Avoid Ggplot2 to Partially Cut Axis Text
Ggplot2: More Complex Faceting
Different Colors with Gradient for Subgroups on a Treemap Ggplot2 R
Remove Numbers at the Beginning and End of a String
Filled.Contour in R 3.0.X Throws Error
R: How to Find What S3 Method Will Be Called on an Object
Global Variable in a Package - Which Approach Is More Recommended
How to Load a Matlab Struct into a R Data Frame
Running Out of Heap Space in Sparklyr, But Have Plenty of Memory