"""BondStats Open Research Templates | Educational, reproducible analysis.
Independent research. No investment advice. Inputs are illustrative unless stated otherwise.
https://www.bondstats.org/tools/developer-tools/research-templates/
"""
import csv
from pathlib import Path

def auction_tail_bp(stop_yield_pct, when_issued_yield_pct):
    """Positive tail = auction stopped at a higher yield than when-issued.
    Inputs are yields in percentage points (e.g. 4.35), output basis points.
    """
    return (stop_yield_pct - when_issued_yield_pct) * 100

def analyze_csv(path):
    results = []
    with open(path, newline='', encoding='utf-8') as f:
        for r in csv.DictReader(f):
            stop, wi = float(r['stop_yield_pct']), float(r['when_issued_yield_pct'])
            tail = auction_tail_bp(stop,wi)
            results.append({'auction': r['auction'], 'tail_bp': round(tail,3),
                            'classification': 'tail' if tail > 0 else ('through' if tail < 0 else 'on-the-screws')})
    return results

if __name__ == '__main__':
    for row in analyze_csv(Path(__file__).with_name('sample_auctions.csv')): print(row)
