-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmarket_data.py
More file actions
1596 lines (1357 loc) · 55.4 KB
/
market_data.py
File metadata and controls
1596 lines (1357 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import json
import os
import configparser
import re
import calendar
from datetime import date, datetime, timedelta, timezone
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import numpy as np
from logging_helpers import human_log
from market_data_sources import get_source_minutes_for_range
from PBCoinData import CoinData, compute_coin_name, get_symbol_for_coin
from pbgui_purefunc import load_symbols_from_ini
_DAY_HOUR_RE = re.compile(r"^(\d{8})-(\d{2})\.(lz4|jsonl|npz)$")
_DAY_RE = re.compile(r"^(\d{8})\.(lz4|jsonl|npz)$")
_DAY_DASH_RE = re.compile(r"^(\d{4})-(\d{2})-(\d{2})\.(lz4|jsonl|npz)$")
_HL_COIN_TO_CCXT_SYMBOL_CACHE: dict[str, str] | None = None
_HL_SYMBOL_TO_CCXT_SYMBOL_CACHE: dict[str, str] | None = None
def _normalize_day_str(day: str) -> str:
s = str(day or "").strip()
if re.fullmatch(r"\d{8}", s):
return s
m = re.fullmatch(r"(\d{4})-(\d{2})-(\d{2})", s)
if m:
return f"{m.group(1)}{m.group(2)}{m.group(3)}"
return ""
def _parse_day_hour_from_filename(name: str) -> tuple[str, str | None] | None:
s = str(name or "")
m = _DAY_HOUR_RE.match(s)
if m:
return (m.group(1), m.group(2))
m = _DAY_RE.match(s)
if m:
return (m.group(1), None)
m = _DAY_DASH_RE.match(s)
if m:
return (f"{m.group(1)}{m.group(2)}{m.group(3)}", None)
return None
def _hours_from_npz(path: Path) -> set[str]:
hours: set[str] = set()
try:
with np.load(path) as data:
arr = data["candles"] if "candles" in data else None
if arr is None or len(arr) == 0:
return hours
ts = arr["ts"].astype("int64", copy=False)
hour_vals = ((ts // 3_600_000) % 24).astype(int)
for h in np.unique(hour_vals):
hours.add(f"{int(h):02d}")
except Exception:
return hours
return hours
def get_market_data_root_dir() -> Path:
"""Root directory for PBGui-managed market data."""
root = (Path(__file__).resolve().parent / "data" / "ohlcv").resolve()
_ensure_exchange_aliases(root)
return root
# PB7 strips the "usdm"/"futures" suffix when looking up ohlcv_source_dir.
# Maintain symlinks so both names resolve to the same data.
_EXCHANGE_ALIASES: dict[str, str] = {
"binance": "binanceusdm",
}
_aliases_created: set[str] = set()
def _ensure_exchange_aliases(root: Path) -> None:
"""Create missing exchange-alias symlinks under *root* (e.g. binance -> binanceusdm).
PB7 normalises exchange names by stripping 'usdm'/'futures' suffixes, so it
looks for 'binance/' while PBGui stores data under 'binanceusdm/'. A symlink
lets both tools share the same files without moving data.
"""
for alias, target_name in _EXCHANGE_ALIASES.items():
if alias in _aliases_created:
continue
target = root / target_name
link = root / alias
if not target.exists():
continue
if link.exists() or link.is_symlink():
_aliases_created.add(alias)
continue
try:
link.symlink_to(target)
_aliases_created.add(alias)
except OSError:
pass
def get_exchange_raw_root_dir(exchange: str) -> Path:
exchange = str(exchange or "").strip().lower()
if not exchange:
raise ValueError("exchange is empty")
return get_market_data_root_dir() / exchange
def normalize_market_data_dataset(dataset: str) -> str:
ds = str(dataset or "").strip()
if ds.lower() == "candles_1m_api":
return "1m_api"
return ds
def _format_ccxt_symbol_dir(symbol: str) -> str:
s = str(symbol or "").strip().upper()
if not s:
return s
return s.replace("/", "_")
def _get_hyperliquid_ccxt_symbol_for_coin(coin: str) -> str:
global _HL_COIN_TO_CCXT_SYMBOL_CACHE
global _HL_SYMBOL_TO_CCXT_SYMBOL_CACHE
key = str(coin or "").strip().upper()
if not key:
return ""
try:
if _HL_COIN_TO_CCXT_SYMBOL_CACHE is None:
cache: dict[str, str] = {}
sym_cache: dict[str, str] = {}
mapping_path = Path(__file__).resolve().parent / "data" / "coindata" / "hyperliquid" / "mapping.json"
if mapping_path.exists():
rows = json.loads(mapping_path.read_text(encoding="utf-8"))
for rec in rows if isinstance(rows, list) else []:
c = str(rec.get("coin") or "").strip().upper()
s = str(rec.get("symbol") or "").strip().upper()
ccxt_symbol = str(rec.get("ccxt_symbol") or "").strip().upper()
if c and ccxt_symbol:
cache[c] = ccxt_symbol
if s and ccxt_symbol:
sym_cache[s] = ccxt_symbol
_HL_COIN_TO_CCXT_SYMBOL_CACHE = cache
_HL_SYMBOL_TO_CCXT_SYMBOL_CACHE = sym_cache
return str((_HL_COIN_TO_CCXT_SYMBOL_CACHE or {}).get(key) or "")
except Exception:
return ""
def _get_hyperliquid_ccxt_symbol_for_market_id(market_id: str) -> str:
key = str(market_id or "").strip().upper()
if not key:
return ""
try:
if _HL_SYMBOL_TO_CCXT_SYMBOL_CACHE is None:
_get_hyperliquid_ccxt_symbol_for_coin("BTC")
return str((_HL_SYMBOL_TO_CCXT_SYMBOL_CACHE or {}).get(key) or "")
except Exception:
return ""
def normalize_market_data_coin_dir(exchange: str, coin: str) -> str:
ex = str(exchange or "").strip().lower()
raw = str(coin or "").strip()
if not raw:
return ""
if ex != "hyperliquid":
return raw.upper()
raw_base = str(raw).split("/")[0].strip()
raw_base_l = raw_base.lower()
if raw_base_l.startswith("xyz:") or raw_base_l.startswith("xyz-"):
tail = raw_base[4:].strip()
tail_u = tail.upper()
for suffix in ("_USDC:USDC", "_USDT:USDT", "_USDC_USDC", "_USDT_USDT"):
if tail_u.endswith(suffix):
tail = tail[: -len(suffix)]
break
tail = str(tail).strip(" _:-")
return f"XYZ-{tail.upper()}_USDC:USDC" if tail else ""
sym = raw
if str(raw).strip().isdigit():
mid_symbol = _get_hyperliquid_ccxt_symbol_for_market_id(raw)
if mid_symbol:
sym = mid_symbol
# Only call get_symbol_for_coin if input is NOT already a symbol (doesn't end with USDC/USDT)
# This prevents kBONKUSDC from becoming kBONKUSDCUSDC
looks_like_symbol = any(sym.upper().endswith(q) for q in ("USDC", "USDT"))
if "/" not in sym and ":" not in sym and not ("_" in sym and ":" in sym) and not looks_like_symbol:
try:
sym = get_symbol_for_coin(sym.upper(), "hyperliquid.swap")
except Exception:
sym = raw
# Hyperliquid mapping stores `symbol` as numeric market id (e.g. BTC -> "0").
# If reverse lookup returned that id, resolve coin -> ccxt_symbol instead.
sym_u = str(sym).strip().upper()
if sym_u.isdigit():
ccxt_symbol = _get_hyperliquid_ccxt_symbol_for_market_id(sym_u)
if not ccxt_symbol:
ccxt_symbol = _get_hyperliquid_ccxt_symbol_for_coin(raw)
if ccxt_symbol:
sym = ccxt_symbol
else:
sym = raw
# Normalize and format for directory
sym = str(sym).strip().upper()
# If already formatted (e.g., BTC_USDC:USDC), return as-is
if "/" in sym or "_" in sym:
return _format_ccxt_symbol_dir(sym)
# Strip quote currency from raw symbols (e.g., kBONKUSDC -> kBONK)
base = sym
quote = None
for q in ("USDC", "USDT"):
if sym.endswith(q) and len(sym) > len(q):
base = sym[: -len(q)]
quote = q
break
# Format as CCXT symbol: BASE/QUOTE:QUOTE
if quote:
sym = f"{base}/{quote}:{quote}"
else:
# No quote found - treat as base and add USDC (Hyperliquid default)
sym = f"{base}/USDC:USDC"
return _format_ccxt_symbol_dir(sym)
def get_exchange_download_log_path(exchange: str) -> Path:
return get_exchange_raw_root_dir(exchange) / "download.log"
def append_exchange_download_log(exchange: str, line: str, level: str = None) -> None:
"""Write one line to the MarketData log using the standard log format."""
ex = str(exchange or "").strip().lower()
tags = ["market_data"]
if ex:
tags.append(f"ex:{ex}")
human_log("MarketData", str(line or "").rstrip("\n"), tags=tags, level=level)
def get_market_data_config_path() -> Path:
return get_market_data_root_dir() / "config.json"
def _atomic_write_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(content, encoding="utf-8")
os.replace(tmp, path)
@dataclass
class MarketDataConfig:
"""Market-data config: enabled coins and auto-enable flags per exchange."""
version: int
enabled_coins: dict[str, list[str]]
auto_enable_new_coins: dict[str, bool]
def to_dict(self) -> dict[str, Any]:
return {
"version": int(self.version),
"enabled_coins": {
str(ex): [str(c) for c in coins]
for ex, coins in (self.enabled_coins or {}).items()
},
"auto_enable_new_coins": {
str(ex): bool(enabled)
for ex, enabled in (self.auto_enable_new_coins or {}).items()
},
}
def _normalize_market_data_exchange(exchange: str) -> str:
ex = str(exchange or "").strip().lower()
if ex in ("binanceusdm", "binance-usdm"):
return "binance"
return ex
def _canonical_enabled_coin(exchange: str, coin: str) -> str:
ex = _normalize_market_data_exchange(exchange)
s = str(coin or "").strip()
if not s:
return ""
if ex == "hyperliquid":
lower = s.lower()
if lower.startswith("xyz:") or lower.startswith("xyz-"):
tail = s[4:].strip().upper()
return f"xyz:{tail}" if tail else ""
return s.upper()
def load_market_data_config() -> MarketDataConfig:
path = get_market_data_config_path()
if not path.exists():
return MarketDataConfig(version=1, enabled_coins={}, auto_enable_new_coins={})
try:
raw = json.loads(path.read_text(encoding="utf-8"))
except Exception:
return MarketDataConfig(version=1, enabled_coins={}, auto_enable_new_coins={})
version = int(raw.get("version", 1)) if isinstance(raw, dict) else 1
enabled = raw.get("enabled_coins", {}) if isinstance(raw, dict) else {}
auto_enable = raw.get("auto_enable_new_coins", {}) if isinstance(raw, dict) else {}
if not isinstance(enabled, dict):
enabled = {}
if not isinstance(auto_enable, dict):
auto_enable = {}
cleaned: dict[str, list[str]] = {}
for ex, coins in enabled.items():
if not isinstance(ex, str):
continue
if not isinstance(coins, list):
continue
ex_key = _normalize_market_data_exchange(ex)
merged = set(cleaned.get(ex_key, []))
merged.update(
_canonical_enabled_coin(ex_key, c)
for c in coins
if _canonical_enabled_coin(ex_key, c)
)
cleaned[ex_key] = sorted(merged)
cleaned_auto: dict[str, bool] = {}
for ex, enabled_flag in auto_enable.items():
if not isinstance(ex, str):
continue
ex_key = _normalize_market_data_exchange(ex)
if not ex_key:
continue
cleaned_auto[ex_key] = bool(enabled_flag)
return MarketDataConfig(version=version, enabled_coins=cleaned, auto_enable_new_coins=cleaned_auto)
def save_market_data_config(cfg: MarketDataConfig) -> None:
path = get_market_data_config_path()
payload = json.dumps(cfg.to_dict(), indent=2, sort_keys=True)
_atomic_write_text(path, payload)
def set_enabled_coins(exchange: str, coins: list[str]) -> MarketDataConfig:
cfg = load_market_data_config()
ex = _normalize_market_data_exchange(exchange)
if not ex:
raise ValueError("exchange is empty")
norm_coins = sorted(
{
_canonical_enabled_coin(ex, c)
for c in (coins or [])
if _canonical_enabled_coin(ex, c)
}
)
cfg.enabled_coins[ex] = norm_coins
save_market_data_config(cfg)
return cfg
def set_auto_enable_new_coins(exchange: str, enabled: bool) -> MarketDataConfig:
cfg = load_market_data_config()
ex = _normalize_market_data_exchange(exchange)
if not ex:
raise ValueError("exchange is empty")
cfg.auto_enable_new_coins[ex] = bool(enabled)
save_market_data_config(cfg)
return cfg
def get_market_data_coin_options(exchange: str) -> list[str]:
ex = _normalize_market_data_exchange(exchange)
if not ex:
return []
try:
coindata = CoinData()
approved_coins, _ = coindata.filter_mapping(
exchange=ex,
market_cap_min_m=0,
vol_mcap_max=float("inf"),
only_cpt=False,
notices_ignore=False,
tags=[],
quote_filter=None,
use_cache=True,
active_only=True,
)
approved = sorted(
{
_canonical_enabled_coin(ex, coin)
for coin in approved_coins
if _canonical_enabled_coin(ex, coin)
}
)
if approved:
return _filter_live_market_data_coin_options(ex, approved)
except Exception:
pass
try:
mapping_path = Path(__file__).resolve().parent / "data" / "coindata" / ex / "mapping.json"
if mapping_path.exists():
mapping = json.loads(mapping_path.read_text(encoding="utf-8"))
mapped_coins: set[str] = set()
for row in mapping if isinstance(mapping, list) else []:
if not bool(row.get("swap", False)) or not bool(row.get("active", True)) or not bool(row.get("linear", True)):
continue
coin = str(row.get("coin") or "").strip()
if not coin:
symbol = str(row.get("ccxt_symbol") or row.get("symbol") or "").strip()
quote = str(row.get("quote") or "").strip().upper()
if not symbol:
continue
coin = compute_coin_name(symbol, quote)
coin = _canonical_enabled_coin(ex, coin)
if coin:
mapped_coins.add(coin)
if mapped_coins:
return _filter_live_market_data_coin_options(ex, sorted(mapped_coins))
except Exception:
pass
try:
symbols = load_symbols_from_ini(ex, "swap")
fallback = sorted(
{
_canonical_enabled_coin(ex, symbol)
for symbol in symbols
if _canonical_enabled_coin(ex, symbol)
}
)
if fallback:
return _filter_live_market_data_coin_options(ex, fallback)
except Exception:
pass
return []
def _filter_live_market_data_coin_options(exchange: str, coins: list[str]) -> list[str]:
ex = _normalize_market_data_exchange(exchange)
normalized = sorted(
{
_canonical_enabled_coin(ex, coin)
for coin in (coins or [])
if _canonical_enabled_coin(ex, coin)
}
)
if ex != "hyperliquid":
return normalized
try:
from hyperliquid_api import resolve_hyperliquid_coin_name
except Exception:
return normalized
out: list[str] = []
for coin in normalized:
coin_l = coin.lower()
if not (coin_l.startswith("xyz:") or coin_l.startswith("xyz-")):
out.append(coin)
continue
try:
resolve_hyperliquid_coin_name(coin=coin, timeout_s=5.0)
out.append(coin)
except Exception:
continue
return out
def get_effective_enabled_coins(
exchange: str,
*,
cfg: MarketDataConfig | None = None,
coin_options: list[str] | None = None,
) -> tuple[list[str], list[str], bool]:
ex = _normalize_market_data_exchange(exchange)
if not ex:
return [], [], False
config = cfg or load_market_data_config()
saved_enabled_raw = [
_canonical_enabled_coin(ex, coin)
for coin in (config.enabled_coins.get(ex, []) or [])
if _canonical_enabled_coin(ex, coin)
]
auto_enable = bool((config.auto_enable_new_coins or {}).get(ex, False))
options_source = coin_options if coin_options is not None else get_market_data_coin_options(ex)
normalized_options = sorted(
{
_canonical_enabled_coin(ex, coin)
for coin in (options_source or [])
if _canonical_enabled_coin(ex, coin)
}
)
if not normalized_options:
return saved_enabled_raw, [], auto_enable
option_set = set(normalized_options)
if auto_enable:
return normalized_options, [], auto_enable
enabled = [coin for coin in saved_enabled_raw if coin in option_set]
missing = sorted(set(saved_enabled_raw) - option_set)
return enabled, missing, auto_enable
def summarize_raw_inventory(
exchange: str,
*,
limit: int = 0,
skip_coverage: bool = False,
datasets_filter: list[str] | None = None,
) -> list[dict[str, Any]]:
"""Return a lightweight inventory of raw files per dataset+coin.
Scans:
data/ohlcv/{exchange}/{dataset}/{coin}/*.{lz4,jsonl,npz}
Returns rows with:
exchange, dataset, coin, n_files,
total_bytes,
oldest_day, newest_day,
n_days, expected_hours, coverage_pct (0 if skip_coverage=True),
missing_days_count (0 if skip_coverage=True), missing_days_sample
Args:
skip_coverage: If True, skip expensive coverage/missing days calculation for faster initial load.
datasets_filter: Optional list of dataset names to include (case-insensitive),
e.g. ["1m", "candles_1m"]. If None, all datasets are scanned.
"""
ex = str(exchange or "").strip().lower()
if not ex:
raise ValueError("exchange is empty")
base = get_exchange_raw_root_dir(ex)
if not base.exists():
return []
rows: list[dict[str, Any]] = []
ds_filter = None
if isinstance(datasets_filter, list) and datasets_filter:
ds_filter = {str(x).strip().lower() for x in datasets_filter if str(x).strip()}
datasets = [p for p in base.iterdir() if p.is_dir()]
for dataset_dir in sorted(datasets, key=lambda p: p.name):
ds_l = dataset_dir.name.strip().lower()
if ds_l.endswith("_src"):
continue
if ds_filter is not None and ds_l not in ds_filter:
continue
for coin_dir in sorted([p for p in dataset_dir.iterdir() if p.is_dir()], key=lambda p: p.name):
n_files = 0
total_bytes = 0
oldest_day = ""
newest_day = ""
n_days = 0
expected_hours = 0
coverage_pct = 0.0
missing_days_count = 0
missing_days_sample = ""
try:
oldest_key: tuple[str, str] | None = None
newest_key: tuple[str, str] | None = None
days_present: set[str] = set()
hours_present: set[tuple[str, str]] = set()
for p in coin_dir.iterdir():
if not p.is_file():
continue
if p.suffix.lower() not in (".lz4", ".jsonl", ".npz"):
continue
if ds_l in ("1m", "candles_1m", "1m_api", "candles_1m_api") and p.suffix.lower() != ".npz":
continue
parsed = _parse_day_hour_from_filename(p.name)
if not parsed:
continue
day, hour = parsed
n_files += 1
try:
total_bytes += int(p.stat().st_size)
except Exception:
pass
days_present.add(day)
# Collect hours for coverage calculation
# - l2book: hour from filename (20241205-16.lz4)
# - 1m_api: minimal - skip detailed hours for speed
# - 1m: will use sources.idx later (fast!)
if not skip_coverage:
if hour is not None:
# l2book or other datasets with hour in filename
hours_present.add((day, hour))
elif ds_l not in ("1m", "candles_1m", "1m_api", "candles_1m_api"):
# Other NPZ files: read hours from file
hours_in_file = _hours_from_npz(p)
for hh in hours_in_file:
hours_present.add((day, hh))
key = (day, hour or "00")
if oldest_key is None or key < oldest_key:
oldest_key = key
if newest_key is None or key > newest_key:
newest_key = key
if oldest_key:
oldest_day = oldest_key[0]
if newest_key:
newest_day = newest_key[0]
if oldest_day and newest_day:
try:
dt0 = datetime.strptime(oldest_day, "%Y%m%d").date()
dt1 = datetime.strptime(newest_day, "%Y%m%d").date()
if ds_l in ("1m", "candles_1m", "1m_api", "candles_1m_api"):
today = date.today()
if today > dt1:
dt1 = today
if dt1 >= dt0:
n_days = (dt1 - dt0).days + 1
expected_hours = int(n_days) * 24
# Fast coverage calculation if requested
if not skip_coverage:
# For 1m dataset: use sources.idx for FAST hour info!
if ds_l in ("1m", "candles_1m"):
try:
from market_data_sources import get_daily_source_counts_for_range
counts = get_daily_source_counts_for_range(
exchange=ex,
coin=coin_dir.name,
start_day=oldest_day,
end_day=newest_day,
lag_minutes=0,
cutoff_ts_ms=None,
)
if counts:
# Count hours from sources.idx
total_minutes = 0
for day_data in counts.values():
total_minutes += sum(day_data.values())
total_hours = total_minutes // 60
if expected_hours > 0:
coverage_pct = (total_hours / float(expected_hours)) * 100.0
# Count missing days (days with < 1440 minutes)
missing_days = []
cur = dt0
while cur <= dt1:
ds = cur.strftime("%Y%m%d")
day_minutes = sum(counts.get(ds, {}).values())
if day_minutes < 1440:
missing_days.append(ds)
cur = cur + timedelta(days=1)
missing_days_count = len(missing_days)
if missing_days_count:
missing_days_sample = ",".join(missing_days[:10])
if missing_days_count > 10:
missing_days_sample += ",..."
except Exception:
pass
# For 1m_api: minimal - skip detailed coverage for speed
elif ds_l in ("1m_api", "candles_1m_api"):
# Just estimate: file count vs expected
if expected_hours > 0 and n_days > 0:
# Rough estimate: each file ~= 1 day
coverage_pct = min(100.0, (n_files / float(n_days)) * 100.0)
# For other datasets (l2book): use collected hours
else:
if expected_hours > 0:
coverage_pct = (len(hours_present) / float(expected_hours)) * 100.0
# FAST missing days: pre-group hours by day O(n) instead of O(n²)
hours_by_day: dict[str, int] = {}
for (day, _hour) in hours_present:
hours_by_day[day] = hours_by_day.get(day, 0) + 1
missing_days: list[str] = []
cur = dt0
while cur <= dt1:
ds = cur.strftime("%Y%m%d")
day_hours = hours_by_day.get(ds, 0)
if day_hours < 24:
missing_days.append(ds)
cur = cur + timedelta(days=1)
missing_days_count = len(missing_days)
if missing_days_count:
missing_days_sample = ",".join(missing_days[:10])
if missing_days_count > 10:
missing_days_sample += ",..."
except Exception:
pass
except Exception:
n_files = int(n_files) if n_files else 0
total_bytes = int(total_bytes) if total_bytes else 0
rows.append(
{
"exchange": ex,
"dataset": dataset_dir.name,
"coin": coin_dir.name,
"n_files": int(n_files),
"total_bytes": int(total_bytes),
"oldest_day": oldest_day,
"newest_day": newest_day,
"n_days": int(n_days),
"expected_hours": int(expected_hours),
"coverage_pct": float(round(coverage_pct, 2)),
"missing_days_count": int(missing_days_count),
"missing_days_sample": missing_days_sample,
}
)
if limit and len(rows) >= int(limit):
return rows
return rows
def _get_pb7_root_dir(pb7_root: str | Path | None = None) -> Path | None:
if pb7_root:
try:
p = Path(pb7_root).expanduser().resolve()
return p if p.exists() else None
except Exception:
return None
try:
cfg = configparser.ConfigParser()
cfg.read(Path(__file__).resolve().parent / "pbgui.ini")
ini_val = str(cfg.get("main", "pb7dir", fallback="") or "").strip()
if ini_val:
p = Path(ini_val).expanduser().resolve()
if p.exists():
return p
except Exception:
pass
try:
p = (Path(__file__).resolve().parents[1] / "pb7").resolve()
return p if p.exists() else None
except Exception:
return None
def _parse_pb7_cache_day_from_name(name: str) -> str:
s = str(name or "")
stem = Path(s).stem
try:
if len(stem) == 10 and stem[4] == "-" and stem[7] == "-":
return stem.replace("-", "")
if len(stem) == 8 and stem.isdigit():
return stem
except Exception:
pass
return ""
def summarize_pb7_cache_inventory(
exchange: str,
*,
pb7_root: str | Path | None = None,
limit: int = 500,
) -> list[dict[str, Any]]:
ex = str(exchange or "").strip().lower()
if not ex:
raise ValueError("exchange is empty")
root = _get_pb7_root_dir(pb7_root)
if root is None:
return []
base = root / "caches" / "ohlcv" / ex
if not base.exists() or not base.is_dir():
return []
rows: list[dict[str, Any]] = []
for timeframe_dir in sorted([p for p in base.iterdir() if p.is_dir()], key=lambda p: p.name):
tf = str(timeframe_dir.name)
for coin_dir in sorted([p for p in timeframe_dir.iterdir() if p.is_dir()], key=lambda p: p.name):
n_files = 0
total_bytes = 0
days_present: set[str] = set()
for f in coin_dir.iterdir():
if not f.is_file():
continue
if f.suffix.lower() != ".npy":
continue
day = _parse_pb7_cache_day_from_name(f.name)
if not day:
continue
n_files += 1
days_present.add(day)
try:
total_bytes += int(f.stat().st_size)
except Exception:
pass
if n_files <= 0:
continue
oldest_day = ""
newest_day = ""
n_days = 0
try:
oldest_day = min(days_present) if days_present else ""
newest_day = max(days_present) if days_present else ""
if oldest_day and newest_day:
dt0 = datetime.strptime(oldest_day, "%Y%m%d").date()
dt1 = datetime.strptime(newest_day, "%Y%m%d").date()
if dt1 >= dt0:
n_days = (dt1 - dt0).days + 1
except Exception:
pass
rows.append(
{
"exchange": ex,
"timeframe": tf,
"coin": coin_dir.name,
"n_files": int(n_files),
"total_bytes": int(total_bytes),
"oldest_day": oldest_day,
"newest_day": newest_day,
"n_days": int(n_days),
}
)
if limit and len(rows) >= int(limit):
return rows
return rows
def get_daily_presence_for_pb7_cache(
exchange: str,
timeframe: str,
coin: str,
*,
pb7_root: str | Path | None = None,
start_day: str | None = None,
end_day: str | None = None,
) -> dict[str, Any]:
"""Return per-day presence for PB7 cache files (status 0/2)."""
ex = str(exchange or "").strip().lower()
tf = str(timeframe or "").strip()
cn = str(coin or "").strip()
if not ex or not tf or not cn:
return {"oldest_day": "", "newest_day": "", "days": []}
root = _get_pb7_root_dir(pb7_root)
if root is None:
return {"oldest_day": "", "newest_day": "", "days": []}
base = root / "caches" / "ohlcv" / ex / tf / cn
if not base.exists() or not base.is_dir():
return {"oldest_day": "", "newest_day": "", "days": []}
days_present: set[str] = set()
for p in base.iterdir():
if not p.is_file() or p.suffix.lower() != ".npy":
continue
day = _parse_pb7_cache_day_from_name(p.name)
if day:
days_present.add(day)
if not days_present:
return {"oldest_day": "", "newest_day": "", "days": []}
try:
data_oldest = min(days_present)
data_newest = max(days_present)
dt0 = datetime.strptime(data_oldest, "%Y%m%d").date()
dt1 = datetime.strptime(data_newest, "%Y%m%d").date()
if start_day:
s0 = _normalize_day_str(start_day)
if re.fullmatch(r"\d{8}", s0):
dt0 = datetime.strptime(s0, "%Y%m%d").date()
if end_day:
s1 = _normalize_day_str(end_day)
if re.fullmatch(r"\d{8}", s1):
dt1 = datetime.strptime(s1, "%Y%m%d").date()
if dt1 < dt0:
return {"oldest_day": "", "newest_day": "", "days": []}
except Exception:
return {"oldest_day": "", "newest_day": "", "days": []}
days: list[dict[str, Any]] = []
cur = dt0
while cur <= dt1:
ds = cur.strftime("%Y%m%d")
present = ds in days_present
days.append(
{
"day": ds,
"hours": 24 if present else 0,
"status": 2 if present else 0,
}
)
cur = cur + timedelta(days=1)
return {"oldest_day": dt0.strftime("%Y%m%d"), "newest_day": dt1.strftime("%Y%m%d"), "days": days}
def get_daily_presence_for_dataset(exchange: str, dataset: str, coin: str) -> dict[str, Any]:
"""Return day-by-day presence for a dataset/coin.
Output keys: oldest_day, newest_day, days=[{day, present}]
"""
ex = str(exchange or "").strip().lower()
ds = normalize_market_data_dataset(dataset)
cn = normalize_market_data_coin_dir(ex, coin)
ds_l = ds.strip().lower()
if not ex or not ds or not cn:
return {"oldest_day": "", "newest_day": "", "days": []}
base = get_exchange_raw_root_dir(ex) / ds / cn
if not base.exists():
return {"oldest_day": "", "newest_day": "", "days": []}
days_present: set[str] = set()
oldest_day = ""
newest_day = ""
for p in base.iterdir():
if not p.is_file():
continue
if p.suffix.lower() not in (".lz4", ".jsonl", ".npz"):
continue
if ds_l in ("1m", "candles_1m", "1m_api", "candles_1m_api") and p.suffix.lower() != ".npz":
continue
parsed = _parse_day_hour_from_filename(p.name)
if not parsed:
continue
day, hour = parsed
days_present.add(day)
if not days_present:
return {"oldest_day": "", "newest_day": "", "days": []}
try:
oldest_day = min(days_present)
newest_day = max(days_present)
dt0 = datetime.strptime(oldest_day, "%Y%m%d").date()
dt1 = datetime.strptime(newest_day, "%Y%m%d").date()
except Exception:
return {"oldest_day": "", "newest_day": "", "days": []}
days: list[dict[str, Any]] = []
cur = dt0
while cur <= dt1:
ds = cur.strftime("%Y%m%d")
days.append({"day": ds, "present": ds in days_present})
cur = cur + timedelta(days=1)
return {"oldest_day": oldest_day, "newest_day": newest_day, "days": days}
def _resolve_dataset_coin_dirs(
exchange: str, dataset: str, coin: str,
) -> list[Path]:
"""Return list of existing directories to scan for a dataset/coin.
Handles the l2Book/l2book casing issue and includes the NAS archive
directory for l2book if configured.
"""
ex = str(exchange or "").strip().lower()
ds = normalize_market_data_dataset(dataset)
cn = normalize_market_data_coin_dir(ex, coin)
ds_l = ds.strip().lower()
if not ex or not ds or not cn:
return []
dirs: list[Path] = []
# Primary local path
base = get_exchange_raw_root_dir(ex) / ds / cn
if base.exists():
dirs.append(base)
# l2book / l2Book casing fallback
if ds_l == "l2book" and not dirs:
for alt in ("l2Book", "l2book"):