import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt stock='NVDA' sector='SMH' lookback='365d' s= yf.download(stock, period=lookback, progress=False) se= yf.download(sector, period=lookback, progress=False) s['SMA20']=s['Close'].rolling(20).mean() s['SMA50']=s['Close'].rolling(50).mean() s['SMA200']=s['Close'].rolling(200).mean() s['Ret']=s['Close'].pct_change() s['Vol20']=s['Ret'].rolling(20).std()*np.sqrt(252) merged=pd.merge(s[['Close','Ret']], se[['Close']], left_index=True, right_index=True, suffixes=('','_etf')) merged['Ret_etf']=merged['Close_etf'].pct_change() merged['Corr']=merged['Ret'].rolling(60).corr(merged['Ret_etf']) # plots plt.figure(figsize=(12,6)) plt.plot(s['Close'],label='Close') plt.plot(s['SMA20'],label='SMA20') plt.plot(s['SMA50'],label='SMA50') plt.plot(s['SMA200'],label='SMA200') plt.legend() plt.title('NVDA price & moving averages') plt.savefig('nvda_ma.png') plt.figure(figsize=(12,4)) plt.plot(s['Vol20'],label='20‑Day Vol (annualized)') plt.legend() plt.title('NVDA volatility') plt.savefig('nvda_vol.png') plt.figure(figsize=(12,4)) plt.plot(merged['Corr'],label='60‑Day Correlation with SMH') plt.legend() plt.title('NVDA vs SMH correlation') plt.savefig('nvda_corr.png') print('Done')