【问题标题】:Struggling to convert tick data with price and volumes to 5min in R努力将价格和交易量的报价数据转换为 R 中的 5 分钟
【发布时间】:2013-11-10 09:53:28
【问题描述】:

这里有这样的数据:

time        price   volume  price*volume
14:56:42    31.15   173 540327
14:56:36    31.15   100 311500
14:56:27    31.16   4   12464
14:56:24    31.16   1   3116
14:56:21    31.16   46  143336
14:56:18    31.15   32  99680
14:56:15    31.16   6   18696
14:56:12    31.16   12  37392
14:56:06    31.16   15  46740
14:56:03    31.16   54  168264
14:55:57    31.19   1   3119
14:55:54    31.19   10  31190

当我使用to.period() 时,它只返回一个带有 ohlc 价格的 xts 对象,缺少数量和价格*数量,我将如何将这样的数据转换为 5 分钟 OHLCV

【问题讨论】:

  • 如果您可以在问题中包含您的数据,这有助于鼓励人们回答。例如。如果您粘贴的数据是使用print(x) 制作的,则还包括dput(x) 的输出。

标签: r xts


【解决方案1】:

这里有一个自定义函数:

period.apply.ohlc <- function(x,FUN= 'mean',INDEX=endpoints(x,"mins",k=1)){
  ll <- sapply(1:(length(INDEX) - 1), function(y) {
    xi <- x[(INDEX[y] + 1):INDEX[y + 1]]
    sapply(xi,FUN)
  })
  xts(t(ll),order.by=index(dat.xts[INDEX]))
}

period.apply.ohlc(dat.xts)
#                     price volume price_volume
# 2013-11-10 14:55:57 31.190    5.5      17154.5
# 2013-11-10 14:56:42 31.157   44.3     138151.5

dat.xts:

dat.xts <- read.zoo(text='time            price   volume  price_volume
14:56:42    31.15   173 540327
14:56:36    31.15   100 311500
14:56:27    31.16   4   12464
14:56:24    31.16   1   3116
14:56:21    31.16   46  143336
14:56:18    31.15   32  99680
14:56:15    31.16   6   18696
14:56:12    31.16   12  37392
14:56:06    31.16   15  46740
14:56:03    31.16   54  168264
14:55:57    31.19   1   3119
14:55:54    31.19   10  31190',tz="",index=1,format="%H:%M:%S",header=TRUE)

【讨论】:

  • FUN 应用于所有列:我想 volume 和 price_volume 应该相加,而不是平均。但如果我将 FUN='mean' 更改为 FUN='sum',那么价格列也会被求和!
  • @DarrenCook 不错!但是很容易给出一个函数和索引的向量,并将sapply替换为mapply,类似于:mapply(FUN=function(x,y) do.call(x,xi[,y]),c('mean','sum','sum'),c(1,2,3))
  • 你比我更擅长像数学家一样思考。你会看到我的回答更加明确:-)
【解决方案2】:

从 agstudy 的答案中借用数据(因此制作 1 分钟条,而不是 5 分钟),我会使用这个:

dat.xts <- as.xts(dat.xts)   ## otherwise you get an error align.time 
                             ## since (dat.xts is a zoo object)
bars <- period.apply(dat.xts, 
                    endpoints(dat.xts,"secs",60),
                    function(xx){
                      ticks=coredata(xx$price)
                      c( first(ticks),max(ticks), min(ticks),
                         last(ticks), sum(xx$volume), sum(xx$price_volume) )
                    })
colnames(bars) <- c("Open","High","Low","Close","Volume","Price*Volume")
align.time(bars,60)

即为period.apply() 提供了一个 xts 对象,该对象在给定的 1 分钟内保存刻度。我使用firstmaxminlast 来制作 OHLC 数据。 (我必须使用coredata 否则这些函数会报错。)

然后将每个周期的 volume 和 price_volume 相加。

在主循环之后,我分配列标题,并对时间戳进行四舍五入。输出是:

                     Open  High   Low Close Volume Price*Volume
2013-11-11 14:56:00 31.19 31.19 31.19 31.19     11        34309
2013-11-11 14:57:00 31.16 31.16 31.15 31.15    443      1381515

【讨论】:

  • 伙计们,你们如何在 C# 上做到这一点?
  • @JaysonRagasa 非常不同。我建议您提出一个新问题,并显示您的数据所在的数据结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
相关资源
最近更新 更多