Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

比如正常 group by [1,2,3,4] 会分成 4 组:[1],[2],[3],[4]. 但我现在希望能累积式的分组为 [1],[1,2],[1,2,3],[1,2,3,4]

我的应用场景是这样的:

比如算一天的日内分段的vwap,正常分组可能是 group by bar(time, 30*60*1000),这样得到的是每30min一个bar里的vwap。但现在需要的是累积的,即第一个组算出来是前30min 的vwap,第二个组算出来是前 60min 的vwap,第三个是前 90 min 的,依次类推。

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
436 views
Welcome To Ask or Share your Answers For Others

1 Answer

可以写个自定义函数来实现这个功能,比如:

t= table(1 2 3 4 1 2 3 4 as id, 1..8 as value)
grps = groups(t.id)
keys = grps.keys().sort()
grpCnt = keys.size()
ag1 = array(DOUBLE, grpCnt)
ag2 = array(DOUBLE, grpCnt)
rowIndices = array(INDEX, 0, t.size())
for(idx in 0:grpCnt){
 rowIndices.append!(grps[keys[idx]])
 ag1[idx] = avg(t.value[rowIndices])
 ag2[idx] = sum(t.value[rowIndices])
}
result = table(keys, ag1, ag2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...