Repair Yahoo adj-close/event-date misalignment (JLPSX, JLPYX)
JLPSX showed a bogus +29.3%/-22.9% 3-day wiggle in Dec 2020: Yahoo dated
the 6.824 year-end cap-gain distribution on the 12-11 record date but the
market went ex-div on 12-14 (close 30.10 -> 23.22), so the raw Adj Close
column pre-applied the adjustment 3 days before the price actually fell.
New 'history' correction op ({date: {col: value}}) patches individual OHLC
cells at bundle assembly (full build, incremental, and correction-changed
recompute paths); check_corrections validates dates/columns; 4 new tests.
scripts/scan_adj_misalign.py finds the artifact set-wide: 481 hits on 278
symbols, overwhelmingly December year-end distributions of value funds
(JLPSX's class of fund). scripts/fix_adj_misalign.py repairs it
arithmetic-only (rescale adj in [event, ex-div) by (1-f); cumulative
returns unchanged, cross-checked implied dist vs the price drop). Applied
to the only curated-fund hit (JLPSX) and its sister class JLPYX (implied
dist 6.824 both = official amount; ex-div 2020-12-14). The remaining ~276
symbols are reported in reports/adj_misalign/scan.md for a bulk run.
This commit is contained in:
@@ -51,6 +51,18 @@ def main() -> int:
|
||||
if not capgf.get(date):
|
||||
print(f"BAD {sym} drop_capg_copy {date}: no capitalGain row")
|
||||
bad += 1
|
||||
for date, cells in (d.get("history") or {}).items():
|
||||
p = FROZEN / f"{sym}-history.csv"
|
||||
if not p.exists():
|
||||
p = DATA / f"{sym}-history.csv"
|
||||
dates = {l.split(",")[0][:10] for l in p.read_text(errors="replace").splitlines()[1:]} if p.exists() else set()
|
||||
if date not in dates:
|
||||
print(f"BAD {sym} history {date}: date not in history file")
|
||||
bad += 1
|
||||
for col in cells:
|
||||
if col not in ("Open", "High", "Low", "Close", "Adj Close", "Volume"):
|
||||
print(f"BAD {sym} history {date}: unknown column {col!r}")
|
||||
bad += 1
|
||||
for key, rows_f in files.items():
|
||||
ops = d.get(key)
|
||||
if not ops or rows_f is None:
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Repair Yahoo adj-close / event-date misalignment (see
|
||||
scripts/scan_adj_misalign.py).
|
||||
|
||||
For a hit date D (adj spikes, close flat, price drops later) the repair is
|
||||
purely arithmetic and leaves cumulative returns unchanged:
|
||||
|
||||
r(t) = adj(t)/close(t); k = r(D)/r(D-1) # the bogus spike
|
||||
f = 1 - 1/k # dist fraction Yahoo applied
|
||||
d = f * close(D-1) # implied per-share dist
|
||||
E = first t > D with close(t) < close(t-1) - 0.5*d # true ex-div date
|
||||
for t in [D, E): adj(t) *= (1 - f) # restore pre-event ratio
|
||||
|
||||
For t before D and from E on the series is already consistent, so only the
|
||||
[D, E) window is patched. The patch is written as a 'history' correction
|
||||
(overrides/corrections/{SYM}.json, merged, with a note), so it survives
|
||||
re-downloads that rewrite the raw CSVs with the same Yahoo values.
|
||||
|
||||
Usage:
|
||||
python3 scripts/fix_adj_misalign.py [--only SYM[,SYM...]] [--dry-run]
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
from scripts.scan_adj_misalign import scan # noqa: E402
|
||||
|
||||
ROOT_DEFAULT = Path.home() / "prog/fin/stocks"
|
||||
CORR = Path(__file__).resolve().parent.parent / "overrides" / "corrections"
|
||||
NOTE = ("adj-close repair: Yahoo dated a distribution on {d} (record date) "
|
||||
"but the price went ex-div on {e} (close {c0:.2f} -> {c1:.2f}, drop "
|
||||
"~{d_amt:.3f}); the raw Adj Close column spiked {spike:+.1%} on {d} "
|
||||
"and snapped back later. Cells in [{d}, {e}) rescaled by (1-f) so "
|
||||
"the adjusted path is smooth; cumulative returns are unchanged. "
|
||||
"Mechanism: scripts/fix_adj_misalign.py, "
|
||||
"scan: reports/adj_misalign/scan.md.")
|
||||
|
||||
|
||||
def load_rows(sym: str, root: Path):
|
||||
import data as D
|
||||
fr = D.FROZEN_DIR / f"{sym}-history.csv"
|
||||
f = fr if (D.FROZEN_DIR.is_dir() and fr.exists()) else root / f"{sym}-history.csv"
|
||||
rows = []
|
||||
for l in f.read_text(errors="replace").splitlines()[1:]:
|
||||
if not l.strip():
|
||||
continue
|
||||
a = l.split(",")
|
||||
if len(a) < 6:
|
||||
continue
|
||||
try:
|
||||
rows.append((a[0][:10], float(a[4]), float(a[5])))
|
||||
except ValueError:
|
||||
continue
|
||||
return rows
|
||||
|
||||
|
||||
def repair(sym: str, date: str, rows) -> dict:
|
||||
"""Return {date: {"Adj Close": value}} patch, or {} if not repairable."""
|
||||
i = next((k for k, r in enumerate(rows) if r[0] == date), None)
|
||||
if i is None or i == 0:
|
||||
return {}
|
||||
dates, closes, adjs = (list(x) for x in zip(*rows))
|
||||
r = [a / c for _, c, a in rows]
|
||||
if r[i - 1] <= 0 or r[i] <= 0 or r[i] <= r[i - 1]:
|
||||
return {}
|
||||
f = 1.0 - r[i - 1] / r[i]
|
||||
if not (0.005 < f < 0.95):
|
||||
return {}
|
||||
d_amt = f * closes[i - 1]
|
||||
E = None
|
||||
for t in range(i + 1, min(i + 11, len(rows))):
|
||||
if closes[t] < closes[t - 1] - 0.5 * d_amt:
|
||||
E = t
|
||||
break
|
||||
if E is None:
|
||||
return {}
|
||||
k = 1.0 - f
|
||||
patch = {}
|
||||
for t in range(i, E):
|
||||
patch[dates[t]] = {"Adj Close": round(adjs[t] * k, 6)}
|
||||
return patch, f, d_amt, E
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--data", type=Path, default=ROOT_DEFAULT)
|
||||
ap.add_argument("--only", default="", help="comma-separated symbols")
|
||||
ap.add_argument("--dry-run", action="store_true")
|
||||
args = ap.parse_args()
|
||||
only = {s.strip().lower() for s in args.only.split(",") if s.strip()}
|
||||
|
||||
hits = scan(args.data, 0.05, 0.02, 3)
|
||||
if only:
|
||||
hits = [h for h in hits if h["symbol"] in only]
|
||||
by_sym: dict[str, list] = {}
|
||||
for h in hits:
|
||||
by_sym.setdefault(h["symbol"], []).append(h)
|
||||
applied = 0
|
||||
for sym, hs in sorted(by_sym.items()):
|
||||
rows = load_rows(sym, args.data)
|
||||
cells, notes = {}, []
|
||||
for h in hs:
|
||||
try:
|
||||
patch, f, d_amt, E = repair(sym, h["date"], rows)
|
||||
except Exception as ex:
|
||||
print(f" !! {sym} {h['date']}: {ex}")
|
||||
continue
|
||||
if not patch:
|
||||
print(f" -- {sym} {h['date']}: not repairable (skipped)")
|
||||
continue
|
||||
cells.update(patch)
|
||||
dates = [x[0] for x in rows]
|
||||
di = next(k for k, r in enumerate(rows) if r[0] == h["date"])
|
||||
notes.append(NOTE.format(d=h["date"], e=dates[E], c0=rows[di - 1][1],
|
||||
c1=rows[E][1], d_amt=d_amt,
|
||||
spike=h["adj_ret"]))
|
||||
print(f" {sym} {h['date']}: patch {len(patch)} cell(s), "
|
||||
f"implied dist {d_amt:.3f}, ex-div {dates[E]}")
|
||||
if not cells:
|
||||
continue
|
||||
pf = CORR / f"{sym.upper()}.json"
|
||||
d = json.loads(pf.read_text()) if pf.exists() else {"symbol": sym}
|
||||
d.setdefault("symbol", sym)
|
||||
d.setdefault("note", "")
|
||||
if notes:
|
||||
d["note"] = (d["note"] + " " if d["note"] else "") + \
|
||||
" ".join(notes).strip()
|
||||
d.setdefault("history", {})
|
||||
for dt, cells_ in cells.items():
|
||||
d["history"].setdefault(dt, {}).update(cells_)
|
||||
if not args.dry_run:
|
||||
pf.write_text(json.dumps(d, indent=2) + "\n")
|
||||
applied += 1
|
||||
print(f"{'(dry) ' if args.dry_run else ''}wrote {pf.name}: "
|
||||
f"{sum(len(v) for v in d['history'].values())} cell(s)")
|
||||
print(f"{'dry-run: ' if args.dry_run else ''}{applied} symbol(s) repaired "
|
||||
f"of {len(by_sym)} candidate(s)")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Scan for Yahoo adj-close/event-date misalignment.
|
||||
|
||||
Yahoo's Adj Close column applies a distribution's adjustment on the EVENT
|
||||
date from its event feed. When that date precedes the actual ex-div date
|
||||
(the market price only drops later), the adjusted series shows a bogus
|
||||
spike on the event date and a snap-back a day or two later.
|
||||
|
||||
Signature per symbol: a date D with |adj return| >= --min-adj (default 5%)
|
||||
while |close return| on D is < --max-close (default 2%), AND the close
|
||||
drops by more than half the implied adjustment within --horizon following
|
||||
sessions (the price finally going ex-div).
|
||||
|
||||
Usage: python3 scripts/scan_adj_misalign.py [--data DIR] [--out PATH]
|
||||
Prints a summary and a table; writes markdown to --out (default
|
||||
reports/adj_misalign/scan.md).
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
ROOT_DEFAULT = Path.home() / "prog/fin/stocks"
|
||||
|
||||
|
||||
def scan(root: Path, min_adj: float, max_close: float, horizon: int) -> list[dict]:
|
||||
import data as D
|
||||
hits = []
|
||||
syms = sorted({f.name[:-len("-history.csv")] for f in root.glob("*-history.csv")})
|
||||
for i, sym in enumerate(syms):
|
||||
fr = D.FROZEN_DIR / f"{sym}-history.csv"
|
||||
f = fr if (D.FROZEN_DIR.is_dir() and fr.exists()) else root / f"{sym}-history.csv"
|
||||
if not f.exists():
|
||||
continue
|
||||
rows = []
|
||||
for l in f.read_text(errors="replace").splitlines()[1:]:
|
||||
if not l.strip():
|
||||
continue
|
||||
r = l.split(",")
|
||||
if len(r) < 6:
|
||||
continue
|
||||
try:
|
||||
cl, aj = float(r[4]), float(r[5])
|
||||
except ValueError: # e.g. '%!f(<nil>)' from an old goget bug
|
||||
continue
|
||||
rows.append((r[0], cl, aj))
|
||||
if len(rows) < 3:
|
||||
continue
|
||||
closes = [x[1] for x in rows]
|
||||
adjs = [x[2] for x in rows]
|
||||
for i in range(1, len(rows)):
|
||||
if closes[i - 1] <= 0 or adjs[i - 1] <= 0 or closes[i] <= 0:
|
||||
continue
|
||||
adj_ret = adjs[i] / adjs[i - 1] - 1
|
||||
cl_ret = closes[i] / closes[i - 1] - 1
|
||||
if abs(adj_ret) < min_adj or abs(cl_ret) >= max_close:
|
||||
continue
|
||||
# the drop has to land within the next `horizon` sessions
|
||||
j = i + horizon
|
||||
if j >= len(rows) or closes[j] <= 0:
|
||||
continue
|
||||
later_drop = (closes[i - 1] - closes[j]) / closes[i - 1]
|
||||
implied = min(abs(adj_ret), 0.6) # cap: events rarely exceed 60%
|
||||
if later_drop < 0.5 * implied:
|
||||
continue
|
||||
hits.append({
|
||||
"symbol": sym,
|
||||
"date": rows[i][0],
|
||||
"adj_ret": round(adj_ret, 4),
|
||||
"close_ret": round(cl_ret, 4),
|
||||
"close_before": closes[i - 1],
|
||||
"close_after_horizon": closes[j],
|
||||
"drop_later": round(later_drop, 4),
|
||||
})
|
||||
if i % 500 == 0:
|
||||
print(f"{i}/{len(syms)} ...", file=sys.stderr)
|
||||
return hits
|
||||
|
||||
|
||||
def main() -> int:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--data", type=Path, default=ROOT_DEFAULT)
|
||||
ap.add_argument("--min-adj", type=float, default=0.05)
|
||||
ap.add_argument("--max-close", type=float, default=0.02)
|
||||
ap.add_argument("--horizon", type=int, default=3)
|
||||
ap.add_argument("--out", type=Path, default=None)
|
||||
args = ap.parse_args()
|
||||
hits = scan(args.data, args.min_adj, args.max_close, args.horizon)
|
||||
hits.sort(key=lambda h: -abs(h["adj_ret"]))
|
||||
print(json.dumps({"hits": len(hits),
|
||||
"symbols": len({h["symbol"] for h in hits})}, indent=1))
|
||||
for h in hits[:30]:
|
||||
print(f"{h['symbol']:8s} {h['date']} adj {h['adj_ret']:+.1%} "
|
||||
f"(close {h['close_ret']:+.2%}), drop later {h['drop_later']:.1%}")
|
||||
if args.out:
|
||||
args.out.parent.mkdir(parents=True, exist_ok=True)
|
||||
lines = ["# Adj-close / event-date misalignment scan",
|
||||
"",
|
||||
f"params: min_adj={args.min_adj} max_close={args.max_close} "
|
||||
f"horizon={args.horizon}; {len(hits)} hits on "
|
||||
f"{len({h['symbol'] for h in hits})} symbols",
|
||||
"",
|
||||
"| symbol | date | adj ret | close ret | later drop |",
|
||||
"|---|---|---|---|---|"]
|
||||
lines += [f"| {h['symbol']} | {h['date']} | {h['adj_ret']:+.2%} | "
|
||||
f"{h['close_ret']:+.2%} | {h['drop_later']:+.2%} |" for h in hits]
|
||||
args.out.write_text("\n".join(lines) + "\n")
|
||||
print(f"-> {args.out}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user