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

57 lines
1.6 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
# 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')