 To calculate data that are arranged in a certain order, we often encounter cross-row references. For example, calculate the increase of stock price compared to the previous day, the moving average price of a stock for 20 days and so on.

SPL supports references to adjacent records by their relative positions in loop calculations.

[e.g. 1] Calculate the increase of the closing price per day of the Shanghai composite index compared to the previous day. Some of the Shanghai composite index data are as follows:

The SPL script looks like this:

A1: import the Shanghai Composite Index table.

A2: use the sort function to sort the data by date.

A3: calculate the increase of daily stock price compared to the previous day, in which CLOSE[-1] represents the price of the previous day. The derive function is used to add computed columns to the ordered table.

We can use the interval of relative positions to refer to the sequence of adjacent records.

[e.g. 2] List the average 20-day closing price of the Shanghai composite index for each day from January 1st to 10th, 2020. Some of the Shanghai composite index data are as follows:

A1: import the Shanghai Composite Index table.

A2: use the sort function to sort the data by date.

A3: select the row numbers of the records from January 1st to 10th, 2020.

A4: calculate the average 20-day closing price for each day by loop operation based on the selected row numbers, in which CLOSE[-19:0] represents the sequence of closing price for the first 19 days and the current day.

SPL also supports adjacent record references in group.

[e.g. 3] Calculate the daily increase of each stock among multiple stocks. Some of the stock market data are as follows:

The SPL script looks like this:

A1: import stock data table.

A2: use the sort function to sort the data by date.

A3: group stock data by stock code.

A4: loop through each stock and calculate its increase of daily price compared to the previous day, and concatenate the records at last.

[e.g. 4] Calculate the trading volume increase of each stock’s maximum closing price compared to the previous day. Some of the stock market data are as follows:

The SPL script looks like this:

A1: import stock data table.

A2: use the sort function to sort the data by date.

A3: group stock data by stock code.

A4: create a table sequence based on the grouped stock market data. Loop through the row number where the record of the highest closing price of each stock is located and calculate the increase in trading volume of the current day and the previous day.