File size: 2,323 Bytes
bc2dac3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Price bar loading, validation, caching, and an optional yfinance fetcher.

Cache layout: <cache_dir>/<interval>/<SYMBOL>.parquet
Index: tz-aware timestamps in Asia/Kolkata. Columns: open, high, low, close, volume.

yfinance caveats: unofficial API, intraday history limited to recent days
(about 60 days for 5m bars). Use it to prototype, not to validate.
"""
from __future__ import annotations

from pathlib import Path

import pandas as pd

TZ = "Asia/Kolkata"
REQUIRED_COLUMNS = ["open", "high", "low", "close", "volume"]


def cache_path(cache_dir: Path | str, symbol: str, interval: str) -> Path:
    return Path(cache_dir) / interval / f"{symbol}.parquet"


def validate_bars(df: pd.DataFrame) -> pd.DataFrame:
    missing = [c for c in REQUIRED_COLUMNS if c not in df.columns]
    if missing:
        raise ValueError(f"bars missing columns: {missing}")
    if df.index.tz is None:
        raise ValueError("bar index must be timezone-aware (Asia/Kolkata)")
    out = df.tz_convert(TZ).sort_index()
    if out.index.duplicated().any():
        raise ValueError("bar index has duplicate timestamps")
    return out[REQUIRED_COLUMNS].astype(float)


def load_bars(symbol: str, cache_dir: Path | str, interval: str = "5min") -> pd.DataFrame | None:
    path = cache_path(cache_dir, symbol, interval)
    if not path.exists():
        return None
    return validate_bars(pd.read_parquet(path))


def save_bars(df: pd.DataFrame, symbol: str, cache_dir: Path | str, interval: str = "5min") -> Path:
    path = cache_path(cache_dir, symbol, interval)
    path.parent.mkdir(parents=True, exist_ok=True)
    validate_bars(df).to_parquet(path)
    return path


def fetch_yfinance(symbol: str, yf_interval: str = "5m", period: str = "60d") -> pd.DataFrame:
    """Download recent bars for an NSE symbol. TODO(verify) limits and data quality."""
    import yfinance as yf

    raw = yf.download(f"{symbol}.NS", interval=yf_interval, period=period,
                      auto_adjust=False, progress=False)
    if raw.empty:
        raise ValueError(f"no data returned for {symbol}")
    if isinstance(raw.columns, pd.MultiIndex):
        raw.columns = raw.columns.get_level_values(0)
    df = raw.rename(columns=str.lower)
    if df.index.tz is None:
        df.index = df.index.tz_localize(TZ)
    return validate_bars(df)