开发者

Filling missing data in xts stock price object

开发者 https://www.devze.com 2023-03-07 19:47 出处:网络
I have 1 minute intraday price data which has missing data points. As such I want to fill them. I read through the suggestions in the following post and tried a similar procedure:

I have 1 minute intraday price data which has missing data points. As such I want to fill them.

I read through the suggestions in the following post and tried a similar procedure: R: Filling missing dates in a time series?

In my case the missing data point is the first trade i.e. at 09:31:00.

> head(s)
                    AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00     6.08     6.10    6.07      6.10        298   6.087           0        39
2010-09-10 09:33:00     6.10     6.14    6.10      6.14        274   6.122           0        70
2010-09-10 09:34:00     6.14     6.15    6.13      6.13        472   6.133           0        96
2010-09-10 09:35:00     6.13     6.14    6.13      6.13        291   6.133           0        68
2010-09-10 09:36:00     6.13     6.13    6.11      6.11        548   6.123          开发者_高级运维 0        97
2010-09-10 09:37:00     6.11     6.11    6.11      6.11         67   6.110           0        26

> na.locf(s, xout=seq(as.POSIXct(head(index(s), 1) - 60), as.POSIXct(tail(index(s), 1)), by="1 min")) -> ss

> head(ss)
                    AMR.Open AMR.High AMR.Low AMR.Close AMR.Volume AMR.WAP AMR.hasGaps AMR.Count
2010-09-10 09:32:00     6.08     6.10    6.07      6.10        298   6.087           0        39
2010-09-10 09:33:00     6.10     6.14    6.10      6.14        274   6.122           0        70
2010-09-10 09:34:00     6.14     6.15    6.13      6.13        472   6.133           0        96
2010-09-10 09:35:00     6.13     6.14    6.13      6.13        291   6.133           0        68
2010-09-10 09:36:00     6.13     6.13    6.11      6.11        548   6.123           0        97
2010-09-10 09:37:00     6.11     6.11    6.11      6.11         67   6.110           0        26

As you can see above the object returned is not filled as desired.

Below you can see that I correctly specified the start and end times.

> as.POSIXct(head(index(s), 1) - 60)
[1] "2010-09-10 09:31:00 EDT"

> as.POSIXct(tail(index(s), 1))
[1] "2010-09-10 16:00:00 EDT"
> 

Could this be because the date range has a time-zone specified whereas the original POSIX index does not? I tried to remove the tz by specifiying tz="" but that does not remove it. That being said, the time-zone may be just a red herring.

I saved the data in rda (binary) format if anyone is interested in testing:

http://www.speedyshare.com/files/28576853/test.rda

Appreciate the help.


na.locf operates on the data, not the index. If you want to add a row of NA to the data, you would need to make a suitable xts object to rbind to s:

miss <- xts(matrix(1*NA,1,NCOL(s)), first(index(s))-60)
s <- rbind(miss,s)
s <- na.locf(s, fromLast=TRUE)
0

精彩评论

暂无评论...
验证码 换一张
取 消