R xts: generating 1 minute time series from second events

前端 未结 2 521
刺人心
刺人心 2020-12-10 00:17

I have an xts sequence of stock trade events that I want to process to generate 1 minute OHLC time series. For instance this set of trades:

Timestamp   Price         


        
相关标签:
2条回答
  • 2020-12-10 00:31

    If there are no trades within a given minute, to.minutes "will miss that minute record entirely" also. You can get around that by merging with a zero-width, strictly regular xts series.

    ## Make sample data
    > x <- xts(cumsum(rnorm(600, 0, 0.2)), Sys.time() - 600:1) # 10 minutes of secondly data
    > # remove all data from a couple different minutes
    > x['2012-03-19 17:33'] <- NA
    > x['2012-03-19 17:35'] <- NA
    > x <- na.omit(x)
    > 
    > ## Convert to minutes
    > xm <- to.minutes(x)
    > head(xm)
                          x.Open   x.High       x.Low    x.Close
    2012-03-19 17:31:59 0.1945049 1.661000 -0.35943057  1.6610000
    2012-03-19 17:32:59 1.7283877 1.728388 -0.69288918  1.1398868
    2012-03-19 17:34:59 2.0529582 2.603881 -0.80532315 -0.8053232
    2012-03-19 17:36:59 0.5314270 1.189609 -0.94996548  0.5807342
    2012-03-19 17:37:59 0.3761700 1.943363  0.04046976  0.9101720
    2012-03-19 17:38:59 1.0614807 1.722110 -0.22147145  1.4075637
    > axm <- align.time(xm) #align times to begining of next period
    > 
    > # to make strictly regular, create an xts object that has values for each minute
    > tmp <- xts(, seq.POSIXt(start(axm), end(axm), by='min'))
    > out <- cbind(tmp, axm)
    > out
                           x.Open      x.High       x.Low     x.Close
    2012-03-19 17:32:00  0.19450494  1.66100005 -0.35943057  1.66100005
    2012-03-19 17:33:00  1.72838773  1.72838773 -0.69288918  1.13988679
    2012-03-19 17:34:00          NA          NA          NA          NA
    2012-03-19 17:35:00  2.05295818  2.60388093 -0.80532315 -0.80532315
    2012-03-19 17:36:00          NA          NA          NA          NA
    2012-03-19 17:37:00  0.53142696  1.18960858 -0.94996548  0.58073422
    2012-03-19 17:38:00  0.37616997  1.94336348  0.04046976  0.91017202
    2012-03-19 17:39:00  1.06148070  1.72211018 -0.22147145  1.40756366
    2012-03-19 17:40:00  1.28437005  1.28437005 -0.62691689 -0.62691689
    2012-03-19 17:41:00 -0.56820166  0.90339983 -0.77554869  0.26101945
    2012-03-19 17:42:00 -0.07443971 -0.07443971 -0.07443971 -0.07443971
    > na.locf(out)
                           x.Open      x.High       x.Low     x.Close
    2012-03-19 17:32:00  0.19450494  1.66100005 -0.35943057  1.66100005
    2012-03-19 17:33:00  1.72838773  1.72838773 -0.69288918  1.13988679
    2012-03-19 17:34:00  1.72838773  1.72838773 -0.69288918  1.13988679
    2012-03-19 17:35:00  2.05295818  2.60388093 -0.80532315 -0.80532315
    2012-03-19 17:36:00  2.05295818  2.60388093 -0.80532315 -0.80532315
    2012-03-19 17:37:00  0.53142696  1.18960858 -0.94996548  0.58073422
    2012-03-19 17:38:00  0.37616997  1.94336348  0.04046976  0.91017202
    2012-03-19 17:39:00  1.06148070  1.72211018 -0.22147145  1.40756366
    2012-03-19 17:40:00  1.28437005  1.28437005 -0.62691689 -0.62691689
    2012-03-19 17:41:00 -0.56820166  0.90339983 -0.77554869  0.26101945
    2012-03-19 17:42:00 -0.07443971 -0.07443971 -0.07443971 -0.07443971
    

    Or, if you really want zeros when there are no values, you could do out[is.na(out)] <- 0

    0 讨论(0)
  • 2020-12-10 00:36

    There are to.period() functions, eg to.minute() in xts which do that.

    Dirk

    0 讨论(0)
提交回复
热议问题