xts

R: xts timestamp differ from real data timestamp by 1 millisecond

南楼画角 提交于 2019-12-04 05:17:23
问题 So I have the following data. tt <- structure(list(Timestamp = c("2018-03-01 09:51:59.969", "2018-03-01 09:51:59.969", "2018-03-01 09:51:59.970", "2018-03-01 09:51:59.971", "2018-03-01 09:51:59.987", "2018-03-01 09:51:59.988"), Mid_Px = c(30755.5, 30755, 30755.5, 30756, 30756.5, 30756.5)), .Names = c("Timestamp", "Mid_Px"), class = "data.frame", row.names = 85774:85779) which looks like this: Timestamp Mid_Px 85774 2018-03-01 09:51:59.969 30755.5 85775 2018-03-01 09:51:59.969 30755.0 85776

convert yyyymm on factor class to character class to be used with ChartSeries()

北慕城南 提交于 2019-12-04 05:09:32
问题 I read a CSV file using read.csv() command and I want to convert into xts and graph with chartSeries() . I changed into a matrix by doing: MyData <- as.matrix(MyData) When I convert to xts using MyData_xts <- xts(MyData[,-1], order.by=as.POSIXct(MyData[,1])) I get the following error message: Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format The column that has my index is in the yyyymm format. I've read that that may be a problem,

timeBasedSeq function repeats some of the days it creates!

落爺英雄遲暮 提交于 2019-12-04 03:43:52
问题 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? 回答1: The bug is in xts. That

Optimize moving averages calculation - is it possible?

纵饮孤独 提交于 2019-12-03 21:07:33
Is it possible to optimize (make it much faster) this piece of code: out <- do.call(rbind, lapply(split(Cl(cumulativeBars), "days"), function(x) { previousFullBars <- barsEndptCl[as.Date(index(barsEndptCl), tz=indexTZ(barsEndptCl)) < as.Date(last(index(x)), tz=indexTZ(x)), ] if (NROW(previousFullBars) >= 4) { last(SMA(last(rbind(previousFullBars, x), n=6), n=5)) } else { xts(NA, order.by=index(x)) } })) Below you can find my original question with all the code example that runs but a bit to slow for my needs. ORIGINAL QUESTION: After I was able to transform xts to lower frequency in cumulative

Aggregating price data to different time horizon in R data.table

六眼飞鱼酱① 提交于 2019-12-03 17:37:50
Hi I'm looking to roll up minutely data in a data.table to 5 minutely (or 10 minutely) horizon. I know this is easily done via using xts and the to.minutes5 function, but I prefer not to use xts in this instance as the data set is rather large. Is there an easy way to do this in data.table ? Data example : In this example the period between 21.30 to 21.34 (both inclusive) would have just one row with t = 21.30, open = 0.88703 , high = 0.88799 , low = 0.88702 , close = 0.88798, volume = 43 (note the data from 21.35 itself is ignored). t open high low close volume 1: 2010-01-03 21:27:00 0.88685

Reading csv with date and time

☆樱花仙子☆ 提交于 2019-12-03 16:01:03
问题 I am working in R and reading csv which has date and time in its first column. I want to import this csv file in R first and then convert it to zoo obect. I am using the code in R EURUSD <- as.xts(read.zoo("myfile.csv",sep=",",tz="",header=T)) My csv file contain data in the format: Date,Open,Low,High,Close 2006-01-02 10:01:00,2822.9,2825.45,2822.1,2824.9 2006-01-02 10:02:00,2825,2825.9,2824,2824.95 2006-01-02 10:03:00,2824.55,2826.45,2824,2826.45 2006-01-02 10:04:00,2826.45,2826.45,2824.9

regressions with xts in R

爷,独闯天下 提交于 2019-12-03 15:04:33
问题 Is there a utility to run regressions using xts objects of the following type: lm(y ~ lab(x, 1) + lag(x, 2) + lag(x,3), data=as.data.frame(coredata(my_xts))) where my_xts is an xts object that contains an x and a y . The point of the question is is there a way to avoid doing a bunch of lags and merges to have a data.frame with all the lags? I think that the package dyn works for zoo objects so i would expect it to work the same way with xts but though there might be something updated. 回答1:

Adding Points, Legends and Text to plots using xts objects

杀马特。学长 韩版系。学妹 提交于 2019-12-03 14:10:37
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. If I use an xts object, i.e., if I use plot(xts-object-with-date-asset1-asset2, ...) instead of plot

Longer object length is not a multiple of shorter object length? [duplicate]

天大地大妈咪最大 提交于 2019-12-03 11:39:14
This question already has an answer here: Why do I get “warning longer object length is not a multiple of shorter object length”? 4 answers I don't understand why R gives me a warning about "Longer object length is not a multiple of shorter object length" I have this object which is generated by doing an aggregate over an xts series giving the weekday median: u <- aggregate(d, list(Ukedag = format(index(d),"%w")), median) 1 314.0 2 282.5 3 270.0 4 267.0 5 240.5 Then I try to apply this to my original xts series, which looks like this (only a lot longer) head(d) 2009-01-02 116 2009-01-05 256

Convert data.frame to xts object and preserve types

天大地大妈咪最大 提交于 2019-12-03 10:09:23
问题 Is there a way to create an xts object from a data.frame and preserve data type? My numerics are being converted to character. This post from 2009 suggests merging columns into an existing xts: http://r.789695.n4.nabble.com/as-xts-convert-all-my-numeric-data-to-character-td975564.html It wasn't clear whether that is the ONLY way to do this. Seems a bit of a hack and cumbersome for large data frames. I would think out-of-the-box xts would respect the datatypes. 回答1: No, you can't. xts/zoo