"""Render the study's headline finding as a PNG for the dataset repo README. python research-data/make_chart.py public/data/xauusd-hourly.csv out.png Drawn with PIL rather than matplotlib, which is not installed here and would be a heavy dependency for one 24-bar chart. Two series on one axis pair: bars — median M5 range in USD, the thing that actually varies across the day line — median spread in USD, the thing that famously does not That contrast IS the finding. Spread sits at $0.16 in all 24 hours while range moves by a factor of several, so cost-as-a-share-of-move is driven almost entirely by the denominator. A reader should be able to see that without opening the CSV. """ from __future__ import annotations import csv import sys from PIL import Image, ImageDraw, ImageFont W, H = 1000, 420 PAD_L, PAD_R, PAD_T, PAD_B = 70, 70, 58, 54 BG = (13, 17, 23) # GitHub dark canvas, so the chart sits flush in the README GRID = (33, 38, 45) BAR = (56, 139, 253) BAR_DIM = (33, 82, 148) LINE = (63, 185, 80) TEXT = (201, 209, 217) MUTED = (125, 133, 144) def font(size: int): for name in ("segoeui.ttf", "arial.ttf", "DejaVuSans.ttf"): try: return ImageFont.truetype(name, size) except OSError: continue return ImageFont.load_default() def main() -> int: src, out = sys.argv[1], sys.argv[2] with open(src, newline="", encoding="utf-8") as fh: rows = list(csv.DictReader(fh)) hours = [int(r["hour"]) for r in rows] rng = [float(r["range_median_usd"]) for r in rows] spr = [float(r["spread_median_usd"]) for r in rows] cov = [float(r["coverage_pct"]) for r in rows] img = Image.new("RGB", (W, H), BG) d = ImageDraw.Draw(img) f_title, f_lbl, f_sm = font(19), font(13), font(11) d.text((PAD_L, 16), "XAUUSD: volatility moves across the day. Spread does not.", font=f_title, fill=TEXT) d.text((PAD_L, 40), f"{len(rows)} hours of broker server time · 70,546 M5 bars · Aug 2025 – Jul 2026", font=f_sm, fill=MUTED) plot_w = W - PAD_L - PAD_R plot_h = H - PAD_T - PAD_B y0 = PAD_T + plot_h rng_max = max(rng) * 1.15 # Spread needs its own scale — it is an order of magnitude smaller than range — but a # second scale MUST be labelled. The first version of this chart drew spread against an # unlabelled axis, which put a $0.16 line at the height of the "$2" gridline on the only # axis a reader could see. That is a misleading chart, not a compact one. spr_max = 0.50 # Horizontal grid, left axis (range, USD), right axis (spread, USD) for i in range(5): y = y0 - plot_h * i / 4 d.line([(PAD_L, y), (PAD_L + plot_w, y)], fill=GRID) d.text((PAD_L - 46, y - 7), f"${rng_max * i / 4:,.0f}", font=f_sm, fill=BAR) d.text((PAD_L + plot_w + 10, y - 7), f"${spr_max * i / 4:.2f}", font=f_sm, fill=LINE) bw = plot_w / len(rows) for i, h in enumerate(hours): x = PAD_L + i * bw bh = plot_h * rng[i] / rng_max # The maintenance-break hour has reduced coverage and must not be read as a # like-for-like comparison, so it is drawn dimmed rather than silently equal. colour = BAR if cov[i] >= 95 else BAR_DIM d.rectangle([x + 2, y0 - bh, x + bw - 3, y0], fill=colour) if h % 3 == 0: d.text((x + bw / 2 - 6, y0 + 8), f"{h:02d}", font=f_sm, fill=MUTED) pts = [(PAD_L + i * bw + bw / 2, y0 - plot_h * spr[i] / spr_max) for i in range(len(rows))] d.line(pts, fill=LINE, width=3) d.text((PAD_L, H - 26), "bars: median 5-min range, left axis", font=f_lbl, fill=BAR) d.text((PAD_L + 250, H - 26), "line: median spread, right axis", font=f_lbl, fill=LINE) d.text((PAD_L + 480, H - 26), "dimmed = reduced coverage (daily break)", font=f_lbl, fill=MUTED) d.text((W - PAD_R - 100, H - 26), "hour, server time", font=f_lbl, fill=MUTED) img.save(out, "PNG", optimize=True) print(f"wrote {out} ({img.size[0]}x{img.size[1]})") return 0 if __name__ == "__main__": raise SystemExit(main())