Coverage for scripts/v660_venue_coverage.py: 100%

59 statements  

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

1"""Which public, no-account sources serve daily bars for the sleeve's 12 coins, and how far back (V6.6.0, Problems.md #192). 

2 

3IB's crypto contracts and TradeStation's crypto can't be read here: both need an account this project doesn't have (lean.json holds none for IB, Kraken, 

4Coinbase or Alpaca). This checks only what answers without one: Coinbase, Kraken and Binance public endpoints. Writes ml/evaluation/v660/venue_data_coverage.json. 

5 

6 python scripts/v660_venue_coverage.py 

7""" 

8 

9from __future__ import annotations 

10 

11import datetime 

12import json 

13import sys 

14import time 

15from pathlib import Path 

16 

17import requests 

18 

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

20OUT = ROOT / "ml" / "evaluation" / "v660" / "venue_data_coverage.json" 

21COINS = ("BTC", "ETH", "LTC", "XRP", "ADA", "BCH", "LINK", "DOGE", "XLM", "EOS", "ETC", "ZEC") 

22KRAKEN_NAME = {"BTC": "XBT"} 

23HEADERS = {"User-Agent": "AetherQuantResearch/6.6 (github.com/leon1706-lol/Aether-quant)"} 

24 

25 

26def get(url: str, **params): 

27 for attempt in range(3): 

28 try: 

29 response = requests.get(url, params=params, headers=HEADERS, timeout=30) 

30 except requests.RequestException: 

31 time.sleep(2.0) 

32 continue 

33 if response.status_code == 429: 

34 time.sleep(3.0 * (attempt + 1)) 

35 continue 

36 return response 

37 return None 

38 

39 

40def day(stamp) -> str: 

41 return datetime.datetime.fromtimestamp(int(stamp), datetime.timezone.utc).strftime("%Y-%m-%d") 

42 

43 

44def coinbase(coin: str) -> dict: 

45 response = get(f"https://api.exchange.coinbase.com/products/{coin}-USD/candles", granularity=86400) 

46 if response is None or response.status_code != 200: 

47 return {"status": None if response is None else response.status_code} 

48 rows = response.json() 

49 return {"status": 200, "bars": len(rows), "first": day(rows[-1][0]) if rows else None, "last": day(rows[0][0]) if rows else None, "per_call_limit": 300} 

50 

51 

52def kraken(coin: str) -> dict: 

53 pair = f"{KRAKEN_NAME.get(coin, coin)}USD" 

54 response = get("https://api.kraken.com/0/public/OHLC", pair=pair, interval=1440) 

55 if response is None or response.status_code != 200: 

56 return {"status": None if response is None else response.status_code} 

57 body = response.json() 

58 if body.get("error"): 

59 return {"status": 200, "error": body["error"][0]} 

60 rows = next(value for key, value in body["result"].items() if key != "last") 

61 return {"status": 200, "bars": len(rows), "first": day(rows[0][0]), "last": day(rows[-1][0]), "per_call_limit": 720} 

62 

63 

64def binance(coin: str) -> dict: 

65 listing = get("https://data-api.binance.vision/api/v3/klines", symbol=f"{coin}USDT", interval="1d", startTime=1502928000000, limit=1000) 

66 if listing is None or listing.status_code != 200: 

67 return {"status": None if listing is None else listing.status_code} 

68 rows = listing.json() 

69 return {"status": 200, "first": day(rows[0][0] / 1000) if rows else None, "per_call_limit": 1000, "archive": "data.binance.vision, monthly zips back to the listing"} 

70 

71 

72def main() -> int: 

73 report = {"checked": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"), "coins": {}} 

74 for coin in COINS: 

75 report["coins"][coin] = {"coinbase": coinbase(coin), "kraken": kraken(coin), "binance": binance(coin)} 

76 print(coin, {venue: (entry.get("first") or entry.get("error") or entry.get("status")) for venue, entry in report["coins"][coin].items()}, flush=True) 

77 time.sleep(0.5) 

78 report["note"] = ( 

79 "Coinbase serves 300 daily bars a call (paged by start/end), Kraken 720 of the newest, Binance's archive everything since listing. " 

80 "None needs an account. Not tested: IB (needs a paper account and a running Gateway), TradeStation (its API documents stocks, options and futures only)." 

81 ) 

82 OUT.write_text(json.dumps(report, indent=1), encoding="utf-8", newline="\n") 

83 return 0 

84 

85 

86if __name__ == "__main__": 

87 sys.exit(main())