#!/usr/bin/env python3
"""Peer / macro signals for 00412A Asia-semi context + hyperscalers."""
import json
import numpy as np
import pandas as pd
import yfinance as yf

out = {}
for sym in ["0981.HK", "005930.KS", "2330.TW", "2379.TW", "2344.TW", "^TWII", "^SOX",
             "AMZN", "MSFT", "GOOGL", "CRWV", "JPYTWD=X"]:
    try:
        h = yf.Ticker(sym).history(period="3mo", auto_adjust=False)
        c = h["Close"].dropna()
        if len(c) < 2:
            raise ValueError("insufficient bars: %d" % len(c))
        out[sym] = {
            "ok": True,
            "last": str(c.index[-1].date()),
            "close": round(float(c.iloc[-1]), 4),
            "prev": round(float(c.iloc[-2]), 4),
            "day_pct": round(float((c.iloc[-1] / c.iloc[-2] - 1) * 100), 2),
            "chg3d_pct": round(float((c.iloc[-1] / c.iloc[-4] - 1) * 100), 2) if len(c) >= 4 else None,
            "chg5d_pct": round(float((c.iloc[-1] / c.iloc[-6] - 1) * 100), 2) if len(c) >= 6 else None,
            "high60": round(float(c.tail(60).max()), 4),
            "dist_high60_pct": round(float((c.iloc[-1] / c.tail(60).max() - 1) * 100), 2),
        }
    except Exception as e:
        out[sym] = {"ok": False, "error": repr(e)[:200]}

print("###JSON###")
print(json.dumps(out, ensure_ascii=False, indent=1))
