-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
32 lines (22 loc) · 918 Bytes
/
Copy pathfetch_data.py
File metadata and controls
32 lines (22 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Pull real daily prices and cache them to data/prices.csv.
Run this once to refresh the dataset. demo.py reads the cached CSV so it stays
offline and reproducible for everyone else.
python fetch_data.py
"""
from __future__ import annotations
from pathlib import Path
import yfinance as yf
# 8 liquid, recognizable names across sectors (tech / finance / energy /
# healthcare / consumer) so the covariance has some real structure.
TICKERS = ["AAPL", "MSFT", "NVDA", "AMZN", "JPM", "XOM", "JNJ", "KO"]
PERIOD = "2y"
OUT = Path(__file__).parent / "data" / "prices.csv"
def main():
df = yf.download(TICKERS, period=PERIOD, auto_adjust=True, progress=False)["Close"]
df = df[TICKERS].dropna()
OUT.parent.mkdir(exist_ok=True)
df.to_csv(OUT)
print(f"wrote {OUT}: {len(df)} days x {len(df.columns)} tickers")
print(df.tail(3).round(2).to_string())
if __name__ == "__main__":
main()