File size: 4,190 Bytes
aeced01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Macro Prices Backfill
=====================
Downloads 1-year daily close prices for macro indicators from Yahoo Finance
and persists them to Supabase `macro_prices`.

Run from the project root:
    python -m backend.scripts.backfill_macro

Requires: SUPABASE_URL + SUPABASE_SERVICE_ROLE_KEY in .env
"""

from __future__ import annotations

import os
import sys
import logging

import httpx
import yfinance as yf
import pandas as pd
from dotenv import load_dotenv

# ── Bootstrap ──────────────────────────────────────────────────────────────────
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "..", ".env"))

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s  %(levelname)-7s  %(message)s",
    datefmt="%H:%M:%S",
)
log = logging.getLogger("backfill_macro")

SUPABASE_URL = os.getenv("SUPABASE_URL", "")
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY", "")

if not SUPABASE_URL or not SUPABASE_KEY:
    log.error("SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY not set in .env")
    sys.exit(1)


MACRO_TICKERS = {
    "India VIX":  "^INDIAVIX",
    "USD/INR":    "INR=X",
    "Nifty 50":   "^NSEI",
    "Gold (INR)": "GC=F",
    "Crude Oil":  "CL=F",
    "Bank Nifty": "^NSEBANK",
}


# ── Supabase helpers ──────────────────────────────────────────────────────────

def sb_headers() -> dict:
    return {
        "apikey": SUPABASE_KEY,
        "Authorization": f"Bearer {SUPABASE_KEY}",
        "Content-Type": "application/json",
        "Prefer": "resolution=merge-duplicates",
    }


def sb_upsert(rows: list[dict]) -> bool:
    """Upsert rows into macro_prices with on_conflict=ticker,date."""
    try:
        resp = httpx.post(
            f"{SUPABASE_URL}/rest/v1/macro_prices?on_conflict=ticker,date",
            headers=sb_headers(),
            json=rows,
            timeout=30,
        )
        resp.raise_for_status()
        return True
    except Exception as exc:
        log.error("Supabase upsert failed: %s", exc)
        return False


# ── Download & upload ──────────────────────────────────────────────────────────

def backfill():
    tickers = list(MACRO_TICKERS.values())
    log.info("Downloading macro data for %d tickers via yf.download…", len(tickers))

    raw = yf.download(
        tickers, period="1y", interval="1d",
        auto_adjust=True, threads=True, progress=False,
    )

    if raw.empty:
        log.error("yf.download returned empty β€” check network or tickers")
        sys.exit(1)

    total_written = 0

    for name, ticker in MACRO_TICKERS.items():
        try:
            if len(tickers) == 1:
                df = raw[["Close"]].copy()
            else:
                df = raw["Close"][[ticker]].copy()
            df = df.dropna().reset_index()
            df.columns = ["date", "close"]
            df["date"] = pd.to_datetime(df["date"]).dt.tz_localize(None)
        except Exception as e:
            log.warning("Failed to extract %s (%s): %s", name, ticker, e)
            continue

        if len(df) < 5:
            log.warning("Skipping %s β€” only %d rows", name, len(df))
            continue

        rows = [
            {
                "ticker": ticker,
                "date": str(row["date"].date()),
                "close": round(float(row["close"]), 4),
            }
            for _, row in df.iterrows()
        ]

        # Upload in 500-row chunks
        written = 0
        for i in range(0, len(rows), 500):
            chunk = rows[i : i + 500]
            if sb_upsert(chunk):
                written += len(chunk)

        log.info("  %-12s  %s  β†’  %d rows written", name, ticker, written)
        total_written += written

    log.info("Backfill complete: %d total rows written across %d tickers.", total_written, len(MACRO_TICKERS))


if __name__ == "__main__":
    backfill()