Python pandas has a pct_change function which I use to calculate the returns for stock prices in a dataframe:
ndf['Return']= ndf['TypicalPrice'].pct_change()
I am using the following code to get logarithmic returns, but it gives the exact same values as the pct.change() function:
ndf['retlog']=np.log(ndf['TypicalPrice'].astype('float64')/ndf['TypicalPrice'].astype('float64').shift(1))
#np is for numpy
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…