请教一下,日度K线表,如果我想每天计算最近5天所有 开、高、低、收(共20个价格数据)的均值和标准差,有办法实现吗?

如题,请教一下,日度K线表,如果我想每天计算最近5天所有 开、高、低、收(共20个价格数据)的均值和标准差有办法实现吗?就是滑动计算的时候考虑多列数据来计算统计值

请先 登录 后评论

1 个回答

Shena Mao

这边可以将公式拆分,写一个自定义函数,即可高效算出均值和标准差。

def std_ohlc(open, close, high, low){
	count = mcount(close,5)+mcount(open,5)+mcount(high,5)+mcount(low,5)
	avg = (msum(close, 5)+msum(open, 5)+msum(low, 5)+msum(high, 5))\count
	sum2 = msum(close*close, 5)+msum(open*open, 5)+msum(high*high , 5)+msum(low*low, 5)
	return sqrt((sum2 -count* square(avg))\(count-1)), avg
}

1.30.19\2.00.7版本之后,新增msum2函数,即sum2的写法可以改为msum2(close,5)等。


验证结果:

bar = table(take(2020.01.01..2020.01.10,20) as tradedate, rand(10.0,20) as open, rand(10.0,20) as close, rand(10.0,20) as high, rand(10.0,20) as low, take(`a,10) join take(`b,10) as id)
select id, open, close , high, low, std_ohlc(open, close, high, low) as `std`avg from bar context by id

结果争取,输出为:

attachments-2022-06-MEgBnm8t62a6a58bf0df2.png

请先 登录 后评论