Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import yfinance as yf
- import pandas as pd
- import numpy as np
- import statistics
- # Benchmark data
- benchmark = "SPY"
- bm_data = yf.download(tickers=benchmark, period="61mo", interval="1mo", prepost=True, progress=False) # bm = benchmark
- bm_data = pd.DataFrame.dropna(bm_data) # Removes rows with NaN, dividend stocks do this for some reason
- bm_close = bm_data['Adj Close']
- # Ticker data
- ticker = "QQQ"
- ticker_data = yf.download(tickers=ticker, period="61mo", interval="1mo", prepost=True, progress=False)
- ticker_data = pd.DataFrame.dropna(ticker_data)
- ticker_close = ticker_data['Adj Close']
- # Correlation coefficient calculation
- bm_std = np.std(bm_close)
- ticker_std = np.std(ticker_close)
- cor_cov = ticker_close.cov(bm_close)
- correlation = cor_cov/(bm_std*ticker_std)
- correlation_rnd = round(correlation, 2)
- print(f"Correlation coefficient: {correlation_rnd}")
- # Beta calculation
- bm_returns = (bm_close.shift(1) / bm_close)
- ticker_returns = (ticker_close.shift(1) / ticker_close)
- beta_cov = ticker_returns.cov(bm_returns)
- beta_var = bm_returns.var()
- beta = beta_cov / beta_var
- beta_rnd = round(beta, 2)
- print(f"Calculated beta: {beta_rnd}")
- # Volume data
- # Benchmark data
- bm_vol_data = yf.download(tickers=benchmark, period="1mo", interval="1d", prepost=True, progress=False) # bm = benchmark
- bm_vol_data = pd.DataFrame.dropna(bm_vol_data)
- bm_vol = bm_vol_data['Volume']
- bm_avg_daily_vol = statistics.mean(bm_vol)
- # Ticker data
- ticker_vol_data = yf.download(tickers=ticker, period="1mo", interval="1d", prepost=True, progress=False) # bm = benchmark
- ticker_vol_data = pd.DataFrame.dropna(ticker_vol_data)
- ticker_vol = ticker_vol_data['Volume']
- ticker_avg_daily_vol = statistics.mean(ticker_vol)
- volume_coefficient = ticker_avg_daily_vol/bm_avg_daily_vol
- volume_coefficient_rnd = round(volume_coefficient, 2)
- print(f"Volume coefficient: {volume_coefficient_rnd}")
- # Novel-alpha prerequisites
- # benchmark percentage change
- bm_df = bm_close.reset_index() # Sets date as a column, not index, so it can be removed
- bm_hist = pd.DataFrame.from_dict(bm_df)
- bm_hist.drop("Date", axis=1, inplace=True) # deleting date column from dataframe
- bm_price_1 = bm_hist.iloc[0, 0] # retrieves first row
- bm_price_2 = bm_hist.iloc[-1, 0] # retrieves last row
- bm_change_percent = ((bm_price_2 - bm_price_1)/bm_price_1)*100
- # ticker percentage change
- ticker_df = ticker_close.reset_index()
- ticker_hist = pd.DataFrame.from_dict(ticker_df)
- ticker_hist.drop("Date", axis=1, inplace=True)
- ticker_price_1 = ticker_hist.iloc[0, 0]
- ticker_price_2 = ticker_hist.iloc[-1, 0]
- ticker_change_percent = ((ticker_price_2 - ticker_price_1)/ticker_price_1)*100
- # Novel-alpha calculation
- bm_change_multiplier = (bm_change_percent + 100)/100
- ticker_change_multiplier = (ticker_change_percent + 100)/100
- novel_alpha = ticker_change_multiplier/bm_change_multiplier
- novel_alpha_rnd = round(novel_alpha, 2)
- print(f"Novel-alpha: {novel_alpha_rnd}")
- # Novel-gamma
- # bm gradient
- bm_y = np.array(bm_close)
- bm_x = np.array(bm_df['Date'])
- bm_x = (len(bm_x)) # converting date(time) into integer array so it can be read by pyplot
- bm_xvalues = []
- i = 0
- while i < bm_x:
- i += 1
- bm_xvalues.append(i) # this loop converts the absolute value of len() into a list of length "len()"
- bm_x = np.array(bm_xvalues) # converts the list into an array for pyplot
- bm_ylength = (len(bm_y))
- bm_yvalues_pc = []
- t = 0
- while t < bm_ylength:
- bm_close_addition = bm_hist.iloc[0+t, 0]
- bm_close_addition_pc = ((bm_close_addition - bm_price_1)/bm_price_1)*100
- bm_yvalues_pc.append(bm_close_addition_pc)
- t += 1 # this loop converts magnitude of price change to percentage change so that the gradient comparison is fair
- m, b = np.polyfit(bm_x, bm_yvalues_pc, 1) # Linear regression
- bm_gradient = m
- # ticker gradient
- ticker_y = np.array(ticker_close)
- ticker_x = np.array(ticker_df['Date'])
- ticker_x = (len(ticker_x)) # converting datetime into integer array so it can be read by pyplot
- ticker_xvalues = []
- i = 0
- while i < ticker_x:
- i += 1
- ticker_xvalues.append(i)
- ticker_x = np.array(ticker_xvalues)
- ticker_ylength = (len(ticker_y))
- ticker_yvalues_pc = []
- t = 0
- while t < ticker_ylength:
- ticker_close_addition = ticker_hist.iloc[0+t, 0]
- ticker_close_addition_pc = ((ticker_close_addition - ticker_price_1)/ticker_price_1)*100
- ticker_yvalues_pc.append(ticker_close_addition_pc)
- t += 1 # this loop converts magnitude of price change to percentage change so that the gradient comparison is fair
- m, b = np.polyfit(ticker_x, ticker_yvalues_pc, 1) # Linear regression
- ticker_gradient = m
- # Novel-gamma calculation
- novel_gamma = (ticker_gradient - bm_gradient) + 1
- novel_gamma_rnd = round(novel_gamma, 2)
- print(f"Novel-gamma: {novel_gamma_rnd}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement