Calculate ratio compared with the previous period for the selected records

Task:Find the trading information and increase rate of a stock in the three trading days with the highest share price.


1 import pandas as pd
2 stock1001_file = "E:\\txt\\stock1001_price.txt"
3 stock1001 = pd.read_csv(stock1001_file,sep = '\t')
4 CL = stock1001['CL']
5 CL_psort = CL.argsort()[::-1].iloc[:3].values
6 CL_psort_shift1 = CL_psort-1
7 CL_rise = CL[CL_psort].values/CL[CL_psort_shift1].values-1
8 max_3 = stock1001.loc[CL_psort].reset_index(drop = True)
9 max_3['RISE'] = CL_rise
10 print(max_3)

Python can't use position to directly calculate ratio compared with the previous period. It needs to construct a position sequence of the previous period and then calculate.


1 =Stocks.sort(TradingDate)  
2 =A1.psort(ClosePrice:-1)([1,2,3]) The position of three days with highest share price
3 =A1.calc(A2, ClosePrice/ClosePrice[-1]-1) Calculate the increase rate for the three days
4 =A1(A2).new(TradingDate,ClosePrice,A3(#):IncreaseRate)  

esProc is very good at these calculations related to order and position, and the code is simple and in line with natural thinking.