如何根据其他的信息切分时间段,比如按一段时间内累计成交150万股切分为一段?

假如已知分钟线数据,某只股票每成交大约150万股便做一次时间切分,最后得到时间窗口不等的若干条数据,大约150万的规则是指当这个点加进去离150万更近,则加入这个点,否则不加。

attachments-2021-11-YhqYqNPM61a3852b5631c.png

请先 登录 后评论

1 个回答

Yating Xie

可以用以下代码实现,分组的关键在iif(accumulate(caclCumVol{1500000}, volume) ==volume, time, NULL).ffill() 这个表达式。

// 自定义一个累计函数calcCumVol,如果当前这个组需要包含这个点,返回累计的volume,否则开始一个新的组,也就是返回当前点的volume。
def caclCumVol(target, a, b){
 newVal = a + b
 if(newVal < target) return newVal
 else if(newVal - target > target - a) return b
 else return newVal
}

// 输入数据
t = loadText("f:/DolphinDB/sample.csv")

// 分组的关键在iif(accumulate(caclCumVol{1500000}, volume) ==volume, time, NULL).ffill() 这个表达式
// 如果累计值 == volume,表示一个新的组的开始
// 如果是新的组开始,记录当前时间,否则空值。然后用ffill填充,这样同一组的数据都会用这个组的开始时间。
output = select first(wind_code) as wind_code, first(date) as date, sum(volume) as sum_volume, last(time) as endTime from t group by iif(accumulate(caclCumVol{1500000}, volume) ==volume, time, NULL).ffill() as startTime


请先 登录 后评论