-
Notifications
You must be signed in to change notification settings - Fork 342
Stocks that have risen by 9.5% for three consecutive days
The following is the daily closing price record StockRecords of a certain stock exchange within one month, where CODE column is the stock code, DT is the date, and CL is the closing price:
Try to find stocks that have risen by 9.5% for three consecutive days this month.
Sort the records by code and date, and then group them by stock code to obtain a price list for each stock over the past month. This makes it easy to calculate the daily rise and fall rate of each stock. By comparing the rise and fall rate with the standard rate, you can determine whether the increase meets the standard. Finally, count the number of days in which the increase meets the standard for three consecutive days.
A | |
---|---|
1 | =T("StockRecords.txt") |
2 | >r=0.095 |
3 | =A1.derive(:UP).sort(CODE,DT) |
4 | =A3.group@o(CODE) |
5 | >A4.run(~.run(UP=if(#==1,0,(CL-CL[-1])/CL[-1]))) |
6 | =A4.select(~.count(UP>r && UP[-1]>r && UP[-2]>r)>0).(CODE) |
https://try.esproc.com/splx?57N
A1 reads the closing price records of the transaction, and A2 sets the standard increase rate r.
On the basis of transaction records, A3 adds a UP field to store the rise and fall rate of closing prices, and sorts the records by stock code and trading date for grouping and calculating the rise and fall rate using adjacent date data.
A4 groups the records in A3 by stock code. Since they have already been sorted earlier, @o option is used to indicate that there is no need to sort them again. A5 loops through all groups and calculates the rise and fall rate UP across rows within each group. The first day's rise and fall rate is calculated as 0. In SPL, cross row calculations can be implemented using x[], while grouping with the group function actually divides the transaction records in the table sequence into different groups. When A5 assigns a value to the UP field, it also changes the data in the original A3:
A6 selects the groups in A4's grouping data that have achieved the target increase for three consecutive days and lists their stock codes:
SPL Resource: SPL Official Website | SPL Blog | Download esProc SPL | SPL Source Code