python - Calculating the stock price volatility from a 3-columns csv -


i looking way make following code work:

import pandas  path = 'data_prices.csv' data =  pandas.read_csv(path, sep=';') data = data.sort_values(by=['ticker', 'date'], ascending=[true, false]) data.columns 

i have 2 dimensional array 3 columns, data looks this:

date;ticker;price 20151231;a un equity;41.81 20151230;a un equity;42.17 20151229;a un equity;42.36 20151228;a un equity;41.78 20151224;a un equity;42.14 20151223;a un equity;41.77 20151222;a un equity;41.22 20151221;a un equity;40.83 20151218;a un equity;40.1 20091120;pcg un equity;42.1 20091119;pcg un equity;41.53 20091118;pcg un equity;41.86 20091117;pcg un equity;42.23 20091116;pcg un equity;42.6 20091113;pcg un equity;41.93 20091112;pcg un equity;41.6 20091111;pcg un equity;42.01 

now, want calculate x-day realized volatility x came input field , x should not bigger number of observations.

the steps need taken:

  • calculate log return each line
  • take returns , run standard deviation on top of it
  • multiply square root of 255 normalize per annum volatility

apologies, it's not clear on sort of output you're hoping i've assumed want enter ticker , period (x) , see current volatility number. below have made use of numpy, in case don't have library.

essentially i've created dataframe of original data , new df filtered given ticker (where user needs type in 'a' or 'pcg' part, because 'un equity' assumed constant). in new df, after checking period (x) input not high, output recent annualised volatility value.

import numpy np import pandas pd  data = pd.read_csv('dump.csv', sep=';') data = data.sort_values(by=['ticker','date'],ascending=[true,true])   def vol(ticker, x):     df = pd.dataframe(data)     df['pct_chg'] = df.price.pct_change()     df['log_rtn'] = np.log(1 + df.pct_chg)      df_filtered = df[df.ticker==ticker+' un equity']      max_x = len(df_filtered) - 1     if x > max_x:         print('too many periods. reduce x')      df_filtered['vol'] = pd.rolling_std(df_filtered.log_rtn, window=x) * (255**0.5)      print(df_filtered.vol.iloc[-1]) 

as example, input of vol('pcg',6) output 0.187855386042

probably not elegant , apologies if i've misunderstood request.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -