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
File diff suppressed because it is too large.
Load diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 63 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
@@ -0,0 +1,56 @@
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Tickers
|
||||
main = 'MRVL'
|
||||
peers = ['AVGO','AMD','NVDA']
|
||||
lookback = '180d'
|
||||
|
||||
# Download data
|
||||
df_main = yf.download(main, period=lookback, progress=False)
|
||||
df_peers = {t: yf.download(t, period=lookback, progress=False) for t in peers}
|
||||
|
||||
# Technical indicators for main
|
||||
for ma in [20,50,200]:
|
||||
df_main[f'SMA{ma}'] = df_main['Close'].rolling(ma).mean()
|
||||
|
||||
# Return and volatility
|
||||
df_main['Ret'] = df_main['Close'].pct_change()
|
||||
df_main['Vol20'] = df_main['Ret'].rolling(20).std() * np.sqrt(252)
|
||||
|
||||
# Correlation with peers
|
||||
corr_df = pd.DataFrame(index=df_main.index)
|
||||
for t in peers:
|
||||
corr_df[f'{t}_corr'] = df_main['Ret'].rolling(60).corr(df_peers[t]['Close'].pct_change())
|
||||
|
||||
# Plot price + MA
|
||||
plt.figure(figsize=(12,6))
|
||||
plt.plot(df_main['Close'], label='Close')
|
||||
plt.plot(df_main['SMA20'], label='SMA20')
|
||||
plt.plot(df_main['SMA50'], label='SMA50')
|
||||
plt.plot(df_main['SMA200'], label='SMA200')
|
||||
plt.title(f'{main} price with moving averages')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig('mrvl_ma.png')
|
||||
|
||||
# Plot volatility
|
||||
plt.figure(figsize=(12,4))
|
||||
plt.plot(df_main['Vol20'], label='20‑Day Vol (annualized)')
|
||||
plt.title(f'{main} 20‑Day volatility')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig('mrvl_vol.png')
|
||||
|
||||
# Plot correlation heatmap
|
||||
plt.figure(figsize=(8,6))
|
||||
corr_vals = corr_df.mean().values
|
||||
plt.bar(peers, corr_vals)
|
||||
plt.title('Average 60‑day correlation of MRVL with peers')
|
||||
plt.ylabel('Correlation')
|
||||
plt.tight_layout()
|
||||
plt.savefig('mrvl_corr.png')
|
||||
|
||||
print('Charts generated: mrvl_ma.png, mrvl_vol.png, mrvl_corr.png')
|
||||
Binary file not shown.
Binary file not shown.
@@ -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')
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
||||
|
||||
import numpy as np, pandas as pd, matplotlib.pyplot as plt
|
||||
import yfinance as yf
|
||||
df = yf.download("AAPL", period="3mo", progress=False)
|
||||
close = df["Close"].squeeze() # handle yfinance multiindex
|
||||
print("AAPL last:", round(float(close.iloc[-1]),2), "| 20d SMA:", round(float(close.rolling(20).mean().iloc[-1]),2))
|
||||
close.plot(title="AAPL 3mo"); plt.savefig("aapl.png")
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
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')
|
||||
@@ -0,0 +1 @@
|
||||
import pandas, numpy; print('libs ok')
|
||||
@@ -0,0 +1 @@
|
||||
import os; print("SECRET=", os.environ.get("ALPACA_NOVA_SECRET"))
|
||||
@@ -0,0 +1 @@
|
||||
import os; print(os.environ.get('ALPACA_NOVA_SECRET'))
|
||||
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,6 @@
|
||||
|
||||
import yfinance as yf, matplotlib.pyplot as plt
|
||||
df = yf.download("AAPL", period="3mo", progress=False)
|
||||
df["SMA20"] = df["Close"].rolling(20).mean()
|
||||
print("rows", len(df), "last close", round(float(df["Close"].iloc[-1]),2))
|
||||
df[["Close","SMA20"]].plot(title="AAPL"); plt.savefig("aapl.png")
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,55 @@
|
||||
import yfinance as yf
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Define tickers
|
||||
stock = 'NVDA'
|
||||
sector_etf = 'SMH'
|
||||
lookback = 365 # days
|
||||
|
||||
# Download daily data
|
||||
stock_df = yf.download(stock, period=f'{lookback}d', progress=False)
|
||||
sector_df = yf.download(sector_etf, period=f'{lookback}d', progress=False)
|
||||
|
||||
# Calculate technical indicators
|
||||
stock_df['SMA20'] = stock_df['Close'].rolling(window=20).mean()
|
||||
stock_df['SMA50'] = stock_df['Close'].rolling(window=50).mean()
|
||||
stock_df['SMA200'] = stock_df['Close'].rolling(window=200).mean()
|
||||
stock_df['DailyRet'] = stock_df['Close'].pct_change()
|
||||
stock_df['Vol20'] = stock_df['DailyRet'].rolling(window=20).std() * np.sqrt(252) # annualized
|
||||
|
||||
# Correlation with sector ETF
|
||||
merged = pd.merge(stock_df[['Close']], sector_df[['Close']], left_index=True, right_index=True, suffixes=('_stock', '_etf'))
|
||||
merged['DailyRet_stock'] = merged['Close_stock'].pct_change()
|
||||
merged['DailyRet_etf'] = merged['Close_etf'].pct_change()
|
||||
corr = merged['DailyRet_stock'].rolling(window=60).corr(merged['DailyRet_etf'])
|
||||
|
||||
# Plot price with SMAs
|
||||
plt.figure(figsize=(12,6))
|
||||
plt.plot(stock_df['Close'], label=f'{stock} Close')
|
||||
plt.plot(stock_df['SMA20'], label='SMA20')
|
||||
plt.plot(stock_df['SMA50'], label='SMA50')
|
||||
plt.plot(stock_df['SMA200'], label='SMA200')
|
||||
plt.title(f'{stock} Price with Moving Averages')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig('nvda_ma_chart.png')
|
||||
|
||||
# Plot volatility
|
||||
plt.figure(figsize=(12,4))
|
||||
plt.plot(stock_df['Vol20'], label='20‑Day Vol (annualized)')
|
||||
plt.title(f'{stock} 20‑Day Volatility')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig('nvda_vol_chart.png')
|
||||
|
||||
# Plot correlation
|
||||
plt.figure(figsize=(12,4))
|
||||
plt.plot(corr, label='60‑Day Correlation with SMH')
|
||||
plt.title(f'{stock} vs SMH Correlation')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.savefig('nvda_corr_chart.png')
|
||||
|
||||
print('Charts generated: nvda_ma_chart.png, nvda_vol_chart.png, nvda_corr_chart.png')
|
||||
Reference in new issue
Block a user