s-ttp commited on
Commit
178588e
·
verified ·
1 Parent(s): 064858d

Upload engine/predict_yearly.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. engine/predict_yearly.py +45 -0
engine/predict_yearly.py CHANGED
@@ -237,6 +237,51 @@ def fetch_all_data_yearly(ticker, years=10, no_cache=False):
237
  """Fetch all data for yearly prediction. 10yr price history + 2Y Treasury."""
238
  data = {}
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  # 1. Daily price history (10+ years)
241
  print(f" [1/17] Daily price history (10yr)...", end=" ", flush=True)
242
  cache_file = CACHE_DIR / f"{ticker}_daily.csv"
 
237
  """Fetch all data for yearly prediction. 10yr price history + 2Y Treasury."""
238
  data = {}
239
 
240
+ # ── Concurrent cache pre-warm ──────────────────────────────────────────────
241
+ # The 20+ Alpha Vantage endpoints below are independent; fetched one-by-one they
242
+ # serialize network latency (~30s cold). Warm them concurrently first (bounded
243
+ # pool + the locked throttle keeps us under AV's 5/s burst & 75/min caps); the
244
+ # sequential blocks then read straight from the just-written cache. Skipped when
245
+ # no_cache — that path bypasses the cache, so a prewarm would double every call.
246
+ # Specs MUST mirror the (params, cache_key, cache_hours) used by the blocks below.
247
+ if not no_cache:
248
+ _prewarm = [
249
+ ({"function": "OVERVIEW", "symbol": ticker}, f"{ticker}_overview", 168),
250
+ ({"function": "INCOME_STATEMENT", "symbol": ticker}, f"{ticker}_income", 168),
251
+ ({"function": "BALANCE_SHEET", "symbol": ticker}, f"{ticker}_balance", 168),
252
+ ({"function": "CASH_FLOW", "symbol": ticker}, f"{ticker}_cashflow", 168),
253
+ ({"function": "EARNINGS", "symbol": ticker}, f"{ticker}_earnings", 168),
254
+ ({"function": "EARNINGS_ESTIMATES", "symbol": ticker}, f"{ticker}_earnings_estimates", 24),
255
+ ({"function": "GLOBAL_QUOTE", "symbol": ticker}, f"{ticker}_gq", 1),
256
+ ({"function": "INSIDER_TRANSACTIONS", "symbol": ticker}, f"{ticker}_insider", 168),
257
+ ({"function": "REALTIME_OPTIONS", "symbol": ticker}, f"{ticker}_options", 4),
258
+ ({"function": "NEWS_SENTIMENT", "tickers": ticker, "limit": "50", "sort": "LATEST"}, f"{ticker}_sentiment", 2),
259
+ ({"function": "ANALYTICS_FIXED_WINDOW", "SYMBOLS": ticker, "RANGE": "1year",
260
+ "INTERVAL": "DAILY", "OHLCV": "close",
261
+ "CALCULATIONS": "MEAN,STDDEV,CUMULATIVE_RETURN"}, f"{ticker}_analytics_1y", 4),
262
+ ({"function": "TREASURY_YIELD", "interval": "daily", "maturity": "10year"}, "treasury_10y", 12),
263
+ ({"function": "FEDERAL_FUNDS_RATE", "interval": "daily"}, "fed_funds", 12),
264
+ ({"function": "CPI", "interval": "monthly"}, "cpi", 24),
265
+ ({"function": "UNEMPLOYMENT"}, "unemployment", 24),
266
+ ({"function": "TREASURY_YIELD", "interval": "daily", "maturity": "2year"}, "treasury_2y", 12),
267
+ ({"function": "REAL_GDP", "interval": "quarterly"}, "real_gdp", 168),
268
+ ({"function": "NONFARM_PAYROLL"}, "nonfarm_payroll", 48),
269
+ ({"function": "WTI", "interval": "monthly"}, "wti_monthly", 168),
270
+ ({"function": "NATURAL_GAS", "interval": "monthly"}, "natural_gas_monthly", 168),
271
+ ({"function": "COPPER", "interval": "monthly"}, "copper_monthly", 168),
272
+ ]
273
+ from concurrent.futures import ThreadPoolExecutor as _TPE
274
+
275
+ def _warm(spec):
276
+ try:
277
+ api_call(spec[0], spec[1], spec[2], no_cache=False)
278
+ except Exception:
279
+ pass
280
+
281
+ print(f" {DIM}Pre-warming {len(_prewarm)} data endpoints (concurrent)…{RESET}", flush=True)
282
+ with _TPE(max_workers=6) as _ex:
283
+ list(_ex.map(_warm, _prewarm))
284
+
285
  # 1. Daily price history (10+ years)
286
  print(f" [1/17] Daily price history (10yr)...", end=" ", flush=True)
287
  cache_file = CACHE_DIR / f"{ticker}_daily.csv"