Merging multiple XTS objects by index - r

There are multiple XTS objects. Each one contains one column only (of prices).
The objects may be of different lengths. I want to merge them by index.
I can merge two XTS objects with merge.xts. But I want to merge multiple
(with one function, without repeating merging process multiple times).
So I applied the approach recommended here. It works. Just one issue: it merges all data into one column, rather than placing each XTS object to be merged into its own column. e.g. In the reproducible example, the desired end result is for XTSM to have 3 columns.
How can that be achieved? Thank you.
library('xts')
#Data
XTS1 <- structure(c(125.26, 125.14, 125.21, 125.26, 125.21, 125.26, 125.24, 125.22, 125.3, 125.32, 125.38, 125.38, 125.29, 125.26, 125.3, 125.25, 125.29, 125.37, 125.21, 125.3, 125.25, 125.24, 125.21, 125.24, 125.24, 125.26, 125.23, 125.23), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403554500, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(28L, 1L))
XTS2 <- structure(c(1867, 1867, 1868.5, 1868.75, 1868.75, 1868.75, 1868.25, 1867.25, 1867.5, 1867.75, 1868, 1868.25, 1868.25, 1868.75, 1868, 1868.5, 1869, 1869.75, 1868.75, 1868.5, 1868.25, 1867.5, 1867, 1866.75, 1866.75, 1867, 1867.5), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(27L, 1L))
XTS3 <- structure(c(1867, 1867, 1868.5, 1868.75, 1868.75, 1868.75, 1868.25, 1867.25, 1867.5, 1867.75, 1868, 1868.25, 1868.25, 1868.75, 1868, 1868.5, 1869, 1869.75, 1868.75, 1868.5, 1868.25, 1867.5, 1867, 1866.75, 1866.75, 1867, 1867.5), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(27L, 1L))
#Following code merges 2 xts objects into a 2 column xts
XTSM <- merge(XTS1,XTS2, all=TRUE)
#Following code merges multiple xts objects into a 1 column xts
XTSlist <- list(XTS1, XTS2, XTS3)
do.call.rbind <- function(lst) {
while(length(lst) > 1) {
idxlst <- seq(from=1, to=length(lst), by=2)
lst <- lapply(idxlst, function(i) {
if(i==length(lst)) { return(lst[[i]]) }
return(rbind(lst[[i]], lst[[i+1]]))
})
}
lst[[1]]
}
XTSM <- do.call.rbind (XTSlist)

Joshua answer the question in the comments. Posting solution for the sake of having the Q answered. It is as simple as this:
XTSM.T <- merge(XTS1,XTS2,XTS3, all=TRUE)
or
XTSM.F <- merge(XTS1,XTS2,XTS3, all=FALSE)

Related

ggseasonplot in R showing error to use X=object

My 1-year time series dataset is
water_ts<- xts(x = water[,-1], order.by = water$DATE)
head(water_ts)
Total_water.received.for.the.month.M3 Total_MWh
2017-01-01 4539014 38.0
2017-01-02 4616418 32.7
2017-01-03 4752335 58.3
2017-01-04 4979395 96.2
2017-01-05 5054494 32.4
2017-01-06 5238897 75.9
class(water_ts)
[1] "xts" "zoo"
>
Now I want to do seasonal plot. With the help from #Rob J Hyndman robjhyndman,
I have written following code.
>
ggseasonplot(water_ts, x = Total_MWh, year.labels=TRUE, year.labels.left=TRUE) +
+ ylab("kwh") +
+ ggtitle("Seasonal plot: energy consumption kwh")
Error in ggseasonplot(water_ts, x = Total_MWh, year.labels = TRUE, year.labels.left = TRUE) :
object 'Total_MWh' not found.
Why this error is coming? I am not getting any reference.
The reproducible data
dput(head(water_ts))
structure(c(4539014, 4616418, 4752335, 4979395, 5054494, 5238897,
38, 32.7, 58.3, 96.2, 32.4, 75.9), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1483228800,
1483315200, 1483401600, 1483488000, 1483574400, 1483660800), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
2L), .Dimnames = list(NULL, c("Total_water.received.for.the.month.M3",
"Total_MWh")))

Side by side plots for PerformanceAnalytics in R

The following script plots 2 charts side by side:
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
plot(XTS1)
plot(XTS2)
The following script fails to plot 2 charts side by side:
require(PerformanceAnalytics)
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
charts.PerformanceSummary(XTS1)
charts.PerformanceSummary(XTS2)
Would anyone know how to get the latter script to plot 2 charts side by side?
I would like to avoid using another package if possible. Thanks.
chart.PerformanceSummary is really just a wrapper to several charts.
You could do this, and extend it to any number of symbols horizontally if you wish (more than 2 symbols if you wanted):
par(mfrow=c(3,2))
# First row
chart.CumReturns(XTS1, ylab = "Cumulative Return", main = "give me a title")
chart.CumReturns(XTS2, ylab = "Cumulative Return", main = "give me a title2")
# second row
chart.BarVaR(XTS1)
chart.BarVaR(XTS2)
# third row
chart.Drawdown(XTS1, main = "DD title", ylab = "Drawdown",
)
chart.Drawdown(XTS2, main = "", ylab = "Drawdown",
)
You need to add the appropriate parameters to each plot for things like colour and titles (leaving that to you), but you have the flexibility of adding any charts from the wonderful xts, quantmod, performanceAnalytics packages (and others).

Estimate overnight returns for many stocks using a for loop and store it in a dataframe with stock names as column names

I am trying to estimate overnight returns for many stocks using a for loop and store it in a dataframe with stock names as column names. The trade has raw intraday data and trade2 has cleaned intraday data. list.namess has stock names. This is my code:
require(xts)
require(highfrequency)
OvernightRet<-list()
list.namess<- list.files(pattern="*.IS Equity")
list.namess<- list.namess[2]
for(Q in 1:length(list.namess)){
trade<-readRDS(list.namess[Q])
trade<-xts(trade[,-1], order.by = trade[,1])
colnames(trade)[c(1,2)]<-c("PRICE", "SIZE")
#Unduplicating
trade2<-do.call(rbind, lapply(split(trade,"days"), mergeTradesSameTimestamp))
trade2<-trade2[,1]
fun.first= function(x) first(x)
fun.last= function(x) last(x)
A=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.first))
B=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.last))
OvernightRetA <- (as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))
colnames(OvernightRetA)<-list.namess[Q]
OvernightRet[[Q]]<-OvernightRetA
}
df.OvernightRet<-do.call(merge, OvernightRet)
However, it gives error, probably because of not being able to rename the OvernightRetA:
Error in `colnames<-`(`*tmp*`, value = "ACEM IS Equity.rds") :
attempt to set 'colnames' on an object with less than two dimensions
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> df.OvernightRet<-do.call(merge, OvernightRet)
Error in as.data.frame(x) : argument "x" is missing, with no default
As trade and trade2 is huge and not appropriate for dput. I am posting given Open(A), Close(B) and list of names (list.namess) for reproducibility of error.
dput(head(A,10))
structure(c(231.9, 236.35, 230, 226.85, 229.05, 225.7, 226.95,
224.55, 227, 234.65), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459481850,
1459741066, 1459827433, 1459913867, 1460000236, 1460086630, 1460345867,
1460432285, 1460518631, 1460950628), tzone = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))
dput(head(B,10))
structure(c(235.35, 231.2, 226.1, 229.05, 226.45, 225.75, 224.55,
223.75, 231.1, 228.6), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459508732,
1459767943, 1459854348, 1459940748, 1460027143, 1460113538, 1460374518,
1460465873, 1460545568, 1460977541), tzone = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))
dput(list.namess) "ACEM IS Equity.rds"
Kindly help me solve this error.
I believe the problem, as the error message implies, is that you are trying to assign a column header to a single value. You can work around this by changing the line above to:
OvernightRetA <- as.data.frame(as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))

Identify a bar of interest on an OHLC chart on the graph itself

I have calculations that identify bar location of events; my calculation output is the number of a bar within my time series or its index (I can work with either). The issue I have is that when I have more than 50 bars on the chart I have to count the bars to identify the bar of interest.
Is there a way to put some kind of an "indicator" on the chart itself for the bars of interest based on the calculation I have?
Say my program indicated bar 3 is a bar of interest. Are there any tricks that would help in identifying that bar graphically on the chart with an arrow or a dot or anything really to avoid having to count the bars. I realize the time is usually on the x axes, but when the chart has many bars you can't read the time and date off the chart, and even if you did its hard to be sure they are aligned properly.
I provided an OHLC AAPL data for 5 minute bars bellow as a dput():
would like to be able to tell R "when you chart my data please identify bar #3 on the chart"
I usually use chartSeries() of the quantmod package to create my charts, but I am open to using other functions.
dput(AAPL)
structure(c(266, 265.95, 265.93, 265.89, 265.91, 266, 266, 265.96,
265.97, 265.98, 265.93, 265.9, 265.84, 265.86, 265.8625, 265.97,
265.96, 265.89, 265.875, 265.98), .Dim = c(5L, 4L), .Dimnames = list(
NULL, c("Open", "High", "Low", "Close")), index = structure(c(1299962039,
1299962098, 1299962157, 1299962219, 1299962278), tzone = "", tclass = c("POSIXct",
"POSIXt")), tclass = c("POSIXct", "POSIXt"), tzone = "", .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", class = c("xts", "zoo"))
chartSeries(AAPL) would create an OHLC bar chart with 5 bars.
AAPL <- structure(c(266, 265.95, 265.93, 265.89, 265.91, 266, 266, 265.96,
265.97, 265.98, 265.93, 265.9, 265.84, 265.86, 265.8625, 265.97,
265.96, 265.89, 265.875, 265.98), .Dim = c(5L, 4L),
.Dimnames = list(NULL, c("Open", "High", "Low", "Close")),
index = structure(c(1299962039, 1299962098, 1299962157, 1299962219, 1299962278),
tzone = "", tclass = c("POSIXct", "POSIXt")),
tclass = c("POSIXct", "POSIXt"), tzone = "",
.indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", class = c("xts", "zoo"))
chartSeries(AAPL)
addLines(v=3)
addPoints(3, Cl(AAPL)[3])

Heatmap with xts

Hi all I want to plot a heatmap:
df ist {xts} and looking like this:
structure(c(1.3728813559322, 0.871666666666667, 0.586666666666667,
0.34, -0.31, -0.973333333333333, -1.52666666666667, -1.71333333333333,
-0.396666666666667, 0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts",
"zoo"), index = c(1364770740, 1364774340, 1364777940, 1364781540,
1364785140, 1364788740, 1364792340, 1364795940, 1364799540, 1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), .Dim = c(24L, 1L), .Dimnames = list(
NULL, "df.xts"))
As in the following post I want to have y-axis 24 hours - one value per hour, and y-axis the date.
Is it possible to work with the existing xts format?
ggplot2 heatmap to assign colors to breaks
I also found another example with heatmap.plus().
z = matrix(rnorm(30),nrow=5,ncol=6);
rlab = matrix(as.character(c(1:5,2:6,3:7,4:8)),nrow=5,ncol=4);
clab = matrix(as.character(c(1:6,6:1)),nrow=6,ncol=2);
colnames(rlab) = LETTERS[1:dim(rlab)[2]];
colnames(clab) = 1:dim(clab)[2];
heatmap.plus(z,ColSideColors=clab,RowSideColors=rlab);
Example is running, but I would prefer a legend, and my data look different from df - and not an xts with date.
Thanks!
This perhaps isn't an answer to the heatmap question, but this code and output will not display properly in a comment. (Noting my comments above...) Using an object named datcreated from the dput output above, but with the tclass-attribute removed, I subsetted it and:
dput(dat['2013-04-01'])
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = c(1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), .Dim = c(15L, 1L), .Dimnames = list(
NULL, "df.xts"))
> d2 <- dat['2013-04-01']
> tclass(d2) <- "POSIXct"
> d2 #displays with time appropriate format
df.xts
2013-04-01 00:59:00 0.6983333
2013-04-01 01:59:00 2.8466667
2013-04-01 02:59:00 4.6833333
2013-04-01 03:59:00 5.3383333
2013-04-01 04:59:00 5.6666667
2013-04-01 05:59:00 5.6366667
2013-04-01 06:59:00 5.6900000
2013-04-01 07:59:00 5.6966667
2013-04-01 08:59:00 5.5433333
2013-04-01 09:59:00 5.5083333
2013-04-01 10:59:00 4.3350000
2013-04-01 11:59:00 3.0650000
2013-04-01 12:59:00 2.4266667
2013-04-01 13:59:00 1.8866667
2013-04-01 14:59:00 1.4783333
> dput(d2)
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), tclass = c("POSIXct", "POSIXt")), .Dim = c(15L,
1L), .Dimnames = list(NULL, "df.xts"))
No added tclass attribute after : tclass(d2) <- "POSIXct". So I wonder if the heatmap axis problem relates to an improperly formed xts object.

Resources