Skip to content

Instantly share code, notes, and snippets.

@shashankvemuri
Last active January 24, 2024 00:01
Show Gist options
  • Save shashankvemuri/a210f7399b56075cbe55c309b460fa9b to your computer and use it in GitHub Desktop.
Save shashankvemuri/a210f7399b56075cbe55c309b460fa9b to your computer and use it in GitHub Desktop.
all the code
import numpy as np
import warnings
from pandas_datareader import data as pdr
import yfinance as yf
import datetime as dt
from yahoo_fin import stock_info as si
import pandas as pd
pd.set_option('display.max_rows', None)
warnings.filterwarnings("ignore")
yf.pdr_override()
num_of_years = 1
start = dt.date.today() - dt.timedelta(days = int(365.25*num_of_years))
end = dt.date.today()
tickers = si.tickers_dow()
dataset = pdr.get_data_yahoo(tickers, start, end)['Adj Close']
stocks_returns = np.log(dataset/dataset.shift(1))
print('\nCorrelation Matrix')
corr_matrix = stocks_returns.corr()
print (corr_matrix)
def get_redundant_pairs(df):
pairs_to_drop = set()
cols = df.columns
for i in range(0, df.shape[1]):
for j in range(0, i+1):
pairs_to_drop.add((cols[i], cols[j]))
return pairs_to_drop
def get_top_abs_correlations(df):
au_corr = df.corr().abs().unstack()
labels_to_drop = get_redundant_pairs(df)
au_corr = au_corr.drop(labels=labels_to_drop).sort_values(ascending=False)
return au_corr
print("\nTop Absolute Correlations")
print(get_top_abs_correlations(stocks_returns))
@shashankvemuri
Copy link
Author

No problem!! You can check out my entire GitHub Python Finance Code Repository at https://github.com/shashankvemuri/Finance

@liav93eliyahu
Copy link

latest pip version breaks the code.
Solution is found here:
ranaroussi/yfinance#937

@anand1321
Copy link

can somone explain me how to get the tickers list for nifty50 or indian stocks. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment