#!/usr/bin/env python3
"""Verify 6669 split via Yahoo + resolve skill's volume-rule convention (pitfall #18)."""
import json
import numpy as np
import pandas as pd
import yfinance as yf

t = yf.Ticker("6669.TW")
h = t.history(period="6mo", auto_adjust=False)
c = h["Close"].dropna()
v = h["Volume"].dropna()

print("=== Yahoo raw Close/Volume around 2026-09-01..09-04 ===")
sub = h.loc["2026-08-28":"2026-09-08", ["Open", "High", "Low", "Close", "Volume"]]
print(sub.to_string())

print("\n=== Yahoo splits ===")
try:
    print(t.splits.tail(10))
except Exception as e:
    print("splits err", e)

print("\n=== Yahoo last 25 ===")
print(h.tail(25)[["Close", "Volume"]].to_string())

# Reproduce pitfall #18 numbers: note written 2026-10-26, last trading day then = 2026-10-22
seg = h.loc[:"2026-10-22"]
print("\n=== Yahoo bars 10/12..10/22 (volume) ===")
print(seg.loc["2026-10-10":"2026-10-22", ["Close", "Volume"]].to_string())

# candidate comparisons as of 2026-10-26 (last bar 10/22)
v26 = seg["Volume"].dropna()
last3_26 = v26.tail(3)
print("\n[as of 10/22] last3 avg:", last3_26.mean(), list(last3_26))
print("[as of 10/22] first3 of 9-day seg (10/12,13,14):", v26.loc["2026-10-12":"2026-10-14"].mean(),
      list(v26.loc["2026-10-12":"2026-10-14"]))
print("[as of 10/22] 10/14,15,16 avg:", v26.loc["2026-10-14":"2026-10-16"].mean())
print("[as of 10/22] pre-streak 3 (10/07,10/08,10/12):", v26.loc["2026-10-07":"2026-10-12"].mean())

# and as of today 10/30
print("\n=== TODAY (10/30) Yahoo volumes ===")
print(h.tail(10)[["Close", "Volume"]].to_string())
vt = v26 = h["Volume"].dropna()
print("today vol:", vt.iloc[-1], "20d avg:", vt.tail(20).mean(), "ratio:", vt.iloc[-1]/vt.tail(20).mean())
print("streak(11d from 10/14) first3 avg:", vt.loc["2026-10-14":"2026-10-16"].mean(),
      "last3 avg:", vt.tail(3).mean())
print("pre-streak3 (10/07,10/08,10/12):", vt.loc["2026-10-07":"2026-10-12"].mean())
