30 lines
661 B
Python
30 lines
661 B
Python
# engine/history.py
|
|
import yfinance as yf
|
|
import pandas as pd
|
|
from pathlib import Path
|
|
|
|
CACHE_DIR = Path("storage/history")
|
|
CACHE_DIR.mkdir(exist_ok=True)
|
|
|
|
def load_monthly_close(ticker, years=10):
|
|
file = CACHE_DIR / f"{ticker}.csv"
|
|
|
|
if file.exists():
|
|
df = pd.read_csv(file, parse_dates=["Date"], index_col="Date")
|
|
return df["Close"]
|
|
|
|
df = yf.download(
|
|
ticker,
|
|
period=f"{years}y",
|
|
auto_adjust=True,
|
|
progress=False
|
|
)
|
|
|
|
if df.empty:
|
|
raise RuntimeError(f"No history for {ticker}")
|
|
|
|
series = df["Close"].resample("M").last()
|
|
series.to_csv(file, header=["Close"])
|
|
|
|
return series
|