# Calculate ratio compared with the previous period for continuous intervals of grouped subsets

Task:Calculate the longest consecutive rising days of each stock.

### Python

 1 import pandas as pd 2 def con_rise(stock:pd.DataFrame): 3 rise_day_list = [] 4 rise_num = 0 5 shift_1 = stock['CL']>stock['CL'].shift(1) 6 for bl in shift_1: 7 if bl == False: 8 rise_num = 0 9 else: 10 rise_num+=1 11 rise_day_list.append(rise_num) 12 return max(rise_day_list) 13 stock_file = "E:\\txt\\StockRecords.txt" 14 stock_info = pd.read_csv(stock_file,sep="\t") 15 stock_info.sort_values(by='DT',inplace=True) 16 stock_group = stock_info.groupby(by='CODE') 17 max_rise_list = [] 18 for index,stock_g in stock_group: 19 code = stock_g.iloc[0]['CODE'] 20 max_rise_list.append([code,con_rise(stock_g)]) 21 max_rise_df = pd.DataFrame(max_rise_list,columns=['CODE','con_rise']) 22 print(max_rise_df)

### esProc

 A B 1 E:\\txt\\StockRecords.txt 2 =file(A1).import@t() 3 =A2.sort(DT) 4 =A3.group(CODE) 5 =A4.new(CODE,func(A6,~):con_rise) 6 func 7 =(num=0,A6.max(num=if(CL>CL[-1],if(#==1,0,num+1),0)))

The idea of comparing with last period for continuous periods of a single stock: if it is higher than the stock price of the previous day, add 1; if it is not greater than, set 0; finally, check the maximum value in the sequence. The calculation method of a single stock is written as a function, and the table of each stock is passed in as a parameter. esProc can easily call the function in the loop function to get the result. With the same idea, pandas code looks much more complex.