Ready for first real test
This commit is contained in:
1 parent
acddf9135f
commit
b91c81ebdf
46 files changed
+34903
-26
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
stock='NVDA'
|
||||
sector='SMH'
|
||||
lookback='365d'
|
||||
|
||||
# Download
|
||||
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)
|
||||
|
||||
# correlation
|
||||
merged=pd.merge(s[['Close']], se[['Close']], left_index=True, right_index=True, suffixes=('','_etf'))
|
||||
merged['Ret']=merged['Close'].pct_change()
|
||||
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')
|
||||
Reference in new issue
Block a user