"""Rebuild the Litecoin.watch LTCC comparison using only Python 3 and daily.csv. Run: python replay.py.txt daily.csv The first data row is the baseline. USD results are rounded only for display. """ import csv from datetime import date from decimal import Decimal, ROUND_HALF_UP, localcontext from pathlib import Path import sys def cents(value): return value.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) def reconcile(filename): with Path(filename).open(encoding='ascii', newline='') as stream: rows = list(csv.DictReader(stream)) if len(rows) < 2: raise ValueError('A baseline and a later observation are required.') days = [] with localcontext() as ctx: ctx.prec = 40 for row in rows: day = date.fromisoformat(row['rate_date']) shares = int(row['shares_outstanding']) assets = Decimal(row['net_assets_usd']) if shares <= 0 or assets <= 0 or (days and day <= days[-1]['date']): raise ValueError(f'Invalid observation for {day}.') per_share = assets / shares if cents(per_share) != Decimal(row['reported_nav_usd']): raise ValueError(f'Implied and published NAV disagree at {day}.') days.append(dict(date=day, shares=shares, assets=assets, per_share=per_share)) changes = [] for before, after in zip(days, days[1:]): ds = after['shares'] - before['shares'] share_effect = ds * after['per_share'] value_effect = before['shares'] * (after['per_share'] - before['per_share']) difference = after['assets'] - before['assets'] if abs(difference - share_effect - value_effect) > Decimal('0.0000001'): raise ValueError('The two components fail to reconcile.') changes.append(dict(date=after['date'].isoformat(), shares_delta=ds, assets_delta=difference, share_effect=share_effect, value_effect=value_effect)) return changes, {k:sum(r[k] for r in changes) for k in ('shares_delta','assets_delta','share_effect','value_effect')} if __name__ == '__main__': changes, total = reconcile(sys.argv[1]) for r in changes: if r['shares_delta']: print(f"{r['date']} shares {r['shares_delta']:+,} assets ${r['assets_delta']:+,.2f} share-count effect ${cents(r['share_effect']):+,.2f}") print(f"Window: net assets ${total['assets_delta']:+,.2f}, share-count effect ${cents(total['share_effect']):+,.2f}, per-share value effect ${cents(total['value_effect']):+,.2f}")