-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_stock_data.py
More file actions
45 lines (34 loc) · 1.65 KB
/
Copy pathfetch_stock_data.py
File metadata and controls
45 lines (34 loc) · 1.65 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
import yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
# The 5 tickers the dashboard covers. Add/remove here only - nothing else
# downstream (Silver, Gold, the ML model) needs to change, since everything
# is keyed off the SERIES_COLNAME (asset_symbol), not a hardcoded list.
TICKERS = ["AAPL", "MSFT", "GOOGL", "AMZN", "NVDA"]
def download_stock_data():
end_date = datetime(2026, 8, 14)
start_date = end_date - timedelta(days=365)
print(f"Downloading {len(TICKERS)} tickers from {start_date:%Y-%m-%d} to {end_date:%Y-%m-%d}...")
frames = []
for ticker in TICKERS:
df = yf.download(ticker, start=start_date.strftime("%Y-%m-%d"), end=end_date.strftime("%Y-%m-%d"))
if df.empty:
print(f" {ticker}: no data returned, skipping")
continue
df = df.reset_index()
df["ticker"] = ticker
df = df[["ticker", "Date", "Open", "High", "Low", "Close", "Volume"]]
df.columns = ["ticker", "trade_date", "open_price", "high_price", "low_price", "close_price", "volume"]
frames.append(df)
print(f" {ticker}: {len(df)} rows")
if not frames:
print("No data retrieved. Verify network connectivity.")
return
combined = pd.concat(frames, ignore_index=True)
price_columns = ["open_price", "high_price", "low_price", "close_price"]
combined[price_columns] = combined[price_columns].round(4)
output_filename = "equities_prices.csv"
combined.to_csv(output_filename, index=False)
print(f"Success! {len(combined)} rows across {len(frames)} tickers saved to {output_filename}")
if __name__ == "__main__":
download_stock_data()