I have simplified the program for better understanding.
Step 1- Copy past the below code and run the program. This will download all the stock prices in your local machine with "xlsx" extension.
import datetime as dt
import pandas as pd
import pandas_datareader.data as web
import numpy as np
def stockdata(ticker):
start= dt.datetime(2000,1,1) ## Start Date Range
end=dt.datetime.now() ## Curret date as end date Range
Stock=web.DataReader(ticker, "yahoo", start, end)
name=str(ticker) + ".xlsx"
Stock.to_excel(name)
return ()
ticker= ["MU", "ASML","TSM","QCOM", "UMC", "^SOX", "INTC","^IXIC"]
for i in ticker:
stockdata(i)
Step 2- Consolidate the adj closed price column of each company from the individual excel file in one common excel file "StockData.xlsx" as shown in the article screenshot.
Step3:- Save a below code in another file and run it. This code will do the analysis.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.stats.outliers_influence import variance_inflation_factor
data=pd.read_excel("StockData.xlsx")
data.set_index("Date", inplace= True)
sns.heatmap(data.corr(), annot=True, cmap="YlGnBu")
plt.show()
X=data.drop(["ASML"], axis=1)
vif = pd.DataFrame()
vif["features"] = X.columns
vif["vif_Factor"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
print(vif)
Save the StockData.xlsx and the second program ( step3) in the same file directory.
In case, any line of the code is not clear then do not hesitate to write back to me and I will be happy to explain.