R: How to change the intervals (holidays) in the time series of the daily stock exchange index according to the previous day?

I speak Russian and work with the daily index of daily series from different countries. In order to make comparisons between different indices (for example, correlation, causality, etc.), I need all the series to have the same number of rows, but since there are differences in the holidays in different countries, the number of rows in each series varies.

I work with extracted files from yahoo finance, with the .csv format, like ...

> head(sp)
>           Date    Open    High     Low   Close     Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14

I need ... for example, suppose that day 2010-01-07 is a holiday, in this case the next line (line 1285) in the file is day 2010-01-08:

> head(sp)
>           Date    Open    High     Low   Close     Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
>1285 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000   1144.98

You need to fill in the gap in 2010-01-07 with the data of the day of the previous day, for example:

> head(sp)
>           Date    Open    High     Low   Close     Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
>1285 2010-01-07 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
>1284 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000   1144.98

How can i do this???

( , kkk)

>library(PerformanceAnalytics)
>library(tseries)
>library(urca)
>library(zoo)
>library(lmtest)
>library(timeDate)
>library(timeSeries)

>setwd("C:/Users/Fatima/Documents/R")

>sp = read.csv("SP500.csv", header = TRUE, stringsAsFactors = FALSE)
>sp$Date = as.Date(sp$Date)
>sp = sp[order(sp$Date), ]

+4
2

xts :

DF <- read.table(text = "           Date    Open    High     Low   Close     Volume Adj.Close
1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
1285 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000   1144.98", header = TRUE)

DF$Date <- as.Date(DF$Date)

library(xts)
X <- as.xts(DF[,-1], order.by = DF$Date)
na.locf(merge(X, seq(min(DF$Date), max(DF$Date), by = 1)))
#              Open    High     Low   Close     Volume Adj.Close
#2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
#2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
#2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
#2010-01-07 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
#2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000   1144.98

Edit:

: :

dates <- seq(min(DF$Date), max(DF$Date), by = 1)
#you might have to adjust the following to the translations in your locale
dates <- dates[!(weekdays(dates) %in% c("Saturday", "Sunday"))]
na.locf(merge(X, dates))
+3

read.zoo, , zoo . , na.locf, NA, .

Lines <- "Date,Open,High,Low,Close,Volume,Adj.Close
2010-01-04,1116.56,1133.87,1116.56,1132.99,3991400000,1132.99
2010-01-05,1132.66,1136.63,1129.66,1136.52,2491020000,1136.52
2010-01-06,1135.71,1139.19,1133.95,1137.14,4972660000,1137.14
2010-01-11,1140.52,1145.39,1136.22,1144.98,4389590000,1144.98"

library(zoo)
z <- read.zoo(text = Lines, header = TRUE, sep = ",")
zout <- na.locf( merge(z, zoo(, seq(start(z), end(z), by = "day"))) )

:

> zout
              Open    High     Low   Close     Volume Adj.Close
2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000   1132.99
2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000   1136.52
2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
2010-01-07 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
2010-01-08 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
2010-01-09 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
2010-01-10 1135.71 1139.19 1133.95 1137.14 4972660000   1137.14
2010-01-11 1140.52 1145.39 1136.22 1144.98 4389590000   1144.98

na.locf na.approx method = "constant" :

na.approx(z, xout = seq(start(z), end(z), by = "day"), method = "constant")

.

NA :

library(chron)
zout[is.weekend(time(zout)), ] <- NA

:

library(chron)
zout[!is.weekend(time(zout))]
+2

All Articles