How to downsample data based on the frequency of 'weekEnd' in DolphinDB?

20 Views Asked by At

I currently use the resample function to lower the data frequency, as shown in the following script:

def agg_k_longer(ticker){    
    klday get_adjust_klday(ticker)    
    rule = "W"
    return select
        indexedSeries(date,date).resample(rule,last) as date,
        indexedSeries(date,code).resample(rule,last) as code,
        indexedSeries(date,open).resample(rule,first) as open,
        indexedSeries(date,high).resample(rule,max) as high,
        indexedSeries(date,low).resample(rule,min) as low,
        indexedSeries(date,close).resample(rule,last) as close,
        indexedSeries(date,volume).resample(rule,sum) as volume,
        indexedseries(date amount).resample(rule,sum) as amount
    from k1daycontext by code
}
agg_k_1 onger(`600519.SH`300750.SZ)

Is there a better way?

1

There are 1 best solutions below

0
dbaa9948 On

The interval function can be used for ordinary upsampling and downsampling, but the time unit 'w' of the parameter duration can only group data by 7 days. To modify data frequency based on the rule of 'weekEnd', you can use the transFreq function as shown in the following script:

select  
    last(tradetime) as tradetime,
    last(securityid) as securityid,
    first(open) as open,
    max(high) as high,
    min(low) as low,
    last(close) as close,
    sum(vol) as vol,
    sum(val) as val
from t group by securityid, transFreq(tradetime, "W")