5.2 Reference a neighboring interval

 

SPL allows referencing a sub-sequence composed of neighboring records through an interval of relative positions.
One task is to list the average closing price of the past 20 days for each date from 2020-01-01 to 2020-01-10 in SSE Composite Index. Below is part of the data:

DATE CLOSE OPEN VOLUME AMOUNT
2020/01/02 3085.1976 3066.3357 292470208 3.27197122606E11
2020/01/03 3083.7858 3089.022 261496667 2.89991708382E11
2020/01/06 3083.4083 3070.9088 312575842 3.31182549906E11
2020/01/07 3104.8015 3085.4882 276583111 2.88159227657E11
2020/01/08 3066.8925 3094.2389 297872553 3.06517394459E11

SPL script:

A
1 =T(“SSEC.csv”)
2 =A1.sort(DATE)
3 =A2.pselect@a(DATE>=date(“2020/01/01”) && DATE<=date(“2020/01/10”))
4 =A2(A3).derive(A2.calc(A3(#),avg(CLOSE[-19:0])):MA20)

A1: Import the SSE Composite Index data.
A2: Use sort() function to sort data by Date.
A3: Get ordinal numbers of records from 2020-01-01 to 2020-01-10.
A3: Loop each selected record to calculate the average closing price of the past 20 days for each target date according to the selected ordinal numbers. CLOSE[-19:0] represents a sequence of closing prices from 19 days before to the current date.