Files
trading-agent/state/sandbox/tmp7bajsrto/script.py
T

49 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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')