"""Independent standard-library Fraction calculation; no API calls or trading. Run: python verify.py.txt # compare the stored lab results python verify.py.txt --calculate # print exact independently computed results The script reads files beside itself. It never writes a file or starts another process. """ from pathlib import Path from datetime import datetime, timezone from fractions import Fraction import hashlib import json import sys BASE = Path(__file__).resolve().parent raw = (BASE / "dataset.json").read_bytes() data = json.loads(raw) assert data["version"] == "chart-lab-20261012-v1" assert data["source"]["product"] == "LTC-USD" window = data["window"] assert window["start_epoch"] == 1791714600 assert window["end_epoch"] == 1791801000 assert window["granularity_seconds"] == 900 assert len(data["candles"]) == window["expected_count"] == 96 def utc(epoch): return datetime.fromtimestamp(epoch, timezone.utc).isoformat().replace("+00:00", "Z") def dec(value): scaled = value * 100_000_000 assert scaled.denominator == 1 n = scaled.numerator whole, part = divmod(abs(n), 100_000_000) tail = f"{part:08d}".rstrip("0") return ("-" if n < 0 else "") + str(whole) + ("." + tail if tail else "") def percent(value): n, d = abs(value.numerator), value.denominator scaled = (n * 10_000 + d // 2) // d return {"numerator": str(value.numerator), "denominator": str(d), "percent_rounded_4": ("-" if value < 0 else "") + f"{scaled // 10_000}.{scaled % 10_000:04d}"} def drawdown(values): best, peak, peak_index = Fraction(0), values[0], 0 indices = [None, None] for index, value in enumerate(values): if value > peak: peak, peak_index = value, index fall = (peak - value) / peak * 100 if fall > best: best, indices = fall, [peak_index, index] return best, indices source = [] for index, row in enumerate(data["candles"]): start = window["start_epoch"] + index * 900 assert row["start_epoch"] == start assert row["start_utc"] == utc(start) assert row["end_exclusive_utc"] == utc(start + 900) prices = {field: Fraction(row[field]) for field in ["open", "high", "low", "close", "volume"]} assert all(prices[field] > 0 for field in ["open", "high", "low", "close"]) assert prices["volume"] >= 0 assert prices["low"] <= min(prices["open"], prices["close"]) assert prices["high"] >= max(prices["open"], prices["close"]) source.append(dict(prices, start=start, end=start + 900)) result = {"version": data["version"], "verifier": "independent Python Fraction calculation; not a second provider capture", "dataset_sha256": hashlib.sha256(raw).hexdigest(), "intervals": []} for minutes in [15, 60, 240]: factor = minutes // 15 assert len(source) % factor == 0 groups = [] for start in range(0, len(source), factor): part = source[start:start + factor] groups.append({"start": part[0]["start"], "end": part[-1]["end"], "open": part[0]["open"], "close": part[-1]["close"], "high": max(row["high"] for row in part), "low": min(row["low"] for row in part), "volume": sum((row["volume"] for row in part), Fraction(0))}) close = [row["close"] for row in groups] dd, indices = drawdown(close) result["intervals"].append({"minutes": minutes, "group_count": len(groups), "source_count": len(source), "anchor_utc": utc(groups[0]["start"]), "first_close": dec(close[0]), "last_close": dec(close[-1]), "first_close_end_boundary_utc": utc(groups[0]["end"]), "last_close_end_boundary_utc": utc(groups[-1]["end"]), "end_boundary_separation_seconds": groups[-1]["end"] - groups[0]["end"], "close_change_percent": percent((close[-1] - close[0]) / close[0] * 100), "maximum_close_drawdown_percent": percent(dd), "drawdown_peak_start_utc": None if indices[0] is None else utc(groups[indices[0]]["start"]), "drawdown_trough_start_utc": None if indices[1] is None else utc(groups[indices[1]]["start"]), "highest_high": dec(max(row["high"] for row in groups)), "lowest_low": dec(min(row["low"] for row in groups)), "highest_close": dec(max(close)), "lowest_close": dec(min(close)), "summed_volume_ltc": dec(sum((row["volume"] for row in groups), Fraction(0))), "buckets": [{"start_utc": utc(row["start"]), "end_exclusive_utc": utc(row["end"]), **{key: dec(row[key]) for key in ["open", "high", "low", "close"]}, "volume_ltc": dec(row["volume"]), "source_15m_buckets": factor} for row in groups]}) result["fictional_paths"] = [] for case in data["fictional_paths"]: assert case["fictional"] is True values = [Fraction(value) for value in case["ordered_prices"]] dd, indices = drawdown(values) result["fictional_paths"].append({"id": case["case_id"], "fictional": True, "ohlc": {"open": dec(values[0]), "high": dec(max(values)), "low": dec(min(values)), "close": dec(values[-1])}, "first_to_last_percent": percent((values[-1] - values[0]) / values[0] * 100), "maximum_ordered_point_drawdown_percent": percent(dd), "peak_index": indices[0], "trough_index": indices[1]}) assert result["fictional_paths"][0]["ohlc"] == result["fictional_paths"][1]["ohlc"] if "--calculate" in sys.argv: print(json.dumps(result, indent=2)) else: assert result == json.loads((BASE / "results.json").read_text(encoding="utf-8")) print(json.dumps({"ok": True, "source_buckets": 96, "groups": [len(item["buckets"]) for item in result["intervals"]], "fictional_paths": 2, "network_requests": 0, "files_written": 0}))