Spaces:
Running
Running
Upload data_fetcher.py
Browse files- src/data_fetcher.py +30 -15
src/data_fetcher.py
CHANGED
|
@@ -25,25 +25,40 @@ class DataFetcher:
|
|
| 25 |
def fetch_market_data(self, days=50):
|
| 26 |
"""
|
| 27 |
Fetches raw OHLCV and VIX data from Yahoo Finance.
|
|
|
|
| 28 |
"""
|
| 29 |
-
print(f"📡
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# 🛡️ STREAMLIT CACHE: Ignores '_self' so it doesn't try to hash the Finnhub client.
|
| 49 |
# ttl=3600 caches the news for 1 hour so repeated button clicks load instantly.
|
|
|
|
| 25 |
def fetch_market_data(self, days=50):
|
| 26 |
"""
|
| 27 |
Fetches raw OHLCV and VIX data from Yahoo Finance.
|
| 28 |
+
Falls back to local CSV in the data/ folder if Yahoo blocks the server IP.
|
| 29 |
"""
|
| 30 |
+
print(f"📡 Attempting to fetch last {days} days of {self.ticker} and {self.vix_ticker}...")
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
+
# 1. TRY TO FETCH LIVE DATA
|
| 34 |
+
#df = yf.download(self.ticker, period=f"{days}d", interval="1d", progress=False)
|
| 35 |
+
#df_vix = yf.download(self.vix_ticker, period=f"{days}d", interval="1d", progress=False)
|
| 36 |
|
| 37 |
+
# Handle yfinance MultiIndex columns if they exist
|
| 38 |
+
if isinstance(df.columns, pd.MultiIndex):
|
| 39 |
+
df.columns = df.columns.get_level_values(0)
|
| 40 |
+
if isinstance(df_vix.columns, pd.MultiIndex):
|
| 41 |
+
df_vix.columns = df_vix.columns.get_level_values(0)
|
| 42 |
|
| 43 |
+
df['VIX'] = df_vix['Close']
|
| 44 |
+
df = df.ffill()
|
| 45 |
+
|
| 46 |
+
# If the dataframe is empty (Yahoo stealth-blocked us), force an error
|
| 47 |
+
if df.empty:
|
| 48 |
+
raise ValueError("Yahoo Finance returned empty data.")
|
| 49 |
+
|
| 50 |
+
return df
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
# 2. FALLBACK TO LOCAL CSV IF BLOCKED
|
| 54 |
+
print(f"⚠️ Live fetch failed ({e}). Loading backup data from data/ folder...")
|
| 55 |
+
|
| 56 |
+
# Load the CSV from your new data folder
|
| 57 |
+
backup_path = "data/market_data_backup.csv"
|
| 58 |
+
df_backup = pd.read_csv(backup_path, index_col=0, parse_dates=True)
|
| 59 |
+
|
| 60 |
+
# Return only the requested number of days
|
| 61 |
+
return df_backup.tail(days)
|
| 62 |
|
| 63 |
# 🛡️ STREAMLIT CACHE: Ignores '_self' so it doesn't try to hash the Finnhub client.
|
| 64 |
# ttl=3600 caches the news for 1 hour so repeated button clicks load instantly.
|