Python -Code für Yahoo -Finanzen, die keine Daten abrufenPython

Python-Programme
Anonymous
 Python -Code für Yahoo -Finanzen, die keine Daten abrufen

Post by Anonymous »

Mein Code: < /p>

Code: Select all

# Stock data Yahoo Finance

import pandas as pd
from datetime import datetime, timezone
import json
import yfinance as yf

# ---------------- Settings ----------------
# Match your original ticker set
tickers =     ["MSFT","AAPL","NVDA","GOOG","AMZN","META","TSLA","CRM","AMD","INTC","BABA","PYPL","CBOE","BLK"]

# Keep the same ms-epoch inputs you were using
start_date = int(datetime(2025, 6, 20, tzinfo=timezone.utc).timestamp() * 1000)
end_date   = int(datetime(2025, 8, 18, tzinfo=timezone.utc).timestamp() * 1000)

# Interval options:     "1m","2m","5m","15m","30m","60m","90m","1h","1d","5d","1wk","1mo","3mo"
interval = "30m"

# ---------------- Fetch ----------------
stock_data_dict = {}

start_ts = pd.to_datetime(start_date, unit="ms", utc=True)
end_ts   = pd.to_datetime(end_date,   unit="ms", utc=True)

print("Fetching Yahoo Finance candles...\n")

for t in tickers:
print(f"--> {t}: {interval} {start_ts.date()} to {end_ts.date()}")
try:
# Download OHLCV
df = yf.download(
t,
start=start_ts.tz_convert(None),
end=end_ts.tz_convert(None),
interval=interval,
auto_adjust=False,
progress=False,
group_by="ticker",
threads=True,
)

if df is None or df.empty:
print(f"    (no data returned)")
stock_data_dict[t] = {"candles": []}
continue

# Ensure the index is datetime and build epoch-ms column from it
idx = pd.to_datetime(df.index, utc=True)
df["datetime"] = (idx.astype("int64") // 10**6)   # ns → ms

# Normalize column names
rename_map = {
"Open": "open", "High": "high", "Low": "low", "Close": "close",
"Adj Close": "adjclose", "Volume": "volume"
}
df = df.rename(columns=rename_map)

# Some intraday frames don’t have Adj Close; use Close if missing
if "adjclose" not in df.columns and "close" in df.columns:
df["adjclose"] = df["close"]

# Some symbols/intervals may lack volume; create if missing
if "volume" not in df.columns:
df["volume"] = pd.NA

keep_cols = ["datetime", "open", "high", "low", "close", "adjclose", "volume"]
records = df.reindex(columns=keep_cols).dropna(subset=["datetime"]).to_dict("records")

stock_data_dict[t] = {"candles": records}
print(f"    fetched {len(records)} rows")

except Exception as e:
print(f"    Error fetching data for {t}: {e}")

# ---------------- Quick summary ----------------
print("\nSummary:")
for t, payload in stock_data_dict.items():
n = len(payload.get("candles", []))
print(f"{t}: {n} rows")

# ---------------- Persist: JSON + CSV ----------------
# Write JSON (similar to your 'AAAA_stock_data_NEW.json')
with open("yahoo_stock_data.json", "w") as f:
json.dump(stock_data_dict, f, indent=4)

# Flatten to one CSV like your pipeline expects
all_rows = []
for t, payload in stock_data_dict.items():
rows = payload.get("candles", [])
for r in rows:
r2 = dict(r)
r2["ticker"] = t
all_rows.append(r2)

all_df = pd.DataFrame(all_rows, columns=["ticker","datetime","open","high","low","close","adjclose","volume"])
# Convert ms → readable timestamp column (optional)
all_df["date"] = pd.to_datetime(all_df["datetime"], unit="ms", utc=True).dt.tz_convert("UTC")
all_df.to_csv("yahoo_stock_data_all.csv", index=False)

print("\nSaved:")
print(" - yahoo_stock_data.json")
print(" - yahoo_stock_data_all.csv")
< /code>
gibt leere Zeilen aus: < /p>
Yahoo Finance Candles ...--> MSFT: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
-->  AAPL: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> NVDA: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> GOOG: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> AMZN: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> META: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> TSLA: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> CRM: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> AMD: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> INTC: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> BABA: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> PYPL: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> CBOE: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
--> BLK: 30m 2025-06-20 to 2025-08-18
fetched 0 rows
Kann jemand herausfinden, warum?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post