DevKX commited on
Commit
3d83caa
·
verified ·
1 Parent(s): 6ccd6c8

Upload data_fetcher.py

Browse files
Files changed (1) hide show
  1. src/data_fetcher.py +48 -25
src/data_fetcher.py CHANGED
@@ -24,40 +24,63 @@ class DataFetcher:
24
 
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.
 
24
 
25
  def fetch_market_data(self, days=50):
26
  """
27
+ Fetches market data using Finnhub (SPY as proxy) with a CSV fallback.
 
28
  """
29
+ print(f"📡 Attempting to fetch last {days} days from Finnhub (using SPY proxy)...")
30
 
31
  try:
32
+ # 1. Setup Timestamps (Finnhub needs Unix seconds)
33
+ end_ts = int(time.time())
34
+ start_ts = int((datetime.now() - timedelta(days=days+10)).timestamp())
35
+
36
+ # 2. Fetch SPY (S&P 500 Proxy)
37
+ # '1' means daily candles
38
+ res = self.finnhub_client.stock_candles('SPY', 'D', start_ts, end_ts)
39
+
40
+ if res.get('s') != 'ok':
41
+ raise ValueError(f"Finnhub API returned status: {res.get('s')}")
42
+
43
+ # Convert Finnhub response to DataFrame
44
+ df = pd.DataFrame({
45
+ 'Date': pd.to_datetime(res['t'], unit='s'),
46
+ 'Close': res['c'],
47
+ 'Open': res['o'],
48
+ 'High': res['h'],
49
+ 'Low': res['l'],
50
+ 'Volume': res['v']
51
+ }).set_index('Date')
52
+
53
+ # 3. Handle VIX (Finnhub free tier often blocks ^VIX)
54
+ # We attempt it, but if it fails, we merge from our backup data
55
+ try:
56
+ vix_res = self.finnhub_client.stock_candles('VIX', 'D', start_ts, end_ts)
57
+ if vix_res.get('s') == 'ok':
58
+ df['VIX'] = vix_res['c']
59
+ else:
60
+ raise Exception("VIX not available on API")
61
+ except:
62
+ print("⚠️ VIX not available on Finnhub. Pulling VIX from backup...")
63
+ backup_df = pd.read_csv("data/market_data_backup.csv", index_col=0, parse_dates=True)
64
+ # Reindex backup to match the dates we just got from the API
65
+ df['VIX'] = backup_df['VIX'].reindex(df.index, method='ffill')
66
+
67
+ # Final cleanup
68
+ df = df.ffill().dropna()
69
 
 
70
  if df.empty:
71
+ raise ValueError("Resulting DataFrame is empty.")
72
+
73
  return df
74
+
75
  except Exception as e:
76
+ print(f"⚠️ Finnhub fetch failed ({e}). Loading full backup from data/ folder...")
 
 
 
77
  backup_path = "data/market_data_backup.csv"
 
78
 
79
+ if not os.path.exists(backup_path):
80
+ print(f"🚨 FATAL: {backup_path} not found!")
81
+ return pd.DataFrame() # This will trigger your safety check in Processor
82
+
83
+ df_backup = pd.read_csv(backup_path, index_col=0, parse_dates=True)
84
  return df_backup.tail(days)
85
 
86
  # 🛡️ STREAMLIT CACHE: Ignores '_self' so it doesn't try to hash the Finnhub client.