BaoKhuong commited on
Commit
05412f4
·
verified ·
1 Parent(s): 9e991da

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -5
app.py CHANGED
@@ -142,6 +142,25 @@ def create_session() -> requests.Session:
142
 
143
  http = create_session()
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # -------- Data helpers (Finnhub with fallback to Alpha Vantage) --------
147
 
@@ -177,23 +196,26 @@ def fetch_finnhub_candles(symbol: str, resolution: str = "D", count: int = 60) -
177
  raise last_error or RuntimeError("Finnhub candles failed")
178
 
179
 
180
- def fetch_alpha_vantage_series_daily(symbol: str, outputsize: str = "compact") -> Dict:
181
  """Fallback: Alpha Vantage TIME_SERIES_DAILY via RapidAPI, format like Finnhub candles."""
182
  if not RAPIDAPI_KEYS:
183
- raise ValueError("Missing RAPIDAPI_KEYS/RAPIDAPI_KEY for Alpha Vantage fallback")
184
  import time as _time
185
  for api_key in RAPIDAPI_KEYS:
186
  try:
187
  url = f"https://{RAPIDAPI_HOST}/query"
188
- headers = {"x-rapidapi-key": api_key, "x-rapidapi-host": RAPIDAPI_HOST}
189
  params = {"function": "TIME_SERIES_DAILY", "symbol": symbol, "outputsize": outputsize}
190
  r = http.get(url, headers=headers, params=params, timeout=30)
191
  r.raise_for_status()
192
  data = r.json()
 
 
 
193
  series = data.get("Time Series (Daily)") or {}
194
  if not series:
195
  continue
196
- dates = sorted(series.keys())
197
  import time as tmod
198
  t, o, h, l, c, v = [], [], [], [], [], []
199
  for d in dates:
@@ -213,7 +235,8 @@ def fetch_alpha_vantage_series_daily(symbol: str, outputsize: str = "compact") -
213
  continue
214
  finally:
215
  _time.sleep(0.5)
216
- raise RuntimeError("Alpha Vantage fallback failed")
 
217
 
218
 
219
  def fetch_alpha_vantage_overview(symbol: str) -> Dict:
 
142
 
143
  http = create_session()
144
 
145
+ # -------- Helpers for mock candles --------
146
+
147
+ def _create_mock_candles(symbol: str, count: int = 60) -> Dict:
148
+ import time as tmod
149
+ import random
150
+ now = int(tmod.time())
151
+ day = 86400
152
+ t, o, h, l, c, v = [], [], [], [], [], []
153
+ price = 100.0 + (hash(symbol) % 50)
154
+ for i in range(count, 0, -1):
155
+ ts = now - i * day
156
+ op = max(1.0, price + random.uniform(-2, 2))
157
+ hi = op + random.uniform(0, 2)
158
+ lo = max(0.5, op - random.uniform(0, 2))
159
+ cl = max(0.5, lo + random.uniform(0, (hi - lo) or 1))
160
+ vol = abs(int(random.gauss(1_000_000, 250_000)))
161
+ t.append(ts); o.append(op); h.append(hi); l.append(lo); c.append(cl); v.append(vol)
162
+ price = cl
163
+ return {"s": "ok", "t": t, "o": o, "h": h, "l": l, "c": c, "v": v, "source": "mock"}
164
 
165
  # -------- Data helpers (Finnhub with fallback to Alpha Vantage) --------
166
 
 
196
  raise last_error or RuntimeError("Finnhub candles failed")
197
 
198
 
199
+ def fetch_alpha_vantage_series_daily(symbol: str, outputsize: str = "compact", count: int = 60) -> Dict:
200
  """Fallback: Alpha Vantage TIME_SERIES_DAILY via RapidAPI, format like Finnhub candles."""
201
  if not RAPIDAPI_KEYS:
202
+ return _create_mock_candles(symbol, count)
203
  import time as _time
204
  for api_key in RAPIDAPI_KEYS:
205
  try:
206
  url = f"https://{RAPIDAPI_HOST}/query"
207
+ headers = {"X-RapidAPI-Key": api_key, "X-RapidAPI-Host": RAPIDAPI_HOST}
208
  params = {"function": "TIME_SERIES_DAILY", "symbol": symbol, "outputsize": outputsize}
209
  r = http.get(url, headers=headers, params=params, timeout=30)
210
  r.raise_for_status()
211
  data = r.json()
212
+ if isinstance(data, dict) and any(k in data for k in ("Note", "Error Message", "Information")):
213
+ # rate limit or error; try next key
214
+ continue
215
  series = data.get("Time Series (Daily)") or {}
216
  if not series:
217
  continue
218
+ dates = sorted(series.keys())[-count:]
219
  import time as tmod
220
  t, o, h, l, c, v = [], [], [], [], [], []
221
  for d in dates:
 
235
  continue
236
  finally:
237
  _time.sleep(0.5)
238
+ # If all keys failed or no data, return mock to keep UI responsive
239
+ return _create_mock_candles(symbol, count)
240
 
241
 
242
  def fetch_alpha_vantage_overview(symbol: str) -> Dict: