5.5 Locate a specified member and compare it with the neighboring row

 

Find the date when each stock has the highest closing price and calculate the growth rate of the transaction amount compared with that of the previous date. Below is part of the stock data table:

DATE CODE NAME CLOSE OPEN AMOUNT
2020/01/02 000001 SS 3085.1976 3066.3357 3.27197122606E11
2020/01/03 000001 SS 3083.7858 3089.022 2.89991708382E11
2020/01/06 000001 SS 3083.4083 3070.9088 3.31182549906E11
2020/01/07 000001 SS 3104.8015 3085.4882 2.88159227657E11
2020/01/08 000001 SS 3066.8925 3094.2389 3.06517394459E11

SPL script:

A
1 =T(“Stock.csv”)
2 =A1.sort(DATE)
3 =A2.group(CODE)
4 =A3.new(CODE,NAME,(p=~.pmax(CLOSE),~.calc(p,AMOUNT/AMOUNT[-1])):INCREASE)

A1: Import the stock data table.
A2: Use sort() function to sort the data table by DATE.
A3: Group the data table by stock code.
A4: Create a table sequence based on the grouped data table, loop each stock to get the ordinal number of the record when each stock has the highest closing price, and calculate the growth rate of the transaction amount compared with that of the previous date.