在dolphindb中计算小时级别均价,一小时内的时刻跨两个交易时段这样的要怎么处理呢?

在期货交易中 期货数据是从9:00——11:30,13:3015:00。
因为11:00—11:30交易半小时,下午13:30—14:00交易半小时,所以想把这两个时间段的半小时合为一小时应该怎么做呢
请先 登录 后评论

2 个回答

chenweijian

可以使用asof函数,asof函数可以确定每一行所属的group,示例如下:

Y=[09:00m,10:00m ,11:00m,14:00m,15:00m,16:00m]
select avg(price) from trade group by symbol,date(timestamp),Y[Y.asof(minute(timestamp))]

attachments-2021-06-OEZ8zHgL60c08677eb94a.png

请先 登录 后评论
谭华

数据:

n = 1000000
date = take(2019.01.01..2019.01.15,n)
time=(09:30:00.000 + rand(2*60*30*1000, n/2)).sort!() join (13:30:00.000 +rand(2*60*60*1000 , n/2)).sort!()
timestamp= concatDateTime(date,time)
price = 100+cumsum(rand(0.02,n)-0.01)
volume= rand(100,n)
symbol = rand(`6500`6011`0633`6633,n)
trade = table(symbol,timestamp,price,volume).sortBy!(`symbol`timestamp)
Y=[09:00m,10:00m ,11:00m,14:00m,15:00m,16:00m]

解决方案:

方案一:

我们可以把这两个半个小时的数据单独拎出来,与其他数据分开计算, 最后再将所有计算结果用UNION拼接起来。

t = select *
from
(
    (select 
        avg(price),
        y 
      from trade 
      where hour(timestamp) not in (11,13) 
      group by symbol,date(timestamp),Y[Y.asof(minute(timestamp))] as y
      )
union 
    (select 
        avg(price),
        minute(11:00:00.000) as y 
      from trade 
      where hour(timestamp) in (11,13) 
      group by symbol,date(timestamp) 
      )
)
order by symbol,date_timestamp,y

方案二:

利用asof进行时间的归并,将11:00 ~11:30和13:30~14:00 的数据都归并到11:00。

n = 1000000
date = take(2019.01.01..2019.01.15,n)
time=(09:30:00.000 + rand(2*60*30*1000, n/2)).sort!() join (13:30:00.000 +rand(2*60*60*1000 , n/2)).sort!()
timestamp= concatDateTime(date,time)
price = 100+cumsum(rand(0.02,n)-0.01)
volume= rand(100,n)
symbol = rand(`6500`6011`0633`6633,n)
trade = table(symbol,timestamp,price,volume).sortBy!(`symbol`timestamp)
X = [9,10 ,11,14,15,16]
timer(1) b = select avg(price),y from trade group by symbol,date(timestamp),X[X.asof(hour(timestamp))] as y


性能测试:

解决方案               耗时

方案一                135ms

方案二                 39 ms

请先 登录 后评论