Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

怎么计算高平均价与低平均价是否在前20日收盘价的区间百分比

请问在DolphinDB database中,我想计算某2列的当前值,与另一列的前面20个数做比较,然后算出是否在它的区间的百分比,有什么函数能用吗?
举例来说,如下图所示:
image.png
比如6月17日这行数据的前面20行数据的close值(即图中标1这列),若其中有75%的数据是在upAvgPrice(图中标2)和upAvgPrice(图中标3)这两个值的区间中,那么signal(图中标4)的值我会设为true,否则设为false。


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

1 Answer

0 votes
by (71.8m points)

这是一个滑动窗口的计算问题。我们使用高阶函数moving来解决问题。对于每一个窗口的处理,写一个自定义函数rangeTest来处理,返回true或false。

def rangeTest(close, downlimit, uplimit){
    size = close.size() - 1
    return between(close.subarray(0, size), downlimit.last() : uplimit.last()).sum() >= size*0.75
}

update t set signal = moving(rangeTest, [close, downAvgPrice, upAvgPrice], 21)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...