计算rps排名

批计算横截面rps排名,只想到通过pivot by或panel转换为横截面数据然后再rowrank计算出横截面排名,期间还涉及到表和矩阵转换。请问有没有高效的写法?

请先 登录 后评论

1 个回答

陈无忌

您好,DolphinDB 通过提供 context by 的 SQL 语句的方式,为时序数据高效便捷加工提供了方便的工具。通过 context by tradeDate + rank 函数的方式,我们可以快捷地计算个股在当日涨幅强度的排行榜。

实现代码如下:

update t set rps = rank(pct_ret) context by tradeDate

完整代码如下:

def genDayKData(startDate, endDate, securityId){
    tradeDate = table(getMarketCalendar("CFFEX", startDate , endDate) as tradeDate)
    // Use matrix and for loop way to generate simulated daily K line.
    randStartOpen = double(int(randNormal(100, 30, size(securityId))))
    openList = [randStartOpen]
    for (day in tradeDate[1:]){
            openList.append!(openList[size(openList)-1] + randNormal(0, 2, size(securityId)))
    }
    res = cj(table(securityId as securityId), tradeDate)
    update res set open = flatten(openList.transpose())
    update res set close = open + norm(0,2,size(res))
    return res
}
// Usage example
// Prepare parameters
securityIdNum = 10
securityId = lpad(string(1..securityIdNum), 6, "000000") $ SYMBOL
startDate = 2023.01.01
endDate = 2023.01.31
// Generate simulated daily K line
t = genDayKData(startDate, endDate, securityId)
update t set pct_ret = close \ close.prev() - 1 context by securityId
update t set rps = rank(pct_ret) context by tradeDate

希望对您的问题有帮助!

请先 登录 后评论
  • 1 关注
  • 0 收藏,565 浏览
  • 迈拓将 提出于 2023-06-27 10:16

相似问题