Advertisement
Offew1995

Stok daily return

Jun 22nd, 2023 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | Cryptocurrency | 0 0
  1. def daily_return_estimator(df):
  2.     df_daily_return = df.copy()
  3.  
  4. # Loop through each stock (column of the dataframe except Date collumn)
  5.     for i in df.columns[1:]:
  6.  
  7.         # Loop through each row/price belonging to the stock
  8.         for j in range(1, len(df)):
  9.  
  10.             # Calculate the percentage of change from the previous day's close price.
  11.             # Simple equation of percentage change.
  12.             df_daily_return[i][j] = ((df[i][j]- df[i][j-1])/df[i][j-1]) * 100
  13.  
  14.          # set the value of first row to zero (previous value is not available)
  15.         df_daily_return.loc[0, i] = 0
  16.  
  17.     return df_daily_return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement