Last active
January 24, 2024 00:01
-
-
Save shashankvemuri/a210f7399b56075cbe55c309b460fa9b to your computer and use it in GitHub Desktop.
all the code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
I use Spyder IDE and not Jupyter notebook so I am not familiar with that specific issue. Please let me know if I could do anything else to help!
Thank you so much! It is a very useful code. I just change to tickers = si.tickers_sp500() + corr_matrix.to_excel and got the whole picture of correlations s&p 500. Best, keep me posted if you got something new.
No problem!! You can check out my entire GitHub Python Finance Code Repository at https://github.com/shashankvemuri/Finance
latest pip version breaks the code.
Solution is found here:
ranaroussi/yfinance#937
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
excuseme sir
running this code in Jupiter I receive this output
[100%**] 1 of 1 completed
Out[7]:
<streamlit.delta_generator.DeltaGenerator at 0x7faa06ba2850>
Why I can't see the app?
Ty Ale