Advertisement
AlexanderJRL

Metrics for benchmark comparison

Jun 5th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 KB | None | 0 0
  1. import yfinance as yf
  2. import pandas as pd
  3. import numpy as np
  4. import statistics
  5.  
  6.  
  7. # Benchmark data
  8. benchmark = "SPY"
  9. bm_data = yf.download(tickers=benchmark, period="61mo", interval="1mo", prepost=True, progress=False)  # bm = benchmark
  10. bm_data = pd.DataFrame.dropna(bm_data)  # Removes rows with NaN, dividend stocks do this for some reason
  11. bm_close = bm_data['Adj Close']
  12.  
  13.  
  14. # Ticker data
  15. ticker = "QQQ"
  16. ticker_data = yf.download(tickers=ticker, period="61mo", interval="1mo", prepost=True, progress=False)
  17. ticker_data = pd.DataFrame.dropna(ticker_data)
  18. ticker_close = ticker_data['Adj Close']
  19.  
  20.  
  21.  
  22.  
  23. # Correlation coefficient calculation
  24. bm_std = np.std(bm_close)
  25. ticker_std = np.std(ticker_close)
  26. cor_cov = ticker_close.cov(bm_close)
  27. correlation = cor_cov/(bm_std*ticker_std)
  28. correlation_rnd = round(correlation, 2)
  29. print(f"Correlation coefficient: {correlation_rnd}")
  30.  
  31.  
  32.  
  33. # Beta calculation
  34. bm_returns = (bm_close.shift(1) / bm_close)
  35. ticker_returns = (ticker_close.shift(1) / ticker_close)
  36. beta_cov = ticker_returns.cov(bm_returns)
  37. beta_var = bm_returns.var()
  38. beta = beta_cov / beta_var
  39. beta_rnd = round(beta, 2)
  40. print(f"Calculated beta: {beta_rnd}")
  41.  
  42.  
  43.  
  44.  
  45. # Volume data
  46. # Benchmark data
  47. bm_vol_data = yf.download(tickers=benchmark, period="1mo", interval="1d", prepost=True, progress=False)  # bm = benchmark
  48. bm_vol_data = pd.DataFrame.dropna(bm_vol_data)
  49. bm_vol = bm_vol_data['Volume']
  50. bm_avg_daily_vol = statistics.mean(bm_vol)
  51. # Ticker data
  52. ticker_vol_data = yf.download(tickers=ticker, period="1mo", interval="1d", prepost=True, progress=False)  # bm = benchmark
  53. ticker_vol_data = pd.DataFrame.dropna(ticker_vol_data)
  54. ticker_vol = ticker_vol_data['Volume']
  55. ticker_avg_daily_vol = statistics.mean(ticker_vol)
  56.  
  57. volume_coefficient = ticker_avg_daily_vol/bm_avg_daily_vol
  58. volume_coefficient_rnd = round(volume_coefficient, 2)
  59.  
  60. print(f"Volume coefficient: {volume_coefficient_rnd}")
  61.  
  62.  
  63.  
  64.  
  65. # Novel-alpha prerequisites
  66. # benchmark percentage change
  67. bm_df = bm_close.reset_index()  # Sets date as a column, not index, so it can be removed
  68. bm_hist = pd.DataFrame.from_dict(bm_df)
  69. bm_hist.drop("Date", axis=1, inplace=True)  # deleting date column from dataframe
  70. bm_price_1 = bm_hist.iloc[0, 0]  # retrieves first row
  71. bm_price_2 = bm_hist.iloc[-1, 0]  # retrieves last row
  72. bm_change_percent = ((bm_price_2 - bm_price_1)/bm_price_1)*100
  73.  
  74. # ticker percentage change
  75. ticker_df = ticker_close.reset_index()
  76. ticker_hist = pd.DataFrame.from_dict(ticker_df)
  77. ticker_hist.drop("Date", axis=1, inplace=True)
  78. ticker_price_1 = ticker_hist.iloc[0, 0]
  79. ticker_price_2 = ticker_hist.iloc[-1, 0]
  80. ticker_change_percent = ((ticker_price_2 - ticker_price_1)/ticker_price_1)*100
  81.  
  82.  
  83. # Novel-alpha calculation
  84. bm_change_multiplier = (bm_change_percent + 100)/100
  85. ticker_change_multiplier = (ticker_change_percent + 100)/100
  86. novel_alpha = ticker_change_multiplier/bm_change_multiplier
  87. novel_alpha_rnd = round(novel_alpha, 2)
  88. print(f"Novel-alpha: {novel_alpha_rnd}")
  89.  
  90.  
  91.  
  92.  
  93. # Novel-gamma
  94. # bm gradient
  95. bm_y = np.array(bm_close)
  96. bm_x = np.array(bm_df['Date'])
  97. bm_x = (len(bm_x))  # converting date(time) into integer array so it can be read by pyplot
  98. bm_xvalues = []
  99. i = 0
  100. while i < bm_x:
  101.     i += 1
  102.     bm_xvalues.append(i)  # this loop converts the absolute value of len() into a list of length "len()"
  103. bm_x = np.array(bm_xvalues)  # converts the list into an array for pyplot
  104.  
  105. bm_ylength = (len(bm_y))
  106. bm_yvalues_pc = []
  107. t = 0
  108. while t < bm_ylength:
  109.     bm_close_addition = bm_hist.iloc[0+t, 0]
  110.     bm_close_addition_pc = ((bm_close_addition - bm_price_1)/bm_price_1)*100
  111.     bm_yvalues_pc.append(bm_close_addition_pc)
  112.     t += 1  # this loop converts magnitude of price change to percentage change so that the gradient comparison is fair
  113.  
  114. m, b = np.polyfit(bm_x, bm_yvalues_pc, 1)  # Linear regression
  115. bm_gradient = m
  116.  
  117. # ticker gradient
  118. ticker_y = np.array(ticker_close)
  119. ticker_x = np.array(ticker_df['Date'])
  120. ticker_x = (len(ticker_x))  # converting datetime into integer array so it can be read by pyplot
  121. ticker_xvalues = []
  122. i = 0
  123. while i < ticker_x:
  124.     i += 1
  125.     ticker_xvalues.append(i)
  126. ticker_x = np.array(ticker_xvalues)
  127.  
  128. ticker_ylength = (len(ticker_y))
  129. ticker_yvalues_pc = []
  130. t = 0
  131. while t < ticker_ylength:
  132.     ticker_close_addition = ticker_hist.iloc[0+t, 0]
  133.     ticker_close_addition_pc = ((ticker_close_addition - ticker_price_1)/ticker_price_1)*100
  134.     ticker_yvalues_pc.append(ticker_close_addition_pc)
  135.     t += 1  # this loop converts magnitude of price change to percentage change so that the gradient comparison is fair
  136.  
  137. m, b = np.polyfit(ticker_x, ticker_yvalues_pc, 1)  # Linear regression
  138. ticker_gradient = m
  139.  
  140. # Novel-gamma calculation
  141. novel_gamma = (ticker_gradient - bm_gradient) + 1
  142. novel_gamma_rnd = round(novel_gamma, 2)
  143. print(f"Novel-gamma: {novel_gamma_rnd}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement