您好,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
希望对您的问题有帮助!