【问题标题】:R xts: generating 1 minute time series from second eventsR xts:从第二个事件生成 1 分钟的时间序列
【发布时间】:2012-03-20 07:18:22
【问题描述】:

我有一个 xts 的股票交易事件序列,我想对其进行处理以生成 1 分钟的 OHLC 时间序列。例如这组交易:

Timestamp   Price  Size
9:30:00.123 12.32  200
9:30.00.532 12.21  100
9:30.32.352 12.22  500
9:30.45.342 12.35  200

应该是 9:30:00 的记录:

Timestamp Open  High  Low   Close
9:30:00   12.32 12.35 12.21 12.35

我的处理方法是按分钟拆分原始交易系列:

myminseries = do.call(rbind, lapply(split(mytrades, "minutes"), myminprocessing))

这会产生我想要的记录,但有一个问题:如果股票在给定的分钟内没有任何交易,我将完全错过该分钟记录。相反,我想要的是为丢失的交易分钟记录全 0。例如,如果 9:31:00 没有任何交易,我应该:

Timestamp  Open  High  Low   Close
9:30:00    12.32 12.35 12.21 12.35
9:31:00    0     0     0     0
9:32:00    12.40 12.42 12.38 12.42

如何回填 1 分钟系列?还是应该使用与 split() 完全不同的方法?

【问题讨论】:

    标签: r xts


    【解决方案1】:

    如果在给定的分钟内没有交易,to.minutes“也将完全错过该分钟记录”。你可以通过合并一个零宽度、严格规则的 xts 系列来解决这个问题。

    ## 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
    

    或者,如果你真的想在没有值的情况下使用零,你可以这样做out[is.na(out)] &lt;- 0

    【讨论】:

      【解决方案2】:

      to.period() 函数,例如 xts 中的 to.minute() 可以做到这一点。

      德克

      【讨论】:

      • 看起来他可能也想将 align.time 包裹起来。
      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      相关资源
      最近更新 更多