Coverage for scripts/fetch_cash_sweep_data.py: 100%

40 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Fetch the cash-sweep ETF's daily bars (V6.2.0; `phase_v2.cash_sweep.ticker`, BIL by default) into its Lean zip. 

2 

3The sweep ticker is not a universe asset (never scored, ranked or capped), so `scripts/build_holdout_data.py` does not 

4fetch it; this uses the same Yahoo pipeline (split/dividend-adjusted, so distributions accrue in the price, x10000 Lean 

5equity format, OHLC sanitised) and the same window end as every other zip. 

6 

7 python scripts/fetch_cash_sweep_data.py [--start 2013-01-01] [--end 2026-09-30] [--apply] 

8""" 

9 

10from __future__ import annotations 

11 

12import argparse 

13import itertools 

14import json 

15import sys 

16from datetime import date 

17from pathlib import Path 

18 

19ROOT_DIR = Path(__file__).resolve().parents[1] 

20sys.path.insert(0, str(ROOT_DIR)) 

21 

22 

23def main() -> int: 

24 from data_pipeline.yfinance_backfill import fetch_yahoo_ohlcv, sanitize_ohlc, scale_for_lean, write_lean_zip 

25 from portfolio.cash_sweep import resolve_cash_sweep_config 

26 

27 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 

28 parser.add_argument("--start", default="2013-01-01") 

29 parser.add_argument("--end", default="2026-09-30", help="last date kept (inclusive)") 

30 parser.add_argument("--apply", action="store_true") 

31 args = parser.parse_args() 

32 

33 config = json.loads((ROOT_DIR / "config.json").read_text(encoding="utf-8")) 

34 sweep = resolve_cash_sweep_config(config.get("phase_v2", {}).get("cash_sweep")) 

35 ticker = sweep["ticker"] 

36 last = date.fromisoformat(args.end) 

37 # yfinance's end is exclusive: ask for the next day and trim, so the zip ends where the universe zips end 

38 rows = fetch_yahoo_ohlcv(ticker, args.start, date.fromordinal(last.toordinal() + 1).isoformat()) 

39 rows = [row for row in rows if row["date"] <= last] 

40 rows, widened = sanitize_ohlc(rows) 

41 if not rows: 

42 print(f"{ticker}: Yahoo returned nothing") 

43 return 1 

44 closes = [float(row["close"]) for row in rows] 

45 worst = max(abs(after / before - 1.0) for before, after in itertools.pairwise(closes)) 

46 print(f"{ticker}: {len(rows)} rows {rows[0]['date']}..{rows[-1]['date']}, {widened} OHLC rows widened, " 

47 f"largest daily move {worst:.4%}") 

48 target = ROOT_DIR / sweep["data_path"] 

49 if not args.apply: 

50 print(f"dry run (pass --apply to write {target})") 

51 return 0 

52 target.parent.mkdir(parents=True, exist_ok=True) 

53 write_lean_zip(target, ticker, scale_for_lean(rows, "equity"), merge_with_existing=False) 

54 print(f"written {target}") 

55 return 0 

56 

57 

58if __name__ == "__main__": 

59 raise SystemExit(main())