import json,datetime as dt,sys
from zoneinfo import ZoneInfo
ny=ZoneInfo('America/New_York')
out=sys.argv[1]
eq=json.load(open('equity.json')); hr=json.load(open('hourly.json'))
W,H,L,R,T,B=760,400,64,24,48,64
FONT="font-family='Inter,system-ui,sans-serif'"
def frame(title,sub):
    return [f"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {W} {H}' width='{W}' height='{H}'>",
            f"<rect width='{W}' height='{H}' fill='#ffffff'/>",
            f"<text x='{L}' y='22' {FONT} font-size='15' font-weight='600' fill='#111'>{title}</text>",
            f"<text x='{L}' y='39' {FONT} font-size='12' fill='#666'>{sub}</text>"]
# 1: equity curves
ser={'Overnight':('overnight50','#2563eb'),'Intraday':('intraday50','#d97706')}
bars=eq['bars']; c0=bars[0][1]
lines={k:[(t,(e/1010-1)*100) for t,e in eq[v]] for k,(v,_) in ser.items()}
lines['Buy and hold']=[(t,(c/c0-1)*100) for t,c in bars]
cols={**{k:v[1] for k,v in ser.items()},'Buy and hold':'#9ca3af'}
t0,t1=bars[0][0],bars[-1][0]
ys=[y for l in lines.values() for _,y in l]; y0,y1=min(ys)-0.2,max(ys)+0.2
X=lambda t:L+(t-t0)/(t1-t0)*(W-L-R); Y=lambda y:T+(y1-y)/(y1-y0)*(H-T-B)
s=frame('Onchain SPY, 10 Sep to 22 Oct 2026: overnight vs intraday','Return on $1,000 per trade, 0.005% pool fee each way, 5-minute bars')
import math
step=0.5
v=math.ceil(y0/step)*step
while v<=y1:
    s.append(f"<line x1='{L}' x2='{W-R}' y1='{Y(v):.1f}' y2='{Y(v):.1f}' stroke='{'#bbb' if abs(v)<1e-9 else '#eee'}'/>")
    s.append(f"<text x='{L-8}' y='{Y(v)+4:.1f}' {FONT} font-size='11' fill='#666' text-anchor='end'>{v:+.1f}%</text>"); v+=step
d=dt.datetime.fromtimestamp(t0,ny).date()
while True:
    d+=dt.timedelta(days=1)
    ts=int(dt.datetime(d.year,d.month,d.day,tzinfo=ny).timestamp())
    if ts>t1: break
    if d.weekday()==0:
        s.append(f"<text x='{X(ts):.1f}' y='{H-B+18}' {FONT} font-size='11' fill='#666' text-anchor='middle'>{d.strftime('%b %-d')}</text>")
for k in ['Buy and hold']+list(ser):
    pts=lines[k][::3]+[lines[k][-1]]
    s.append(f"<polyline fill='none' stroke='{cols[k]}' stroke-width='{1.4 if k=='Buy and hold' else 2}' points='{' '.join(f'{X(t):.1f},{Y(y):.1f}' for t,y in pts)}'/>")
for i,k in enumerate(list(ser)+['Buy and hold']):
    x=L+i*190
    s.append(f"<rect x='{x}' y='{H-16}' width='12' height='3' fill='{cols[k]}'/><text x='{x+18}' y='{H-11}' {FONT} font-size='11' fill='#333'>{k} {lines[k][-1][1]:+.2f}%</text>")
s.append('</svg>'); open(out+'/equity.svg','w').write('\n'.join(s))
# 2: hourly moves
W2=W; s=frame('How much onchain SPY moves, by New York hour','Mean absolute change over the hour, basis points, weekdays, 10 Sep to 22 Oct 2026')
vals=[hr['weekday'][str(h)] for h in range(24)]; m=max(vals)*1.1
bw=(W-L-R)/24
s.append(f"<rect x='{L+9.5*bw:.1f}' y='{T}' width='{6.5*bw:.1f}' height='{H-T-B}' fill='#f3f4f6'/>")
s.append(f"<text x='{L+12.75*bw:.1f}' y='{T+14}' {FONT} font-size='11' fill='#666' text-anchor='middle'>NYSE open 9:30 to 16:00</text>")
for v in range(0,int(m)+1,5):
    y=T+(m-v)/m*(H-T-B)
    s.append(f"<line x1='{L}' x2='{W-R}' y1='{y:.1f}' y2='{y:.1f}' stroke='#eee'/><text x='{L-8}' y='{y+4:.1f}' {FONT} font-size='11' fill='#666' text-anchor='end'>{v} bp</text>")
for h,v in enumerate(vals):
    x=L+h*bw; y=T+(m-v)/m*(H-T-B)
    s.append(f"<rect x='{x+3:.1f}' y='{y:.1f}' width='{bw-6:.1f}' height='{H-B-y:.1f}' fill='{'#2563eb' if h<9 or h>=16 else '#93c5fd'}'/>")
    if h%3==0: s.append(f"<text x='{x+bw/2:.1f}' y='{H-B+18}' {FONT} font-size='11' fill='#666' text-anchor='middle'>{h:02d}:00</text>")
we=hr['weekend']; y=T+(m-we)/m*(H-T-B)
s.append(f"<line x1='{L}' x2='{W-R}' y1='{y:.1f}' y2='{y:.1f}' stroke='#dc2626' stroke-dasharray='4 4'/><text x='{L+6}' y='{H-12}' {FONT} font-size='11' fill='#dc2626'>- - - weekend average, {we} bp an hour</text>")
s.append('</svg>'); open(out+'/hourly.svg','w').write('\n'.join(s))
