create an OHLC series from ticker data using R

前端 未结 3 1468
梦如初夏
梦如初夏 2020-12-19 08:08

This seems like it should be a common thing, but all my searching comes up with half or unfinished answers.

I have a set of data in a csv. But the data is set up so

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 08:40

    from http://r.789695.n4.nabble.com/Convert-tick-data-to-OHLC-td926206.html

    ohlc <- function(ttime,tprice,tvolume,fmt) {
        ttime.int <- format(ttime,fmt)
        data.frame(time = ttime[tapply(1:length(ttime),ttime.int,function(x) {head(x,1)})],
        .Open = tapply(tprice,ttime.int,function(x) {head(x,1)}), 
        .High = tapply(tprice,ttime.int,max),
        .Low = tapply(tprice,ttime.int,min),
        .Close = tapply(tprice,ttime.int,function(x) {tail(x,1)}),
        .Volume = tapply(tvolume,ttime.int,function(x) {sum(x)}),
        .Adjusted = tapply(tprice,ttime.int,function(x) {tail(x,1)}))
    } 
    

提交回复
热议问题